I'm new to this site and programming in general. I've created a small loop to try and improve my understanding of the basics. What I'd like to do is at the end with the user inputs 'Y' or 'N', either be able to disable every key bar those two, ensuring I only get one of those two answers. If that isn't feasible, I would create an error that then loops back to the question, thus not stopping the program unintentionally.
Many thanks
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String answer = "y";
do {
System.out.println("Enter your number:");
double originalNumber = scan.nextDouble();
System.out.println("Calculating sum...");
double modifyingNumber = (int) (Math.random() * ((100 - 0.5 + 10) + 3));
double result = (originalNumber * modifyingNumber + 10d);
if (result % 2 == 0) {
System.out.println("Even- " + result);
} else {
System.out.println("Odd- " + result);
}
System.out.println("Random number was " + modifyingNumber);
System.out.println("Would you like to try again? Y/N");
answer = scan.next();
} while (answer.equalsIgnoreCase("y"));
}
}
Add another do while loop
do {
System.out.println("Enter your number:");
double originalNumber = scan.nextDouble();
System.out.println("Calculating sum...");
double modifyingNumber = (int) (Math.random() * ((100 - 0.5 + 10) + 3));
double result = (originalNumber * modifyingNumber + 10d);
if (result % 2 == 0) {
System.out.println("Even- " + result);
} else {
System.out.println("Odd- " + result);
}
System.out.println("Random number was " + modifyingNumber);
do {
System.out.println("Would you like to try again? Y/N");
answer = scan.next();
} while(!answer.equalsIgnoreCase("y") && !answer.equalsIgnoreCase("n"));
} while (answer.equalsIgnoreCase("y"));