Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向用户请求影响另一个变量的输入_Java - Fatal编程技术网

Java 如何向用户请求影响另一个变量的输入

Java 如何向用户请求影响另一个变量的输入,java,Java,我需要为用户创建一种方式来输入天气(下雪、下雨或晴天),在他们这样做之后,它会影响汽车的MPG(每加仑英里数)。雪少4,雨少2,晴天没有影响。我得到的提示是在.equals中使用,但我知道我的代码还远远不够。所以我的问题是,我如何要求用户输入一个影响另一个变量的输入,并给出三个可能的答案 import java.util.Scanner; public class AdvancedTripCalc { public static void main(String[] args) {

我需要为用户创建一种方式来输入天气(下雪、下雨或晴天),在他们这样做之后,它会影响汽车的MPG(每加仑英里数)。雪少4,雨少2,晴天没有影响。我得到的提示是在.equals中使用,但我知道我的代码还远远不够。所以我的问题是,我如何要求用户输入一个影响另一个变量的输入,并给出三个可能的答案

 import java.util.Scanner;

public class AdvancedTripCalc {

    public static void main(String[] args) {
        // New Trip Calc.
        String firstname;
        int mpg;
        int miles;
        double price;
        double totalcost;

        Scanner in = new Scanner(System.in);
        System.out.println("Please enter your First Name");
        firstname = in.nextLine();
        System.out.println("Please enter the MPG of your car");
mpg = in.nextInt();
boolean city;
System.out.println("Enter true if trip is in the city false if the trip is on the highway");
city = in.nextBoolean();
if (city){
    mpg = mpg - 2;
}
else { 
    mpg = mpg + 5;
}
boolean weather;
double snowy = 0;
double rainy = 0;
double sunny = 0;
System.out.println("What is the weather like?");
weather = in.equals(snowy);
{
    mpg = mpg - 4;
}
weather = in.equals(rainy);{
    mpg = mpg - 1;
}
weather = in.equals(sunny);{
    mpg = mpg;
}
System.out.println("Please enter the Miles to be traveled");
miles = in.nextInt();
System.out.println("Please enter the price per gallon of gas");
price = in.nextDouble();

totalcost = miles * price / mpg;
System.out.println("Your name is " +firstname);
System.out.println("Your MPG is " +mpg);
System.out.println("Your miles to be traveled is " +miles);
System.out.println("Your price per gallon of gas is " +price);
System.out.println("Your total cost is " +totalcost);

    }

}

这就是我到目前为止所做的。

这里有一个很好的例子,我没有时间给你们写出来。但实际上,您需要为system.in创建一个扫描仪。在继续之前,这将在此时阻止应用程序,并等待system in上的一些输入。通常,最好在此之前进行System.out.print打印,以便用户知道他们需要输入一些内容。

在使用
扫描仪上的
对象(
in
)上的
下一步()
读取用户输入后,您可以使用
if
语句:


如果a和b是字符串,那么java中的字符串比较是通过a.equals(b)完成的

a==b
将测试a和b是否为同一对象。因此,您需要做的是: 将天气作为字符串读取(例如,
String weather=in.readLine()
),然后可以将其与以下内容进行比较:

if(weather.equals(“snowy”){..}

else if(weather.equals(“rainy”){..}


依此类推

您不能在.equals
中使用
,但您可以先读取字符串,然后再读取到
.equals

String weather = in.next();
if("sunny".equals(weather)) {
    mpg = mpg - 4;
} else if("rainy".equals(weather)) {
    mpg = mpg - 1;
}

您可以做的是获取用户的输入,并使用case语句(甚至if语句)进行比较,如下所示:


Downvoter请解释它是错误的,正如你所知道的。但是现在修复了,所以会改变。你是对的,但是给一个男人一个编辑的机会,因为这只是一个疏忽;)当然,我会解释的,但是你在我可以之前就删除了它。我使用了这个方法,它很有效!非常感谢你的帮助,我知道这可能是非常基本的。
String weather = in.next();
if("sunny".equals(weather)) {
    mpg = mpg - 4;
} else if("rainy".equals(weather)) {
    mpg = mpg - 1;
}
System.out.println("What is the weather like?");
Scanner read = new Scanner(System.in);
string weather = read.next()
if (weather.equals("snowy")){
    mpg -= 4;}
else if (weather.equals("rainy")){
    mpg -= 2;}