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

标签 java string validation loops methods

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

我的具体问题是了解如何让程序在新游戏开始时重新开始尝试次数,因为它将继续计算上次停止的位置,以及验证选择字符串。我们需要用不同的方法进行验证。我已经尝试了很多技术,但我无法验证任何内容,并且学习障碍也无济于事......

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

    else if (猜测== secret 值+10){ System.out.println("太高了!再猜一下。\n"); 继续;

    }

对此:

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

    }

您已经差不多知道是/否了...只需要进行一些调整。

  • 更改此:

    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() 方法。

  • 更改此:

    public static boolean getChoice(扫描仪 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;
        }
    }
    

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

    关于java - java猜谜游戏的选择和计数验证帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33066591/

    相关文章:

    java - 从 Java 中的另一个类调用重绘?

    java - 如何在不处理异常java的情况下防止被零除

    java - java 读取并添加到文本文件

    jquery - 根据数组值验证表单

    php - 我需要在 header ("Location: http://localhost/..."之后使用 exit 吗)?

    jQuery 验证插件 - 仅显示 errorContainer 处的第一个错误

    java - HashSet 允许重复

    java - 可能不正确调用 ArrayList 索引?

    c# - 用 0 到 9 的数字填充字符串并开始新的直到长度为 50

    JavaScript 原语