简体   繁体   中英

Random number (1-100) picking loop program | Understanding methods

what I am looking to do is create a program that will randomly pick an integer between 1 and 100. Then, ask the user to guess it. Loop until they do, and after each incorrect guess tell them if they are too high or too low. I want to use two different methods to validate their input. One to test whether it is a valid int, the other to test the range (1-100). This second will require another parameter for the high range value.

The problems I am having:

1. I do not understand why I have to enter a number multiple times before my while (guess != a) { is triggered.

Example from console :

I am thinking of a number from 1 to 100 ... guess what it is ?6
I am thinking of a number from 1 to 100 ... guess what it is ?6
I am thinking of a number from 1 to 100 ... guess what it is ?6
6
higher!

2. how could I use my check methods and have them pertain to my while guess loop?

Example from console again:

`I am thinking of a number from 1 to 100 ... guess what it is? 10001
I am thinking of a number from 1 to 100 ... guess what it is? 10001
Error! Must be less than 100
I am thinking of a number from 1 to 100 ... guess what it is? 10001
100
lower!
10001
lower!`

{What I have full written currently}

package labbs;

import java.util.Scanner;

public class Lab12 {

    public static double getDoubleGreaterThan(double low, Scanner input, String prompt) {
        double num;
        num = getDouble(input,prompt);
            if(num <= low)
            System.out.println("Error! Must be greater than 1");
            num = getDouble(input,prompt);    
                if (num > 100)
                System.out.println("Error! Must be less than 100");
                num = getDouble(input,prompt);


        return num;
    }

        public static double getDouble(Scanner input, String prompt) {
            boolean OK;
            double val=0;
            do {
                System.out.print(prompt);
                OK = true;
                try {
                    val = input.nextDouble();
                }
                catch(Exception e) {
                    OK = false;
                    System.out.println("Error! Invalid input. Must be a double value");
                    input.next();
                }
            }while(! OK);

            return val;

    }

    public static void main(String args[]) {

        Scanner keyboard = new Scanner(System.in);

        double output, letscheck;
        int count=0, guess=0;
        int a=1 + (int) (Math.random() * 99);

        letscheck = getDoubleGreaterThan(-0.9, keyboard,"I am thinking of a number from 1 to 100"
                + " ... guess what it is ?");


        while (guess != a) {
            guess = keyboard.nextInt();
            count++;
            if (guess > a) {
                System.out.println("lower!");
            } else if (guess < a) {
                System.out.println("higher!");
            }
        }
        System.out.println("Congratulations. You guessed the number with "
                + count + " tries!");

    }

}

1. Query: You have just missed bracket in getDoubleGreaterThan() method as after if statement block always working not on the basis of input so change your code like below:

public static double getDoubleGreaterThan(double low, Scanner input, String prompt) {
            double num;
            num = getDouble(input,prompt);
                if(num <= low){
                System.out.println("Error! Must be greater than 1");
                num = getDouble(input,prompt);
                }
                    if (num > 100){
                    System.out.println("Error! Must be less than 100");
                    num = getDouble(input,prompt);
                    }


            return num;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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