我是java編程的初學者,所以我經常瀏覽以使用我的基本知識來查找初學者項目。最近我正在創建一個聊天程序,用戶可以與我的電腦聊天。這裏是代碼的一部分,因爲我不需要顯示整個代碼:你可以在if語句中有兩個條件
System.out.println("Hello, what's our name? My name is " + answer4);
String a = scanner1.nextLine();
System.out.println("Ok, Hello, " + a + ", how was your day, good or bad?");
String b = scanner2.nextLine();
**if (b.equals("good"))** {
System.out.println("Thank goodness");
} else **if (b.equals("it was good"))** {
System.out.println("Thank goodness");
} else **if (b.equals("bad"))** {
System.out.println("Why was it bad?");
String c = scanner3.nextLine();
System.out.println("Don't worry, everything will be ok, ok?");
String d= scanner10.nextLine();
}
else **if (b.equals("it was bad"))**{
System.out.println("Why was it bad?");
String c = scanner3.nextLine();
System.out.println("Don't worry, everything will be ok, ok?");
String d= scanner10.nextLine();
}
if(age<18){System.out.println("How was school?");}
else if (age>=18){System.out.println("How was work?");}
if語句的條件是粗體。第一和第二是相似的,第三和第四也是如此。我認爲這是可能的,但它不是:
if (b.equals("good"),b.equals("it was good")) {
System.out.println("Thank goodness");
} else if (b.equals("bad"),(b.equals("it was bad"))) {
System.out.println("Why was it bad?");
String c = scanner3.nextLine();
System.out.println("Don't worry, everything will be ok, ok?");
String d= scanner10.nextLine();
}
有人可以糾正它給我嗎?我真的很感激它!
'如果(b.equals( 「好」)|| b.equals( 「這是很好的」))''的'||是或操作 – luizfzs
謝謝!這真的很有幫助! –