Java 猜谜游戏 - 优化和提示

标签 java

我的程序遇到了一个小问题。这是一个简单的 Java 猜谜游戏,会生成一个随机数,然后用户猜测该数字。每次猜测不成功时,用户都会看到“太高”或“太低”消息,直到他们最终找到该数字。找到号码后,提示用户是否继续或退出游戏。

一切正常,除了我只需要在选择“无答案”后停止程序打印数据输入框。我还想要一些关于优化的建议,以及将继续游戏输入(Y 或 y)设为"is"而不是依赖于数字。我已经考虑过将字符串转换为 int 并使用我现在拥有的方法,但我忍不住觉得有一种更简单的方法。

对于造成的困惑和所有的一切,我深表歉意(我是一年级计算机科学学生,理解力平庸),并且我非常感谢您提供的任何建议或提示。

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"         
     }
   }
}
}
}

最佳答案

如果您想以 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 匹配“Y”或“Yes”,它将跳出循环。如果它匹配“N”或“No”,它将返回并退出该方法。

希望这有帮助!

关于Java 猜谜游戏 - 优化和提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33638630/

相关文章:

java - 即使应用程序关闭也显示警报通知

java - Axis 1.4 如何修改soap信封属性?

java - 类加载器加载错误的文件

java - 查询在mysql中有效,但在java中无效

java - Hibernate + MySQL + JPA

java - 如何对 HTTPServlet 进行单元测试?

Scala 中匿名类的 Java 风格 Lambda 表达式

Java Jersey 2.6 : ContainerRequestFilter: Not Inside a request scope

java - 使用新标准 javax.json 将 Pojos 序列化为 JSON

java - h2db 按字段排序