0

我目前正在完成我的第一个 Java 课程和基于随机数游戏的作业。我对一些更大和更少的陈述有一些问题。

我真的很感激一些一般性的评论。

我希望输入“随机”数字的原因是出于测试目的我还需要合并get()set()方法,因为分配需要面向对象的编程,所以我需要使用多个类

任何建议将不胜感激

我遇到的问题是检查随机数是否在范围内的最后一个 if 语句总是得到它超出范围的输出,不管


 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!");     
    }

    }


 }

}
4

3 回答 3

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!");           
                }
于 2012-06-22T16:04:53.897 回答
3
if (randomNum < rangeMin);
{
    System.out.println("Random number is out of range!");
}

问题出;在第一行的(分号)中。Java 认为这是该if条件的结束,并且每次都打印您的错误消息。然后声明说:

“如果(randomNum < rangeMin),什么也不做。然后打印出'随机数超出范围!' 。”

于 2012-06-22T16:05:45.290 回答
1

只需;在 if 之后删除符号。

if (randomNum < rangeMin);
{
System.out.println("Random number is out of range!");
}
于 2012-06-22T16:05:22.443 回答