2012-06-22 56 views
0

我目前正在完成我的第一個Java課程和基於隨機數字遊戲的任務。 我有一些問題,如果更多和更少的陳述。隨機數字遊戲幫助 - if語句發出

我真的很感謝一些一般的意見。

我期待輸入究其原因,「隨機」數是用於測試目的 我還需要結合get()set()方法作爲分配需要面向對象編程,所以我需要使用多個類

任何意見將高度讚賞

我遇到的問題是,在過去,如果它檢查隨機數語句是在範圍內始終得到輸出,這是超出範圍,無論


package randomnumbergame; 

/* 
    * @author Matthew O 
    */ 

import java.util.Scanner; 
public class Main { 


public static void main(String[] args) 
{ 


int numGuesses = 3; 
int rangeMin = 1; 
int rangeMax = 10; 
int randomNum = 0; 

Scanner scan = new Scanner (System.in); 


System.out.println("Welcome to the random number game!"); 
System.out.println("The object of this game is to correctly " 
+"guess a number between a set range within a certain ammount of guesses."); 
System.out.println("\nThe default range is " + rangeMin + " - " + rangeMax); 
System.out.println("Would you like to change this?");       
System.out.println("Please enter y for yes or n for no and press enter."); 

String range = scan.nextLine();   
char changeRange = range.charAt(0);  
if (! (changeRange == 'Y' || changeRange == 'y'|| changeRange == 'n' || changeRange == 'N')) 
    { 
    System.out.println("Error: invalid input entered for the interstate question"); 
    System.exit(0);               
    }                   

if (changeRange == 'Y' || changeRange == 'y')  
    { 
    System.out.println ("\nPlease enter the new numerical minimum value for the range");  
    int newRangeMin = scan.nextInt();            
    rangeMin = newRangeMin;         
    System.out.println ("\nPlease enter the new maximum numerical value for the range");  
    int newRangeMax = scan.nextInt();            
    rangeMax = newRangeMax;   
    } 

{ 
System.out.println("\nThe default number of guesses is " + numGuesses);   
System.out.println("Would you like to change this?");       
System.out.println("\nPlease enter y for yes or n for no and press enter.");  

String guesses = scan.next();             
char changeGuesses = guesses.charAt(0);           
if (! (changeGuesses == 'Y' || changeGuesses == 'y' || changeGuesses == 'n' || changeGuesses 
== 'N'))       

    { 
System.out.println("Error: invalid input entered for the interstate question");   
System.exit(0); 
    } 

if (changeGuesses == 'Y' || changeGuesses == 'y') 
    { 
System.out.println("\nPlease enter the new number of permitted guesses");  
int newGuesses = scan.nextInt();            
numGuesses = newGuesses;              
    } 

    { 
System.out.println ("\n\n\n\n\n\n\n\n\n");          
System.out.println("Welcome to the random number game!\n");      
System.out.println("You have chosen a range of " + rangeMin + " to " + rangeMax); 
System.out.println("You have chosen to have " + numGuesses + " Guesses");  
    } 
} 

{ 
System.out.println("\nPlease press y to input the random number or n" + 
" to let the number be automatically generated");   
String random = scan.next();              
char changeRandom = random.charAt(0);           
if (! (changeRandom == 'Y' || changeRandom == 'y' || changeRandom == 'n' || 
changeRandom == 'N'))        
    { 
    System.out.println("Error: invalid input entered for the interstate question");  
    System.exit(0); 
    } 

    if (changeRandom == 'Y' || changeRandom == 'y') 
    { 
    System.out.println("\nPlease enter the new 'random' number between " 
    + rangeMin + " and " + rangeMax);       
    int inputRandom = scan.nextInt();          
    randomNum = inputRandom;            
    System.out.println("\nThe 'random' number chosen for the game is " + randomNum);   
    } 

if (randomNum < rangeMin);  
    { 
    System.out.println("Random number is out of range!");  
    } 

    } 


} 

} 
+2

正確縮進你的代碼:它是不可讀的。並告訴我們*您遇到哪些問題,以及哪些*聲明。 –

+0

您是否可以將代碼縮小到您遇到問題的部分?另外,請告訴我們問題是什麼。 「我遇到了一些問題」不是對你的問題非常有幫助的描述。 – Daniel

+0

有_what_問題? – TheZ

回答

4

你必須在有關if語句的結束不必要的;,所以即使其被評估,結果是什麼都沒有。

if (randomNum < rangeMin); 
      { 
       System.out.println("Random number is out of range!");   
      } 

無論如何評估if,郵件將始終打印。上述以

變化:

if (randomNum < rangeMin) 
       { 
        System.out.println("Random number is out of range!");   
       } 
+1

非常感謝你的回覆,這是我的一個業餘錯誤。也很抱歉讓你的眼睛流血,我會在我的縮進上更加努力。謝謝 –

1

只是刪除後如果;符號。

if (randomNum < rangeMin); 
{ 
System.out.println("Random number is out of range!"); 
} 
3
if (randomNum < rangeMin); 
{ 
    System.out.println("Random number is out of range!"); 
} 

的問題是在;(分號)上的第一行。 Java認爲這是if條件的結尾,並且每次都打印出錯信息。聲明然後說:

「如果(randomNum < rangeMin),什麼都不做。然後打印出'隨機數超出範圍!'「。