2014-10-11 97 views
0

我已经尝试了很多次,以使其工作。我想这样做,如果用户输入除了要输入的内容之外的任何东西,那么它会给他们一个错误信息并提示他们输入一个新的答案。然而,每次我尝试这个时,无论我输入什么,即使它是正确的,它都会显示一条错误消息(我的错误消息)。帮帮我?输入验证在Java

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课程。

回答

0

采取

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

,并把它变成文字:

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

所以现在将您的选择设置为"rock"

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

使用&&

,念道:How do I compare strings in Java?

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

的问题是在这if语句,你说如果选择的不是岩石或没有纸或不剪。 当用户输入其中一个时,它会失败其他2.

尝试使用& &符号代替。

+0

谢谢你们,解决了我的问题!尽管如此,在我看来,它更有意义。 – coreynj 2014-10-11 01:20:29

+0

你需要考虑你写的代码。 如果它不是摇滚乐或它不是纸或它不是剪刀 当你选择摇滚时,你只能通过其中1/3的测试。 它会失败其他2 – 2014-10-11 02:00:30

0

有两个问题与您输入验证,首先是,作为人指出的那样,你需要使用& &不||因为通过使用如果我的输入不等于a或不等于b或不等于c,则表示输入必须等于a,b和c,因为如果它不等于它们中的任何一个,你会得到一个真正的价值。第二个问题是,您永远不会在循环内部更改myChoiceIsntCorrect,这意味着一旦进入该循环,myChoiceIsntCorrect将始终评估为true,并且您的程序将一直保持循环。

0

这里有两个问题。

首先,您的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"是不同的对象。您的arr[select] == "..." ifs需要替换为"...".equals(arr[select])

0

你有事情发生在这里。

  1. 如果这段代码是正确的,将导致无限循环。观察:

    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(); 
    } 
    

    您从不更新myChoiceIsntCorrect

    相反,考虑将检查逻辑移动到另一个方法中,并将环路更改为do...while,这样您就可以获得询问的好处以及错误检查的好处。你也想修正你的布尔值;在这里,我已经改变它是肯定的而不是否定的(这会导致混淆)。

    private boolean valid(String word) { 
        return myChoice.equalsIgnoreCase("rock") || 
          myChoice.equalsIgnoreCase("paper") || 
          mychoice.equalsIgnoreCase("scissors"); 
    } 
    
    // block of code to follow 
    String myChoice; 
    do { 
        myChoice = scan.nextLine(); 
    } while(!valid(myChoice)); 
    
  2. 你在一个事例正确检查字符串,而不是在另一:

    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 
        } 
    } 
    

上述所有的方法都是为了提高可读性和意图的透明度。如果你不想让后者更清楚(即你只想修复坏的==比较),那很好。