问题描述
我是编码初学者.我最近正在创建一个聊天程序,用户可以在其中与我的计算机聊天.以下是部分代码:
i'm a beginner in coding. i was recently working with to create a chatting programme where a user will chat with my computer. here is a part of the code:
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"))** { //1 system.out.println("thank goodness"); } else **if (b.equals("it was good"))** { //2 system.out.println("thank goodness"); } else **if (b.equals("bad"))** { //3 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"))**{ //4 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 语句中.
the conditions of the if statements are in bold (surrounded with **). in case of first and the second condition i want my application to do same thing. similarly third and fourth condition. i thought it was possible to somehow group them in if statement.
我试过下面的代码,但它没有编译:
i tried with below code but it doesn't compile:
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(); }
谁能帮我改正?
推荐答案
您可以使用逻辑运算符来组合您的布尔表达式.
you can use logical operators to combine your boolean expressions.
- &&是逻辑and(两个条件都需要true)
- || 是一个逻辑或(至少需要一个条件为true)
- ^ 是一个 xor(恰好一个条件需要是 true)
- (== 按身份比较对象)
- && is a logical and (both conditions need to be true)
- || is a logical or (at least one condition needs to be true)
- ^ is a xor (exactly one condition needs to be true)
- (== compares objects by identity)
例如:
if (firstcondition && (secondcondition || thirdcondition)) { ... }
还有位运算符:
- & 是按位和
- | 是按位或
- ^ 是 xor
- & is a bitwise and
- | is a bitwise or
- ^ is a xor
主要用于bits and bytes操作.不过还有一点不同,我们再来看看这个表达式:
they are mainly used when operating with bits and bytes. however there is another difference, let's take again a look at this expression:
firstcondition && (secondcondition || thirdcondition)
如果您使用逻辑运算符并且 firstcondition 计算结果为 false 那么 java 将不会计算第二个或作为整个逻辑表达式的结果的第三个条件已知为 false.但是,如果您使用按位运算符,那么 java 将不会停止并继续计算所有内容:
if you use the logical operators and firstcondition evaluates to false then java will not compute the second or third condition as the result of the whole logical expression is already known to be false. however if you use the bitwise operators then java will not stop and continue computing everything:
firstcondition & (secondcondition | thirdcondition)