Java If/else语句
我几天前刚刚开始使用java。我目前正在学习这门课程。我还没有遇到任何问题,但现在我被任务32困住了 以下是我到目前为止的代码(总是以Squirrel而不是moose作为输出): 公开课问题32{Java If/else语句,java,if-statement,Java,If Statement,我几天前刚刚开始使用java。我目前正在学习这门课程。我还没有遇到任何问题,但现在我被任务32困住了 以下是我到目前为止的代码(总是以Squirrel而不是moose作为输出): 公开课问题32{ public static void main(String[] args) { boolean animal, vegetable, mineral, smallerthan; String whatIsIt, biggerThan; Scanner keyboard =
public static void main(String[] args) {
boolean animal, vegetable, mineral, smallerthan;
String whatIsIt, biggerThan;
Scanner keyboard = new Scanner(System.in);
System.out.println("Hello and welcome, i've got 2 questions for you!");
System.out.println("Think of an object and i'll try to guess it");
System.out.println();
System.out.println("Question 1) Is it an animal, vegetable or mineral?");
System.out.print(">");
whatIsIt = keyboard.nextLine();
if (whatIsIt == "animal")
animal = true;
if (whatIsIt == "vegetable")
vegetable = true;
if (whatIsIt == "mineral")
mineral = true;
System.out.println("Question 2) Is it bigger than a breadbox?");
System.out.print(">");
biggerThan = keyboard.nextLine();
if (biggerThan == "yes")
smallerthan = false;
if (biggerThan == "no"){
smallerthan = true;}
System.out.print("My guess is that you are thinking of a ");
if (animal = true){
if (smallerthan = true)
System.out.println("squirrel");
}else {
System.out.println("moose");}
}
}
提前谢谢!我也很想听听如何以更智能的方式编写代码的提示。友好一点,记住我才刚刚开始
编辑:好的,我采取了另一种方法。我的第一次尝试真的很奇怪。谢谢你的帮助
工作代码如下:
import java.util.Scanner;
公开课问题32{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String whatIsIt, whatIsIt2;
String animal = "animal";
String mineral = "mineral";
String vegetable = "vegetable";
String bigger = "yes";
String smaller = "no";
System.out.println("Hello and welcome, i've got 2 questions for you!");
System.out.println("Think of an object and i'll try to guess it");
System.out.println();
System.out.println("Question 1) Is it an animal, vegetable or mineral?");
System.out.print(">");
whatIsIt = keyboard.nextLine();
System.out.println("Question 2) Is it bigger than a breadbox?");
System.out.print(">");
whatIsIt2 = keyboard.nextLine();
if (whatIsIt.equalsIgnoreCase(animal)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a moose");
}else{ System.out.println("My guess is that you are thinking of a squirrel");
}
}
if (whatIsIt.equalsIgnoreCase(vegetable)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a melon");
}else{ System.out.println("My guess is that you are thinking of a carrot");
}
}
if (whatIsIt.equalsIgnoreCase(mineral)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a Camaro");
}else{ System.out.println("My guess is that you are thinking of a paper clip");
}
}
System.out.println("I would ask you if I'm right, but I dont actually care.");
}
}
在if语句中,您每次都将animal设置为true
if(animal=true){
,而不是检查它(=
)。
另外,对于字符串,您必须使用
.equals()
而不是=
不要将字符串与=
或!=
进行比较。请理解,这会比较引用--两个字符串变量引用相同的字符串对象,而这不是您想要的。请使用字符串equals(…)
或其等效信号情况(…)
执行函数相等性测试的方法—字符串具有相同顺序的相同字符—这正是您想要的。还要了解赋值和关系相等性之间的差异。另外,还要了解yoda表达式。即使只有凌晨3点的调试会话会永久性地加强这种差异。不确定您使用的是什么IDE,但您应该得到一个关于在if条件内执行赋值的大警告。忽略这些警告肯定会引入错误,特别是对于初学者来说。好的,equalsIgnoreCase(…)工作得很好。对于“在if条件内不执行赋值”,我仍然有点困惑.所以通过我这样做的方式,我总是将布尔值设置为真?绕过它的最佳方式是什么?顺便说一句,我使用Eclipse和1.8,我没有收到任何关于这些问题的警告
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String whatIsIt, whatIsIt2;
String animal = "animal";
String mineral = "mineral";
String vegetable = "vegetable";
String bigger = "yes";
String smaller = "no";
System.out.println("Hello and welcome, i've got 2 questions for you!");
System.out.println("Think of an object and i'll try to guess it");
System.out.println();
System.out.println("Question 1) Is it an animal, vegetable or mineral?");
System.out.print(">");
whatIsIt = keyboard.nextLine();
System.out.println("Question 2) Is it bigger than a breadbox?");
System.out.print(">");
whatIsIt2 = keyboard.nextLine();
if (whatIsIt.equalsIgnoreCase(animal)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a moose");
}else{ System.out.println("My guess is that you are thinking of a squirrel");
}
}
if (whatIsIt.equalsIgnoreCase(vegetable)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a melon");
}else{ System.out.println("My guess is that you are thinking of a carrot");
}
}
if (whatIsIt.equalsIgnoreCase(mineral)){
if (whatIsIt2.equalsIgnoreCase(bigger)){
System.out.println("My guess is that you are thinking of a Camaro");
}else{ System.out.println("My guess is that you are thinking of a paper clip");
}
}
System.out.println("I would ask you if I'm right, but I dont actually care.");
}
}