输入 验证 java,在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!");

}

}

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.

解决方案

Take

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.

Use &&.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值