简体   繁体   English

Java IF语句需要多个输入

[英]Java IF statement requiring multiple inputs

So I'm working on a four function calculator for java (yes I know I'm wasting my time, I'm just getting used/into java.) It takes the first two inputs fina and add/subtracts/etc them, and outputs that as an answer, and I attatched a while loop after that answer so the user can input as many more numbers as he would like. 所以我正在为Java开发一个四功能计算器(是的,我知道我在浪费时间,我只是习惯了/进入Java。)它需要前两个输入fina并对其进行加/减/等运算,将其作为答案输出,我在该答案之后加入了一个while循环,以便用户可以输入任意多的数字。

while(finalanswer == 0){
        System.out.println("If finished, type 'done'.");
        System.out.println("To change your operation, type 'change'. Otherwise, enter your next number.");
        if(calc.next().equals("done")){
            finalanswer = 1; break;
        }else if(calc.next().equals("change")){
            System.out.println("'addition', 'subtraction', 'division', or 'multiplication'?");
            userOperation = calc.next();
        }else{
        modanswer = calc.nextDouble();
        if(userOperation.equals("addition")){
            System.out.println(answer += modanswer);
        }else if(userOperation.equals("subtraction")){
            System.out.println(answer -= modanswer);
        }else if(userOperation.equals("division")){
            System.out.println(answer /= modanswer);
        }else{System.out.println(answer *= modanswer);}
    }
    }

That is the loop that allows the user to add more inputs if he wants, but when I try and add a number I have to type two other numbers first (which have no effect as far as I've seen) and then the third number is finally input and added to the original answer. 这是一个循环,允许用户根据需要添加更多的输入,但是当我尝试添加数字时,我必须先键入另外两个数字(据我所见,这没有任何作用),然后是第三个数字最后输入并添加到原始答案中。

Like Pshemo said, you have to store calc.next() before you check the different conditions: 就像Pshemo所说的那样,您必须在检查不同条件之前存储calc.next()

String tmp = calc.next();

if(tmp.equals("done")){
      finalanswer = 1; break;
}else if(tmp.equals("change")){
      System.out.println("'addition', 'subtraction', 'division', or 'multiplication'?");
      userOperation = calc.next();
}else{
.
.
.

As explained briefly by Pshemo, the calc.next() function asks the user to input something, and returns the input. 正如Pshemo简要解释的那样, calc.next()函数要求用户输入某些内容,然后返回输入。 In this code, calc.next() is called three times in each cycle of the while loop. 在这段代码中,在while循环的每个循环中, calc.next()被调用3次。 If the first input isn't "done", it continues. 如果第一个输入不是“完成”,它将继续。 If the second input isn't "change", it continues, and the third input (obtained through calc.nextDouble() ) is assigned to modanswer . 如果第二个输入不是“ change”,它将继续,并且第三个输入(通过calc.nextDouble()获得)分配给modanswer

You need to store the value of calc.next() as a separate variable to avoid having to read the input three times: 您需要将calc.next()的值存储为单独的变量,以避免必须读取输入三遍:

while(finalanswer == 0){
    System.out.println("If finished, type 'done'.");
    System.out.println("To change your operation, type 'change'. Otherwise, enter your next number.");
    String input = calc.next();
    if(input.equals("done")){
        finalanswer = 1; break;
    }else if(input.equals("change")){
        System.out.println("'addition', 'subtraction', 'division', or 'multiplication'?");
        userOperation = calc.next();
    }else{
    modanswer = Integer.parseInt(input);
...

Note that you need to use Integer.parsInt() to convert the String obtained through calc.next() into an Integer. 请注意,您需要使用Integer.parsInt()将通过calc.next()获得的String转换为Integer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM