0

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.

1 Answers1

0

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.

PandaConda
  • 3,396
  • 2
  • 20
  • 22
  • It somewhat works. It glitches out after the second time, though and won't let me pick a new maxValue. It gets worse when it suddenly finishes the game for no reason. –  Jun 16 '14 at 10:34
  • Missed that. You need to reset the tryCount variable to 0 each time. I'll update my answer. – PandaConda Jun 16 '14 at 10:36
  • I updated the code. I also added another do-while loop at the end to check that the input is either 0 or 1 when asking the player if he or she wants to play again. – PandaConda Jun 16 '14 at 10:42
  • Ok, so I've implemented the code. It's almost right, except for the fact that the maxValue remains the same, which makes both "Enter a max value: " and "Guess a number from 1 to (maxValue): " at the same time, which means the user cannot choose a new maxValue. Should I just reset the maxValue as you did the tryCount? –  Jun 16 '14 at 10:59
  • Actually, doesn't matter. I implemented the code to reset the maxValue and it worked. Thank you very much for your help. I'm finding the activity of coding to be very rewarding when you can talk to people to make something happen. –  Jun 16 '14 at 11:01