我是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
谢谢!这真的很有帮助! –