所以我对java很陌生。我一直在搞乱一些使用扫描仪测试用户输入的 if 语句。当我运行程序时,四个 if 语句中有三个起作用。第四次作为第三次运行......我不知道这是为什么。这是我的代码。希望大家给点建议。
import java.io.*;
import java.util.Scanner;
public class TestFile5
{
public static void main(String[] args) throws IOException
{
//Have two choices? Simple and complicated? Simple example = input (X) input. Complicated = below
Scanner user_input = new Scanner(System.in);
System.out.println("Choose calculation type: Long or Short");
String choicetype;
System.out.print("Type: ");
choicetype = user_input.next();
String selection;
if(choicetype.equals("Long"))
{
System.out.println("Selection: Long");
System.out.println(" ");
System.out.println("This is a calculator");
System.out.println("First select type of function");
System.out.println("1: Addition");
System.out.println("2: Subtraction");
System.out.println("3: Multiplication");
System.out.println("4: Division");
System.out.print("Selection: ");
selection = user_input.next();
if(selection.equals("Addition"))
{
System.out.println("Enter two numbers to add");
int first_number;
System.out.print("1: ");
first_number = user_input.nextInt();
int second_number;
System.out.print("2: ");
second_number = user_input.nextInt();
System.out.println(first_number + second_number);
System.exit(0);
}
if(selection.equals("Subtraction"))
{
System.out.println("Enter two numbers to subtract");
int first_number;
System.out.print("1: ");
first_number = user_input.nextInt();
int second_number;
System.out.print("2: ");
second_number = user_input.nextInt();
System.out.println(first_number - second_number);
System.exit(0);
}
if(selection.equals("Multiplication"));
{
System.out.println("Enter two numbers to multiply");
int first_number;
System.out.print("1: ");
first_number = user_input.nextInt();
int second_number;
System.out.print("2: ");
second_number = user_input.nextInt();
System.out.println(first_number * second_number);
System.exit(0);
}
//Does Not work. Trys to multiply
if(selection.equals("Division"))
{
System.out.println("Enter two numbers to divide");
int first_number;
System.out.print("1: ");
first_number = user_input.nextInt();
int second_number;
System.out.print("2: ");
second_number = user_input.nextInt();
System.out.print("Answer: ");
System.out.print(first_number / second_number);
System.exit(0);
}
}
if(choicetype.equals("Short"))
{
System.out.println("Selection: Short");
System.out.println("");
}
}
}