为什么此代码会打印所有if语句条件?

时间:2013-10-25 13:14:59

标签: java if-statement

我是Java的新手,我正在尝试编写一个基本的计算器。

但是,在下面的代码中,当我输入任何值时,它会打印if块中的所有语句。 我觉得无论我写的是什么,都符合所有的条件,因此打印所有的陈述,但我看不出我做错了什么。

import java.util.Scanner;

public class calculate {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //variable to store user input
    String userinputOperation;
    int userinputNumber;
    int userinputNumber2; 
    int result;

    //user input variables
    Scanner myscanner = new Scanner(System.in);
    Scanner number1 = new Scanner(System.in);
    Scanner number2 = new Scanner(System.in);

    //different options for calculator i.e., add, subtract, divide, multiply.
    String one = "Add"; // addition
    String two = "Subtract"; // subtract
    String three = "Multiply"; // multiplication
    String four = "Divide"; // division
    String five = "Exit the application."; // exit

    //explain how to use application
    System.out.print("Welcome to Barney's Calculator.");
    System.out.println(" What would you like to do?");
    System.out.println("Write 'one' to add two numbers.");
    System.out.println("Write 'two' to subtract two numbers.");
    System.out.println("Write 'three' to multiply two numbers together.");
    System.out.println("Write 'four' to divide two numbers.");
    System.out.println("Write 'five' to exit.");



    //obtain user input
    userinputOperation = myscanner.nextLine();

    //explain what user has selected
    if (userinputOperation.equals("one")); //add
    System.out.print("You have chosen to " + one + " two numbers");

    if (userinputOperation.equals("two")); //subtract
    System.out.print("You have chosen to " + two + " two numbers");

    if (userinputOperation.equals("three")); //multiply
    System.out.print("You have chosen to " + three + " two numbers");

    if (userinputOperation.equals("four")); //divide
    System.out.print("You have chosen to " + four + " two numbers");

    if (userinputOperation.equals("five")); //exit
    System.out.print("You have chosen to " + one);

    //obtain what the numbers the user wants to operate with

    System.out.println("Input the first number you want to operate with: ");
    userinputNumber = number1.nextInt();

    System.out.println("Input the second number you want to operate with: ");
    userinputNumber2 = number2.nextInt();

    //calculate stuff

    if (userinputOperation == "one"); //add the numbers
    result=(userinputNumber + userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "two"); //subtract the numbers
    result=(userinputNumber - userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "three"); //multiply
    result=(userinputNumber * userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "four"); //add
    result=(userinputNumber / userinputNumber2);
    System.out.print(result);


}

}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

在每个if语句中都有;,例如

if (userinputOperation.equals("one")); //add

如果您保留;,则表示声明结束。即使ifuserinputOperation.equals("one"));返回false,也会执行System.out.print("You have chosen to " + one + " two numbers"); 同样删除;语句的每个if和结尾

最终代码如下

import java.util.Scanner;

public class calculate {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //variable to store user input
    String userinputOperation;
    int userinputNumber;
    int userinputNumber2; 
    int result;

    //user input variables
    Scanner myscanner = new Scanner(System.in);
    Scanner number1 = new Scanner(System.in);
    Scanner number2 = new Scanner(System.in);

    //different options for calculator i.e., add, subtract, divide, multiply.
    String one = "Add"; // addition
    String two = "Subtract"; // subtract
    String three = "Multiply"; // multiplication
    String four = "Divide"; // division
    String five = "Exit the application."; // exit

    //explain how to use application
    System.out.print("Welcome to Barney's Calculator.");
    System.out.println(" What would you like to do?");
    System.out.println("Write 'one' to add two numbers.");
    System.out.println("Write 'two' to subtract two numbers.");
    System.out.println("Write 'three' to multiply two numbers together.");
    System.out.println("Write 'four' to divide two numbers.");
    System.out.println("Write 'five' to exit.");



    //obtain user input
    userinputOperation = myscanner.nextLine();

    //explain what user has selected
    if (userinputOperation.equals("one")) //add
    System.out.print("You have chosen to " + one + " two numbers");

    if (userinputOperation.equals("two")) //subtract
    System.out.print("You have chosen to " + two + " two numbers");

    if (userinputOperation.equals("three")) //multiply
    System.out.print("You have chosen to " + three + " two numbers");

    if (userinputOperation.equals("four")) //divide
    System.out.print("You have chosen to " + four + " two numbers");

    if (userinputOperation.equals("five")) //exit
    System.out.print("You have chosen to " + one);

    //obtain what the numbers the user wants to operate with

    System.out.println("Input the first number you want to operate with: ");
    userinputNumber = number1.nextInt();

    System.out.println("Input the second number you want to operate with: ");
    userinputNumber2 = number2.nextInt();

    //calculate stuff

    if (userinputOperation == "one") //add the numbers
    result=(userinputNumber + userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "two") //subtract the numbers
    result=(userinputNumber - userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "three") //multiply
    result=(userinputNumber * userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "four") //add
    result=(userinputNumber / userinputNumber2);
    System.out.print(result);


}

}

答案 1 :(得分:0)

删除语句末尾的;。 如果在语句后没有添加{},它只会将下一个语句放在if块中。在这种情况下,您有一个空语句,分号。

转过来

if (userinputOperation.equals("one"));
System.out.print("You have chosen to " + one + " two numbers");

进入这个

if (userinputOperation.equals("one"))
System.out.print("You have chosen to " + one + " two numbers");

甚至更好,因为它更清晰,添加括号

if (userinputOperation.equals("one")){
    System.out.print("You have chosen to " + one + " two numbers");
}