简体   繁体   English

Java猜测游戏的选择和计数验证帮助

[英]Choice and count validation help on java guessing game

I have seen a lot of this question on here, but the content is either too simple or more complex than what I am trying to do. 我在这里看到了很多这个问题,但是内容比我想做的太简单或更复杂。

My specific issues are understanding how to get the program to start over the number of tries when a new game begins as it will continue to count where it left off, as well as validating the Yes or No choice String . 我的具体问题是了解如何让程序从新游戏开始时的尝试次数开始,因为它将继续计算从何处终止的游戏,以及验证YesNo choice String We need to have our validation in separate methods. 我们需要在单独的方法中进行验证。 I've tried so many techniques but I can't get anything to validate and having a learning disability doesn't help... 我已经尝试了很多技术,但是我无法获得任何验证,学习障碍也无济于事...

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. Change this: 更改此:

    else if (guess == secretValue +10){ System.out.println("Way too high! Guess again. \\n"); 否则,如果(猜测== secretValue +10){System.out.println(“方式太高!再次猜测。\\ n”); continue; 继续;

    } }

To this: 对此:

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

    }

You're almost there with your Y/N...just a few tweaks need to be made. 您的Y / N几乎就在那...只需进行一些调整即可。

  1. Change this: 更改此:

    System.out.println("Try again? y/n"); System.out.println(“再试一次?是/否”); choice = sc.next(); 选择= sc.next();
    } }

To this: 对此:

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

And finally you need to change your getChoice() method. 最后,您需要更改getChoice()方法。

  1. Change this: 更改此:

    public static boolean getChoice(Scanner sc) { String choice = "y"; public static boolean getChoice(Scanner sc){字符串选择=“ y”; boolean getChoice = true; 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; } 

To this: 对此:

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

Now you just need to add some exception handling and you should be in business. 现在,您只需要添加一些异常处理就可以了。 Best of luck! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM