【问题标题】:Choice and count validation help on java guessing gamejava猜谜游戏的选择和计数验证帮助
【发布时间】:2016-01-09 01:47:02
【问题描述】:

我在这里看到了很多这样的问题,但内容要么太简单,要么比我想要做的更复杂。

我的具体问题是了解如何让程序在新游戏开始时重新开始尝试次数,因为它将继续从停止的地方计算,以及验证 Yes选择String。我们需要在不同的方法中进行验证。我尝试了很多技术,但我无法验证任何东西,学习障碍也无济于事......

import java.util.Scanner;
import java.util.Random;
import java.lang.Math; 

public class Gamerandom {  
    public static void main( String[] args ) {
        System.out.println("Welcome to the Guess the Number Game ");
        System.out.println();
        System.out.println("I am thinking of a number between 1 and 100.");
        System.out.println("Try to guess it.");
        System.out.println();

        Scanner sc = new Scanner(System.in);

        int secretValue;
        secretValue = (int) (Math.random() * 99 + 1);
        int guess; 
        int tries = 0;
        String choice;
        boolean playing = true;

        while(playing) {  

        System.out.print("Enter number: ");
        guess = sc.nextInt();
        tries++;

        if (guess == secretValue) {
            if (tries <= 3) {
                System.out.println("You got it in " + tries + " tries.");
                System.out.println("Great Work! You are a mathematical wizard!");
            } else if (tries > 3 && tries <= 7) {
                System.out.println("You got it in " + tries + " tries.");
                System.out.println("Not too bad! You've got some potential. ");
            } else  {
                System.out.println("You got it in " + tries + " tries.");
                System.out.println("What took you so long? Maybe you should take some lessons.");
            }
         } else if (guess < secretValue) {
            System.out.println("Too low! Guess again. \n");
            continue;
        } else if (guess == secretValue +10) {
            System.out.println("Way too high! Guess again. \n");
            continue;
        } else if (guess > secretValue) {
            System.out.print("Too high! Guess again. ");
            continue;
        } 

        System.out.println("Try again? y/n");
        choice = sc.next();      
        }                   
    }  

    public static boolean getChoice(Scanner sc) {
        String choice = "y";
        boolean getChoice = true;

        while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
            System.out.println("Error! Entry must be 'y' or 'n'. Try again");
            System.out.println("Try again? (y/n)");
            choice = sc.nextLine();
        }

        if (choice.equalsIgnoreCase("n")) {
            System.out.println("Bye - Come back again soon!");
            return true;
        } else {
            System.out.println("I am thinking of a number between 1 and 100.");
            System.out.println("Try to guess it. ");

        }
        return false;
        }  
    }

【问题讨论】:

    标签: java string validation loops methods


    【解决方案1】:
    1. 改变这个:

      else if (guess == secretValue +10){ System.out.println("太高了!再猜一次。\n"); 继续;

      }

    到这里:

     else if (guess >= secretValue +10){
            System.out.println("Way too high! Guess again. \n");
            continue;
    
        }
    

    你的 Y/N 就快到了……只需要进行一些调整。

    1. 改变这个:

      System.out.println("再试一次?y/n"); 选择 = sc.next();
      }

    到这里:

    System.out.println("Try again? y/n");
            choice = sc.next();
    
            // This will set the boolean to continue the loop
            // or finish the program
            if (playing = getChoice(choice)){
                tries = 0;
            }
    

    最后你需要改变你的 getChoice() 方法。

    1. 改变这个:

      public static boolean getChoice(Scanner sc) { 字符串选择 = "y"; boolean getChoice = true;

      while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
          System.out.println("Error! Entry must be 'y' or 'n'. Try again");
          System.out.println("Try again? (y/n)");
          choice = sc.nextLine();
      }
      
      if (choice.equalsIgnoreCase("n")) {
          System.out.println("Bye - Come back again soon!");
          return true;
      } else {
          System.out.println("I am thinking of a number between 1 and 100.");
          System.out.println("Try to guess it. ");
      
      }
      return false;
      }  
      

    对此:

    // Pass choice in as the parameter instead of a Scanner object
    public static boolean getChoice(String choice) {
    
        // Create a new scanner object
        Scanner sc = new Scanner(System.in);
    
        while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
            System.out.println("Error! Entry must be 'y' or 'n'. Try again");
            System.out.println("Try again? (y/n)");
            choice = sc.nextLine();
        }
        if (choice.equalsIgnoreCase("n")) {
            System.out.println("Bye - Come back again soon!");
            // This will close your loop and the program will end
            return false;
        } else {
            System.out.println("I am thinking of a number between 1 and 100.");
            System.out.println("Try to guess it. ");
            // This will cause your loop to continue
            return true;
        }
    }
    

    现在您只需要添加一些异常处理,您就应该开始工作了。祝你好运!

    【讨论】:

    • 太棒了!是或否验证方法还有什么?
    • 这很有帮助:)。还有一个我直到现在才注意到的问题..当新游戏开始时,尝试计数不会重新开始。我不想向任何人施压以获得更多建议,哈哈,所以我会看看它会把我带到哪里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多