2014-01-16 59 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