gpt4 book ai didi

java - 如何让我的代码使用带有 if 语句的扫描仪跳过用户输入,

转载 作者:太空宇宙 更新时间:2023-11-04 10:55:03 26 4
gpt4 key购买 nike

我正在使用扫描仪询问用户输入(压力、体积、温度、摩尔),但我正在 IF 语句中求解两个不同的方程。我试图让它只请求所需的变量而不是所有变量。例如,求解摩尔时,我只需要压力、气体、温度和体积。我不需要询问用户“摩尔数”,因为这就是我想要解决的问题。有人可以帮忙吗?谢谢,

import java.util.Scanner;

/**
*
* @author 261141
*/
public class IdealGasLaw1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

Scanner s = new Scanner(System.in);
Scanner in = new Scanner(System.in);

String answer;

System.out.println("What do you want to solve for?");
answer = s.nextLine();


System.out.print("Pressure? (in Torr) ");
double pressure = in.nextDouble();

System.out.print("Volume? (in liters) ");
double volume = in.nextDouble();

System.out.print("Temp ? (in Kelvin) ");
double temp = in.nextDouble();

System.out.print("Moles? ");
double moles = in.nextDouble();

double gas = 62;

double PresAns = (pressure)*(volume)/gas * (temp+273);
double MoleAns = (pressure)*(moles);

if (answer.equals("Moles" )) {
System.out.println("Moles =" +MoleAns +" moles");
}
else
System.out.println("Pressure =: "+PresAns +" Torr");
}
}

最佳答案

使用answer来决定是否需要痣。例如:

double moles = 0;
if (!answer.equalsIgnoreCase("Moles")) { // note the use of the NOT symbol
System.out.print("Moles? ");
moles = in.nextDouble();
}

此外,moles 是在 if 语句之外声明的,并且必须进行初始化,否则代码将无法编译。对其他输入变量尝试类似的操作。

关于java - 如何让我的代码使用带有 if 语句的扫描仪跳过用户输入,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47399065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com