I have created a simple project for my self, and now i am trying to integrate a code that will make the program restart when i enter "restart" in the console. To do that I have created a second method in my program that contains exactly the same code as my main method but now i want to appoint the second method which i don't know how to do. All help is appreciated! here is my code so far:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("guess the number between 1 and 9");
int random = (int)(Math.random() * 9 + 1);
int value = 0;
do{
value = scanner.nextInt();
if (value != random){
System.out.println("Try again");
}
}
while(value != random);
System.out.println("You guessed the number");
if(value == random){
System.out.println("Would you like to restart?");
String reset = scanner.nextLine();
if (reset.equals("restart")){
/*
*I need some code here
*/
}
}
}
public static void restart(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("guess the number between 1 and 9");
int random = (int)(Math.random() * 9 + 1);
int value = 0;
do{
value = scanner.nextInt();
if (value != random){
System.out.println("Try again");
}
}
while(value != random);
System.out.println("You guessed the number");
if(value == random){
System.out.println("Would you like to restart?");
String reset = scanner.nextLine();
if (reset.equals("restart")){
}
}
}
do { ... } while (reset.equals("restart");