简体   繁体   中英

How can I code a console-based Java game to replay when the user types in “1”?

First of all, I'd like to say that I am REALLY new to all of this... I've tried learning as much as I can, so apologies if any of my code seems ridiculous or all-over-the-place, but I needed somewhere to start. (By the way, credit to the very base of this code goes to CrossCoastGaming: http://tinyurl.com/kktyq4e ).

Now to the matter at hand. I have improved (for lack of a better word) on the coding that the man in the video shows, by adding several different phrases, making use of variables and adding a try counter. Here is my code:

import java.util.Random;
import java.util.Scanner;

public class Main {

public static int number, guess, tryCount, replay;
public static int maxValue =1;
public static Scanner scan;
public static Random rand;

public static void main(String args[]) {
    scan = new Scanner(System.in);
    rand = new Random();
    System.out.print("Enter a maximum number: ");
    while(maxValue < 2)
        maxValue = scan.nextInt();
    number = rand.nextInt(maxValue);
    System.out.print("Guess a number from 1 to " + maxValue + ": ");
    while (guess != number) {
        guess = scan.nextInt();
        tryCount++;
        if (guess < 1) {
            System.out.print("Guess is not positive. Try again: ");
        }else if (guess < number) {
            System.out.print("Too low! Try again: ");
        }
        if (guess > maxValue) {
            System.out.println("Guess is higher than " + maxValue + ". Try again: ");
        }else if (guess > number) {
            System.out.print("Too high! Try again: ");
        }
    }
    if (tryCount == 1) {
        System.out.println("Nailed it! It only took you 1 try!");
    }
    else {
        System.out.println("Nailed it! It took you " + tryCount + " tries.");
    }
    System.out.println("Type 0 to play again. Type 1 to quit.");
    if (replay == 1) {
        replay = scan.nextInt();

    }
}

}

Ok, so hopefully that gives anyone who knows what they're doing an idea of my goal. Now, as you can see by this line:

if (replay == 1) {
        replay = scan.nextInt();

    }

I would like to write a way so people can replay the game without having to reboot the file. I already have an idea of what I kind of would like to do, but I've searched everywhere and can't seem to find out what to continue with after this point. I'm sure that I'm missing something. Any help would be greatly appreciated.

You can use a do-while loop to achieve this:

import java.util.Random;
import java.util.Scanner;

public class Main {

    public static int number, guess, tryCount, replay;
    public static int maxValue = 1;
    public static Scanner scan;
    public static Random rand;

    public static void main(String args[]) {
        scan = new Scanner(System.in);
        rand = new Random();
        do { // start of do-while loop
            tryCount = 0; // reset tryCount
            System.out.print("Enter a maximum number: ");
            while(maxValue < 2) {
                maxValue = scan.nextInt();
            }
            number = rand.nextInt(maxValue);
            System.out.print("Guess a number from 1 to " + maxValue + ": ");
            while (guess != number) {
                guess = scan.nextInt();
                tryCount++;
                if (guess < 1) {
                    System.out.print("Guess is not positive. Try again: ");
                } else if (guess < number) {
                    System.out.print("Too low! Try again: ");
                }
                if (guess > maxValue) {
                    System.out.println("Guess is higher than " + maxValue + ". Try again: ");
                } else if (guess > number) {
                    System.out.print("Too high! Try again: ");
                }
            }
            if (tryCount == 1) {
                System.out.println("Nailed it! It only took you 1 try!");
            } else {
                System.out.println("Nailed it! It took you " + tryCount + " tries.");
            }

            do { // check the user's input
                System.out.println("Type 0 to play again. Type 1 to quit.");
                replay = scan.nextInt();
                if (replay != 0 && replay != 1) {
                    System.out.println("Input not recognized.");
                }
            } while (replay != 0 && replay != 1);
        } while (replay == 0); // end of do-while loop
    }
}

do-while loops are always executed at least once. The condition is checked at the end of the loop.

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