如果需要,我需要使用'ifs''elses'和'if else'创建决策树。 我尝试仅使用ifs,但无法使其正常工作。我已经使用elses和ifs重做了开始部分。我收到未找到“ answer1”的错误。有人可以告诉我这是否是正确的开始吗?我不想再次对整个事情进行编码,而不得不重做很多事情,因为结果不正确。
我只使用if语句,但我想我也必须使用else。如果我要包括所有我尝试过的内容,它将超过200行,因此请不要将其保留到我开始的修订部分。
import java.util.Scanner;
公共类猜测{ 公共静态void main(String [] args){
Scanner guess = new Scanner( System.in );
System.out.println("Think of something");
System.out.println("is it a living animal, plant, or non-living thing?");
String answer0;
answer0 = guess.nextLine();
answer0 = answer0.toUpperCase();
switch (answer0)
{
case "PLANT" : System.out.println("Is it a tree?");
break;
// case "LIVING : System.out.println("Does it have feathers, fur, or neither"); break;
// case "NON-LIVING" : System.out.println("Is it man-made?"); break;
// case "NON LIVING" : System.out.println("Is it man-made?");
default : System.out.println("Invalid");
}
if (answer0.equals("PLANT"))
{
System.out.println("Is it deciduous?");
String = answer1;
answer1 = guess.nextLine();
answer1 = answer1.toUpperCase();
switch (answer1)
{
case "YES" : System.out.println("Is it a fruit tree?");
break;
case "NO" : System.out.println("Is it native to North America?");
break;
default : System.out.println("Invalid");
}
}
if (answer1.equals("NO"))
{
System.out.println("Does it have an enclosed seed?");
String answerq3;
answerq3 = guess.nextLine();
answerq3 = answerq3.toUpperCase();
switch (answerq3)
{
case "YES" : System.out.println("Are you thinking of a cypress tree?");
break;
case "NO" : System.out.println("Are you thinking of a yew?");
break;
default : System.out.println("Invalid");
}
String answer4;
answer4 = guess.nextLine();
answer4 = answer4.toUpperCase();
switch (answer4)
{
case "YES" : System.out.println("Correct");
break;
case "NO" : System.out.println("Wrong");
break;
default : System.out.println("Invalid");
}
}
// if answer = tangerine tree
// system.out.print("Correct");
//}
// else {
// System.out.println("is it a banana tree");
//}
else
{
System.out.println("is it a weeping tree?");
}
else
{
System.out.println("Does it have flowers?");
}
}
}
很难不去描述决策树的预期结果,就会变得混乱或无法发布图片
答案 0 :(得分:0)
因此,我认为您的做法正确。如果是,那么我接下来要问的是,如果没有要问其他问题。然后,您一次又一次地继续。如果我理解正确的代码看起来不错。因此,if
是,而不是else if
,并且else
输入无效。试试吧,祝你好运!