gpt4 book ai didi

java - Java 中正确的用户输入

转载 作者:行者123 更新时间:2023-12-02 05:20:47 27 4
gpt4 key购买 nike

正在解决一个问题,基本代码如下所示,从用户处获取 4 个可能的输入,并根据输入的一个生成响应。不过,我需要添加一个测试来验证是否仅输入了 4 个可能答案中的 1 个。

我知道如何比较 4 个输入中的 2 个,但不能同时比较所有 4 个输入,有人能给我一个想法吗?我更愿意自己找出实际的代码,但指向正确方向的指针会很有帮助。

因此,为了澄清如何 - 如果输入是除“帐单、通函、明信片或信件”以外的任何内容,则产生错误消息 X

    System.out.println("What type of Letter has been received?");
System.out.println("Bill, Circular, Postcard or Letter");
String Letter = kybd.nextLine();

{
if (Letter.equalsIgnoreCase("Bill"))
{
System.out.println("Bills must be paid");
}
else if (Letter.equalsIgnoreCase("Circular"))
{
System.out.println("Circulars are thrown away");
}
else if (Letter.equalsIgnoreCase("Postcard"))
{
System.out.println("Postcards are put on the wall");
}
else if (Letter.equalsIgnoreCase("Letter"))
{
System.out.println("Personal letters are read and have replies written for them");
}
}

最佳答案

            if (Letter.equalsIgnoreCase("Bill"))  
{
System.out.println("Bills must be paid");
}
else if (Letter.equalsIgnoreCase("Circular"))
{
System.out.println("Circulars are thrown away");
}
else if (Letter.equalsIgnoreCase("Postcard"))
{
System.out.println("Postcards are put on the wall");
}
else if (Letter.equalsIgnoreCase("Letter"))
{
System.out.println("Personal letters are read and have replies written for them");
}else{
System.out.println("ERROR");
}

关于java - Java 中正确的用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26536176/

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