Java猜测游戏 - 优化和提示

时间:2015-11-10 20:09:00

标签: java

我的程序遇到了一个小问题。它是一个简单的Java猜谜游戏,其中生成随机数,并且用户猜测该数字。每次不成功的猜测,用户都会显示“太高”#34;或者"太低"消息,直到他们最终找到号码。找到号码后,系统会提示用户是继续还是退出游戏。

一切正常,除了我只需要在选择无应答后停止程序打印数据输入框。我也想要一些关于优化的建议,并将继续游戏输入(Y或y)视为是,而不是依赖于数字。我已经看过将字符串转换为int并使用我现在使用的方法,但我无法帮助,但觉得有一种更简单的方法。

我为混乱和所有人道歉(我是一名平庸理解的第一年CS学生),我非常感谢您提出的任何建议或提示。

import java.util.Scanner;

public class NumberGame 
{
public static void main(String[] args) 
{
   Scanner keysIn = new Scanner(System.in);

   for (int x =1; x>0; x++)
//Infinite loop to be exited when user quits
   {
   System.out.println("I'm thinking of a number between 1 and 100.");
   System.out.println("What is it?");
   int num = (int) (Math.random()*100+1);
//Generate Random number between 1 and 100      
   while(true)
   {
     int num2 = keysIn.nextInt();
// initialize the variable num2 and set it to next integer input
     System.out.println("Guess:" + num2);

     if (num == num2) 
//If the generated number is equal to the guess
     {
       System.out.println("You go it!");
       System.out.println("Play Again? (Y = 1/N = 0)");
       Scanner scan = new Scanner(System.in);
       int desc = scan.nextInt();
//Make a new scanner and take the "descion" input whether to continue or quit (1 or 0)           
       if(desc != 0){
       break;
//If the input is not 0 break the loop and play perform actions again           
       }
       else{
         System.out.println("Thanks for playing");
         continue;
  //If the input is 0 break the loop and continue the program post-loop             

     }  
     }
     else if(num > num2){
     System.out.println("Too Low.");
//If the number generated is greater than the guessed number print "Too Low"        
     }
     else if (num <num2){
     System.out.println("Too High.");
  //If the number generated is less than the guessed number print "Too High"         
     }
   }
}
}
}

2 个答案:

答案 0 :(得分:1)

如果您希望使用0结束游戏,而不是打破循环,我只会return,因此您在退出时无法获取数据条目。像这样:

else{
    System.out.println("Thanks for playing");
    return;
    //If the input is 0 return and exit the method             
}

要让用户输入字母而不是数字,请尝试这样的事情(您可以添加或删除任意数量的案例):

String desc = scan.nextLine();
if (desc.equals("N") || desc.equals("No")){
    System.out.println("Thanks for playing");
    return;
}
break;

或者您可以使用switch命名您的外部循环,以便在致电break时,您可以突破外部循环,而不仅仅是switch

Scanner keysIn = new Scanner(System.in);
for (int x =1; x>0; x++)
    //Infinite loop to be exited when user quits
{
    System.out.println("I'm thinking of a number between 1 and 100.");
    System.out.println("What is it?");
    int num = (int) (Math.random()*100+1);
    //Generate Random number between 1 and 100
    Outer: //Name loop for breaking later on
    while(true)
    {
        int num2 = keysIn.nextInt();
        // initialize the variable num2 and set it to next integer input
        System.out.println("Guess:" + num2);

        if (num == num2) 
            //If the generated number is equal to the guess
        {
            System.out.println("You go it!");
            System.out.println("Play Again? (Y = 1/N = 0)");
            Scanner scan = new Scanner(System.in);
            String desc = scan.nextLine();
            switch (desc){
            case "Y":
            case "Yes":
                break Outer;
            case "N":
            case "No":
                System.out.println("Thanks for playing");
                return;
            } 
        }
        else if(num > num2){
            System.out.println("Too Low.");
            //If the number generated is greater than the guessed number print "Too Low"        
        }
        else if (num <num2){
            System.out.println("Too High.");
            //If the number generated is less than the guessed number print "Too High"         
        }
    }
}

switch语句与ifelse类似。如果desc匹配&#34; Y&#34;或&#34;是&#34;它会突破循环。如果匹配&#34; N&#34;或&#34;否&#34;它将返回并退出方法。

希望这有帮助!

答案 1 :(得分:0)

import java.io.IOException;
import java.util.Scanner;

public class NumberGame 
{
public static void main(String[] args) throws IOException
{
   Scanner keysIn = new Scanner(System.in);

   for (int x =1; x>0; x++)
//Infinite loop to be exited when user quits
   {
   System.out.println("I'm thinking of a number between 1 and 100.");
   System.out.println("What is it?");
   int num = (int) (Math.random()*100+1);
//Generate Random number between 1 and 100      
   while(true)
   {

     int num2 = keysIn.nextInt();
// initialize the variable num2 and set it to next integer input
     System.out.println("Guess:" + num2);

     if (num == num2) 
//If the generated number is equal to the guess
     {
       System.out.println("You go it!");
       System.out.println("Play Again? (Y)");
       Scanner scan = new Scanner(System.in);
       char desc  = (char) System.in.read();;
//Make a new scanner and take the "descion" input whether to continue or quit (1 or 0)           
       if(desc == 'Y' || desc=='y'){
       break;
//If the input is not 0 break the loop and play perform actions again           
       }
       else{
         System.out.println("Thanks for playing");
         System.exit(0);
  //If the input is 0 break the loop and continue the program post-loop             

     }  
     }
     else if(num > num2){
     System.out.println("Too Low.");
//If the number generated is greater than the guessed number print "Too Low"        
     }
     else if (num <num2){
     System.out.println("Too High.");
  //If the number generated is less than the guessed number print "Too High"         
     }
   }
}
}
}