2014-01-16 28 views
0

所以我试图复制我以前玩过的这个游戏。在这个游戏中,你会看到一个数字,并且有一个隐藏的数字。您需要猜测这个隐藏的号码是小号,大号还是与显示的号码相同。我遇到问题需要输入才能工作。我看不出让switch语句起作用。我也遇到了扫描仪问题。虽然它在while循环的外部起作用,但在它内部却不起作用。在我的while循环中遇到这个开关的问题

import java.util.Scanner; 
import java.util.Random; 
public class Jamey { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

      //This will give us the first random shown number 
     Random yourRandom = new Random(); 
     int y = 1+yourRandom.nextInt(10); 

     //Here is the introduction text 
     System.out.println("Welcome to the guessing game!"); 
     System.out.println("The objective of this game is simple."); 
     System.out.println("You will be shown one of two numbers which range between one and ten."); 
     System.out.println("You have to gues if the number shown is larger, smaller, or equal to the hidden number."); 
     System.out.println("If you believe the number you see is larger enter 1."); 
     System.out.println("If you believe the number you see is smaller enter the 3."); 
     System.out.println("If you believe the number you see is the same enter the 2."); 
     System.out.println("Good luck, your number is "+y+"."); 

     boolean isDone = false; 
     while(isDone=false){    

      //This allows the user to guess 
      Scanner guess = new Scanner(System.in); 
      int g = guess.nextInt(); 

      //This will help us to keep score later. 
      int score = 0; 

      //This will give us the new random number 
      Random newRandom = new Random(); 
      int n = 1+newRandom.nextInt(10); 

      //This will give us the new hidden number 
      Random hiddenRandom = new Random(); 
      int r = 1+hiddenRandom.nextInt(10); 

      //This is to allow multiple different inputs 
      switch(score){ 
      case 1 : 
       score +=1; 
       if(y>r){ 
        System.out.println("Correct"); 
        System.out.println("Your new number is "+n+"."); 
       } 
       else{ 
        score +=1; 
        System.out.println("Inccorect, your overall score was "+score+"."); 
        isDone = true; 
       } 
       break; 

      case 2 : 
       score +=1; 
       if(y==r){ 
        System.out.println("Correct"); 
        System.out.println("Your new number is "+n+"."); 
       } 
       else{ 
        System.out.println("Inccorect, your overall score was "+score+"."); 
        isDone = true; 
       } 
       break; 

      case 3 : 
       score +=1; 
       if(y<r){ 
        System.out.println("Correct"); 
        System.out.println("Your new number is "+n+"."); 
       } 
       else{ 
        System.out.println("Inccorect, your overall score was "+score+"."); 
        isDone = true; 
       } 
       break; 
      default: 
       System.out.println("Invalid input."); 
       isDone = true; 
       break; 

      }  
     } 
    } 
} 

回答

1

你while语句使用分配

while(isDone=false) // Incorrect 
while (isDone == false) OR while(!isDone) // Better 

通知的==,而不是仅仅=。你没有做比较,每次迭代只设置isDone为假。

您的switch语句失败,因为您正在检查您的score变量而不是猜测变量。

switch(score) // Incorrect 
switch(g) // Better 

此外,要创建大量Random的对象时,你只需要创建一个单一实例,然后相应地命名每个变量。例如

Random rand = new Random(); 
int yourRandom = 1 + rand.nextInt(10); 
int newRandom = 1 + rand.nextInt(10); 
int hiddenRandom = 1 + rand.nextInt(10); 
+0

而(!isDone)看起来更好 – Divers

+0

当有人写,如果(二== true),还是东西我从来没有见过像这样,每个人都写如果(b)。有时候新手会这样做。 – Divers

+0

感谢您的帮助。我不敢相信这很简单。 – user3203973

0

while(isDone=false){ 应该 while(isDone==false){ 甚至更​​好 while(!isDone){

+0

谢谢,最小的东西总是让我。 – user3203973

+0

现在我所得到的是从我的默认语句获取“无效输入”响应。对此有何帮助? – user3203973