-2

我是 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);


}

}

任何帮助将不胜感激。

4

2 回答 2

0

删除;语句末尾的 。如果语句后面不加a {},只会将下一条语句放在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");
}
于 2013-10-25T13:19:24.180 回答
0

you have ; in every if statements like

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

If you keep ; then it means that end of statement.So even ifuserinputOperation.equals("one")); returns false this will be executedSystem.out.print("You have chosen to " + one + " two numbers"); similarly remove every ; and the end of if statements

final code is as follows

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);


}

}
于 2013-10-25T13:17:35.747 回答