gpt4 book ai didi

java - 将生成的回复与 if 语句混合在一起

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:22 25 4
gpt4 key购买 nike

我正在做一些编程(自学),我一直在努力处理 else 语句,但我修复了它。现在,当我运行它时,它会运行错误的回复..如果这是有道理的。我希望我说得足够清楚,可以为您提供帮助!w

import java.util.Scanner;
public class Test {

private static Scanner scanner;

public static void main(String[] args) {
// TODO Auto-generated method stub
String answer;
scanner = new Scanner(System.in);

System.out.println("Hello Human");
System.out.println("Do you want to build a snowman?");
answer = scanner.next();

if (answer.equals("Yes"))
System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");

{
else


System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.

}

}

}

最佳答案

您为 if-else block 提供了错误的大括号。请检查以下代码。

您可以使用 equalsIgnoreCase() 来区分用户输入的大小写。

import java.util.Scanner;
public class Test {

private static Scanner scanner;

public static void main(String[] args) {
// TODO Auto-generated method stub
String answer;
scanner = new Scanner(System.in);
System.out.println("Hello Human");
System.out.println("Do you want to build a snowman?");
answer = scanner.next();
if (answer.equalsIgnoreCase("Yes")) {
System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
}
else {
System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.
}
}
}

PS: 这里的 if 和 else` block 只有单个语句,如果您愿意,可以完全删除它们。但如果你有多个,那么你必须使用括号。

关于java - 将生成的回复与 if 语句混合在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32961386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com