在Java中输入验证 [英] Input validation in Java

查看:139
本文介绍了在Java中输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过很多次,得到这个工作。我想使它所以如果用户输入比,就是要输入任何其他,那么它给了他们一个错误信息,并将其提示输入新的答案。然而,每一次我尝试这一次,它会显示一个错误信息(我的错误消息)不管我怎么输入,即使它是正确的。帮助?

I have tried plenty of times to get this to work. I want to make it so if a user inputs anything other than what is meant to be input, then it gives them an error message and prompts them to input a new answer. However, every time I attempt this, it displays an error message (my error message) no matter what I input, even if it's correct. Help?

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。我正在一个java课程在学校大气压。

I have attempted it without the boolean, and thought the boolean might work. But it didn't work with or without. What am I doing wrong? And yes, I am a newbie to java. I am taking a java course at school atm.

推荐答案

if ((!myChoice.equalsIgnoreCase("rock")) || (!myChoice.equalsIgnoreCase("paper")) || (!myChoice.equalsIgnoreCase("scissors"))) {
    myChoiceIsntCorrect = true
}

,并把它变成文字:

and put it into words:

如果我的选择是不是摇滚,或者如果我的选择是不是纸,或者如果我的选择是不是剪刀差,我的选择是不正确的。

If my choice isn't "rock" or if my choice isn't "paper" or if my choice isn't "scissors", my choice isn't correct.

所以,现在你的选择设置为岩石

So now set your choice to "rock".

我的选择是岩石。我的选择是不是纸。如此设置,我的选择是不正确的。

My choice is "rock". My choice isn't "paper". So set that my choice isn't correct.

使用&放大器;&安培;

和阅读:我如何在Java中比较字符串

这篇关于在Java中输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆