0
package w3school;

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

public class nyttprogram {

   static void indata() {
      {
         Scanner determinedNumber = new Scanner(System.in);
         int user, computer, number, user2;
         System.out.println("Input a number from 0-10");
         user = determinedNumber.nextInt();

         Random random = new Random();
         int randomInt = random.nextInt(10);

         if (user == randomInt) {
            System.out.println("You guessed the correct number!");
         } else {
            System.out.println("You guessed the wrong number");
            System.out.println("The correct number was: " + randomInt);
         }
         System.out.println("Input 1 if you want to try again: ");
      }
   }

   public static void main(String[] args) {
      indata();
   }
}

How do I make the class start over when user input 1 OR if the Class can start over if User inputs wrong number from the start, many thanks

Goion
  • 923
  • 8
  • 16
  • 2
    Put the code in some loop `do-while` perhaps. – Goion Sep 28 '19 at 19:01
  • 1
    Possible duplicate of [Validating input using java.util.Scanner](https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom Sep 28 '19 at 19:11

1 Answers1

-1

How do I make the class start over when user input 1 OR if the Class can start over if User inputs wrong number from the start, many thanks

  • The "start over" logic based on some conditions is usually implemented with while and do/while loops.
  • First let's extract those conditions. We want to iterate again (start over) if:
    • The user's guess is wrong.
    • The user's guess is correct, but they input a number different than 1 when asked if they want to continue.
  • Since we want to run the program at least once, the natural approach would be with a do/while. This will run one iteration, then check against the conditions wanted.

Here's what it looks like:

private static void inData() {
  Scanner userInputScanner = new Scanner(System.in);
  Random random = new Random();

  // Declare the stop/continue condition
  boolean isLoopContinue;

  do {
    // Generate a random number
    int expectedNumber = random.nextInt(10);

    // Ask the user to guess a number
    System.out.println("Input a number from 0-10");
    int givenNumber = userInputScanner.nextInt();

    if (givenNumber == expectedNumber) {

      // Correct answer, check if the user wants to continue
      System.out.println("You guessed the correct number!");
      System.out.println("\nInput 1 if you want to try again: ");

      // If they input "1", then we continue. Else we stop
      isLoopContinue = userInputScanner.nextInt() == 1;
    } else {

      // Wrong answer, loop again
      System.out.println("You guessed the wrong number");
      System.out.println("The correct number was: " + expectedNumber);
      isLoopContinue = true;
    }

  } while (isLoopContinue);
}
molamk
  • 3,568
  • 1
  • 9
  • 21