所以我對java很陌生。我一直在使用掃描儀來測試用戶輸入的if語句。當我運行該程序時,四條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("");
}
}
}
謝謝。我有點認爲這將是明顯的東西 – Schmidty15
沒問題 - 有時它只是需要另一雙眼睛看到這些東西。 – PinkElephantsOnParade