【发布时间】:2020-02-05 04:16:25
【问题描述】:
如果这很明显,我是初学者抱歉。我有一个练习,我正在尝试完成,但似乎无法弄清楚。我已经搞定了程序的逻辑。我应该用 6 个蘑菇中的 3 个问题来猜测某人在想什么类型的蘑菇。
他们都有自己的特点。我有一个决策树,纸上有“是”和“否”的答案,可以告诉我这个人在猜什么。
我在尝试用 Java 编写代码时遇到的问题是,我尝试使用的 IF 语句拒绝了我正在输入的变量字符串(类型不匹配),即使我已经使该字符串等于布尔值。
我已尝试使用 parse.Boolean() 和 Valueof(),但都没有奏效。我试过简单地做
boolean oui = true;
然后我尝试了
import java.util.Scanner;
public class Champi {
public static void main(String[] args) {
Scanner clavier = new Scanner(System.in);
System.out.println("Pensez a un champignon : amanite tue mouches, pied bleu, girolle,");
System.out.println("cèpe de Bordeaux, coprin chevelu ou agaric jaunissant.");
boolean oui = true;
boolean Oui = true;
boolean non = false;
boolean Non = false;
System.out.print("Est-ce que votre champignon a un anneau (true : oui, false : non) ? ");
String Reponse1 = clavier.nextLine();
if (Reponse1) {
System.out.print("l'agaric jaunissant");
}
else {
System.out.print("l'amanite tue-mouches");}
当然,期望的行为是用户在输入中输入“Yes”或“oui”(因为这是用法语完成的),并且 IF 语句将其转换为“True”或“No”转换为“错误的” 似乎没有任何效果!
感谢您的帮助。
【问题讨论】: