這是我在Java中如何在Java中從控制檯獲取字符串輸入?
import java.util.Scanner;
class calc
{
public static void main(String[] args)
{
boolean go=true;
Scanner input=new Scanner(System.in);
while(go)
{
int num1;
int num2;
int total;
int choice;
System.out.println("\nHi This is Console type Calculator");
System.out.println("1. Addition");
System.out.println("2. Substraction");
System.out.println("3. Multiply");
System.out.println("4. Divisoin");
System.out.print("Enter Your Choice : ");
choice=input.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter First Number");
num1=input.nextInt();
System.out.println("Enter Second Number");
num2=input.nextInt();
total=num1+num2;
System.out.println("Addition of "+num1+" and "+num2+" are "+total);
break;
case 2:
System.out.println("Enter First Number");
num1=input.nextInt();
System.out.println("Enter Second Number");
num2=input.nextInt();
total=num1-num2;
System.out.println("Substraction of "+num1+" and "+num2+" are "+total);
break;
case 3:
System.out.println("Enter First Number");
num1=input.nextInt();
System.out.println("Enter Second Number");
num2=input.nextInt();
total=num1*num2;
System.out.println("Multiplication of "+num1+" and "+num2+" are "+total);
break;
case 4:
System.out.println("Enter First Number");
num1=input.nextInt();
System.out.println("Enter Second Number");
num2=input.nextInt();
total=num1/num2;
System.out.println("Divistion of "+num1+" and "+num2+" are "+total);
break;
default:
System.out.println("Please Choose right option...Try again");
break;
}
System.out.println("Do You Want more Calculation...Yes/No");
String str=input.nextLine();
System.out.println("Do You Want more Calculation...Yes/No");
String str1=input.nextLine();
if("no".equals(str1))
{
go=false;
System.out.println("Thanks For using...Bye");
}
}
}
}
代碼和我的代碼,以輸入以下部分有問題。這不是從用戶的任何輸入中跳過這部分。這段代碼有沒有問題?
System.out.println("Do You Want more Calculation...Yes/No");
String str1=input.nextLine();
if("no".equals(str1))
{
go=false;
System.out.println("Thanks For using...Bye");
}
你能說出你在str1中獲得了什麼樣的價值嗎? – GurV
閱讀[Yoda條件](https://en.wikipedia.org/wiki/Yoda_conditions) – Guy
@GurwinderSingh它不會在str1中輸入任何內容。 – Mathur