选择和计数验证帮助java猜谜游戏

时间:2015-10-11 15:16:10

标签: java string validation loops methods

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

我的具体问题是了解如何在新游戏开始时让程序重新开始,因为它会继续计算停止的位置,以及验证选择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;
        }  
    }

1 个答案:

答案 0 :(得分:2)

  1. 更改此内容:

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

    }

  2. 对此:

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

    你的Y / N几乎就在那里......只需要进行一些调整。

    1. 更改此内容:

      System.out.println(“再试一次?y / n”);     choice = sc.next();
          }

    2. 对此:

      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){     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;
        }  
        
      2. 对此:

        // 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;
            }
        }
        

        现在你只需要添加一些异常处理,你应该开展业务。祝你好运!