gpt4 book ai didi

java - 如果语句跳到 else

转载 作者:行者123 更新时间:2023-11-29 04:07:31 25 4
gpt4 key购买 nike

我是从 Python 开始接触 Java 的新手,所以请原谅我的迟钝。我正在尝试制作一个简单的 if 语句,但它不起作用:(。它忽略了 if 语句并直接进行其他操作。

我尝试在 if 语句中使用 .contains 和 .equalsIgnoreCase。

package me.johnminton;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);

String species_animal;
System.out.println("Please enter your species: ");
species_animal = user_input.next();

if (species_animal.contains("Erectus")) {
System.out.println("random input statement");


}
else
{
System.out.println("okay");
}
}
}

如果我在第一个输入中输入直立人,我希望它输出“随机输入语句”。但相反,它直接进入 else 并输出“okay”。

最佳答案

next() 方法仅从扫描器中获取单个单词,但您可以通过为扫描器指定定界符来更改该行为。

在您的情况下,如果您键入 Eructussian 或类似的东西,您将获得所需的结果,但如果您键入 Home Erectus,则不会。

我怀疑您打算使用 nextLine() 而不是 next(),它会获取整行文本。

关于java - 如果语句跳到 else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57549220/

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