Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的输入验证_Java_String_Validation_Boolean - Fatal编程技术网

Java中的输入验证

Java中的输入验证,java,string,validation,boolean,Java,String,Validation,Boolean,我已经试了很多次来让它工作。我想这样做,如果用户输入的内容不是要输入的内容,那么它会给他们一条错误消息,并提示他们输入新的答案。但是,每次我尝试这样做时,无论我输入什么,它都会显示一条错误消息(我的错误消息),即使它是正确的。帮忙 import java.util.Random; import java.util.Scanner; public class RandomSelect { public static void main(String[] args) {

我已经试了很多次来让它工作。我想这样做,如果用户输入的内容不是要输入的内容,那么它会给他们一条错误消息,并提示他们输入新的答案。但是,每次我尝试这样做时,无论我输入什么,它都会显示一条错误消息(我的错误消息),即使它是正确的。帮忙

import java.util.Random;
import java.util.Scanner;
public class RandomSelect 
{
    public static void main(String[] args) 
    {
         String [] arr = {"rock", "paper", "scissors"};
         Random random = new Random();
         Scanner scan = new Scanner(System.in);
         System.out.println("Please select: rock, paper, or scissors?");
         String myChoice = scan.nextLine();
         boolean myChoiceIsntCorrect = false;
         if ((!myChoice.equalsIgnoreCase("rock")) || (!myChoice.equalsIgnoreCase("paper")) || (!myChoice.equalsIgnoreCase("scissors")))
         {
            myChoiceIsntCorrect = true;
         }
         while ( myChoiceIsntCorrect == true )
         {
            System.out.println("Your input wasn't either rock, paper, or scissors. Please select one of the 3.");
            myChoice = scan.nextLine();
         }

         int select = random.nextInt(arr.length); 
         System.out.println("Computer selected " + arr[select] + ".");

         if (arr[select] == "rock" && myChoice.equalsIgnoreCase("paper"))
            System.out.println("You win!");
         if (arr[select] == "rock" && myChoice.equalsIgnoreCase("scissors"))
            System.out.println("You lose!");
         if (arr[select] == "rock" && myChoice.equalsIgnoreCase(arr[select]))
            System.out.println("It's a tie!");
         if (arr[select] == "paper" && myChoice.equalsIgnoreCase("rock"))
            System.out.println("You lose!");
         if (arr[select] == "paper" && myChoice.equalsIgnoreCase("scissors"))
            System.out.println("You win!");
         if (arr[select] == "paper" && myChoice.equalsIgnoreCase(arr[select]))
            System.out.println("It's a tie!");
         if (arr[select] == "scissors" && myChoice.equalsIgnoreCase("paper"))
            System.out.println("You lose!");
         if (arr[select] == "scissors" && myChoice.equalsIgnoreCase("rock"))
            System.out.println("You win!");
         if (arr[select] == "scissors" && myChoice.equalsIgnoreCase(arr[select]))
            System.out.println("It's a tie!");
    }
}
我在没有布尔值的情况下尝试了它,并且认为布尔值可能会起作用。但不管有没有,它都不起作用。我做错了什么?是的,我是java的新手。我正在学校atm机上一门java课程。

if ((!myChoice.equalsIgnoreCase("rock")) || (!myChoice.equalsIgnoreCase("paper")) || (!myChoice.equalsIgnoreCase("scissors"))) {
    myChoiceIsntCorrect = true
}
用文字表达:

如果我的选择不是“石头”,或者如果我的选择不是“纸”,或者如果我的选择不是“剪刀”,那么我的选择是不正确的

因此,现在将您的选择设置为
“rock”

我的选择是“摇滚”。我的选择不是“纸”。所以设置为我的选择不正确

使用
&&

并阅读:

问题在于if语句,你说的是选择不是石头,不是布,不是剪刀。 当用户输入其中一个时,另两个将失败


尝试改用&&号。

输入验证有两个问题,第一个问题是,正如其他人指出的,您需要使用&¬ | | |,因为如果我的输入不等于a或不等于b或不等于c,则表示输入必须等于a、b和c,因为如果它不等于其中任何一个,你会得到一个真实的值。第二个问题是,您永远不会在循环内更改MyChoiceIntCorrect,这意味着一旦进入该循环,MyChoiceIntCorrect的计算结果将始终为true,并且您的程序将永远保持循环

这里有两个问题

首先,您的while循环将永远运行,因为您没有在循环中更新
myChoiceIsntCorrect
。您应该将其更改为以下内容:

boolean invalidChoice = true;
do {
    myChoice = scan.nextLine();
    if ((!myChoice.equalsIgnoreCase("rock")) || (!myChoice.equals("paper")) || (!myChoice.equals("scissors"))) {
        System.out.println("Your input wasn't either rock, paper, or scissors. Please select one of the 3.");
    } else {
        invalidChoice = false;
    }
} while ( invalidChoice );
请注意,这里可以通过反转if和else子句来简化if

其次,将字符串与
==
进行比较。在java中,
==
比较引用。像
“thing”==“thing”
这样的东西总是返回false,因为这两个
“thing”
是不同的对象。您的ifs与
arr[select]==“…”
需要用
“…”.equals(arr[select])
替换

  • 如果这段代码是正确的,它将导致无限循环。注意:

    boolean myChoiceIsntCorrect = false;
     if ((!myChoice.equalsIgnoreCase("rock")) || (!myChoice.equalsIgnoreCase("paper")) || (!myChoice.equalsIgnoreCase("scissors")))
     {
        myChoiceIsntCorrect = true;
     }
     while ( myChoiceIsntCorrect == true )
     {
        System.out.println("Your input wasn't either rock, paper, or scissors. Please select one of the 3.");
        myChoice = scan.nextLine();
     }
    
    您从不更新
    mychoicesntcorrect

    取而代之的是,将检查逻辑移到另一个方法中,并将循环更改为 do…而,这样您就可以得到请求的好处,并结合错误检查的好处。您还需要修复布尔值;在这里,我把它改为肯定的,而不是否定的(这会导致混淆)

  • 您可以在一个实例中正确检查字符串,但在另一个实例中检查不正确:

    if (arr[select] == "rock" && myChoice.equalsIgnoreCase("paper"))
    
    那根本行不通

    您可以修复损坏的比较,或者使用
    switch
    语句使其读得更好,并将实际的布尔比较逻辑移到另一个方法中

    switch(select) {
        case 0: // Rock
            checkWinCondition(arr[0], myChoice);
            break;
        // other cases and default with a good error message
    }
    
    private boolean checkWinCondition(String computerSelected, String userSelected) {
        if(computerSelected.equalsIgnoreCase(userSelected)) {
            // draw
        } else {
            // other conditions
        }
    }
    

  • 上述所有方法都旨在提高可读性和意图的清晰性。如果您不想让后者更清楚(即,您只想修复不好的
    ==
    比较),那没关系。

    谢谢各位,这解决了我的问题!不过,在我看来,使用or更有意义。你需要考虑一下你写的代码。如果你选择石头时不是石头,不是纸,也不是剪刀,你只能通过三分之一的测试。它会让另外两个失败
    if (arr[select] == "rock" && myChoice.equalsIgnoreCase("paper"))
    
    switch(select) {
        case 0: // Rock
            checkWinCondition(arr[0], myChoice);
            break;
        // other cases and default with a good error message
    }
    
    private boolean checkWinCondition(String computerSelected, String userSelected) {
        if(computerSelected.equalsIgnoreCase(userSelected)) {
            // draw
        } else {
            // other conditions
        }
    }