mercredi 25 avril 2018

Guessing game outputting hot/cold with regards to how close the guess is too the answer

The goal of the game is to guess a number between 1 and 50. If the user enters a guess that is within 10 of the actual answer, the program tells them that they are HOT. If they enter a number that is within 3 of the answer, it tells them that they are VERY HOT. If they input anything else, they are COLD. Here is an example of the program running (the correct answer is 17):

Guess a number between 1 and 50: 40 You’re COLD, guess again: 10 You’re HOT, guess again: 20 You’re VERY HOT, guess again: 17 Correct, the number was 17.

This is expanding on a previous code that had guesses between 1-10 and simply had a "you're correct or a "you're incorrect, try again" code. Does anyone know where I should start to modify this code:

 import java.util.Scanner;
 import java.util.Random;
 public class Problem2 {

public static void main(String[] args) {

    // TODO, add your application code
    Random rand = new Random();
    Scanner keyboard = new Scanner(System.in);
    int numberToGuess = rand.nextInt(10);
    System.out.println("Guess a number between 1 and 10: ");
    int guess = keyboard.nextInt();

    if (guess==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else
        System.out.println("Incorrect, guess again: ");
        int guess2 = keyboard.nextInt();
    if (guess2==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");
        int guess3 = keyboard.nextInt();
    if (guess3==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");
        int guess4 = keyboard.nextInt();
    if (guess4==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");
        int guess5 = keyboard.nextInt();
    if (guess5==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");
        int guess6 = keyboard.nextInt();
    if (guess6==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");
        int guess7 = keyboard.nextInt();
    if (guess7==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");
        int guess8 = keyboard.nextInt();
    if (guess8==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");
        int guess9 = keyboard.nextInt();
    if (guess9==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");
        int guess10 = keyboard.nextInt();
    if (guess10==numberToGuess)
        System.out.println("Correct, the number was " + numberToGuess);
    else 
        System.out.println("Incorrect, guess again: ");



}

to work with the new problem?




Aucun commentaire:

Enregistrer un commentaire