Answer the question
In order to leave comments, you need to log in
How to add to the program if the user pressed the wrong character and send it to its original position?
It is necessary to add to the super small code so that if the input is incorrect, instead of numbers, another character will issue a warning and return back to the original one. And also when typing D or N, if some other character is typed, you also need to have a warning come out and be asked to try again.
Here is the code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int gallons;
double liters = 3.7854;
Scanner scanner = new Scanner(System.in);
System.out.println("Введите количество галлонов, и узнаете сколько будет в литрах");
gallons = scanner.nextInt();
System.out.println("В " + gallons + " галонах " + gallons * liters + " литров");
System.out.println();
System.out.println("Введите букву D, если хотите начать заново, если не хотите введите N");
while (true) {
String num = scanner.nextLine();
if ("D".equals(num)) {
System.out.println();
main(args);
}
if ("N".equals(num)) {
System.out.println("Досвиданья");
break;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Can regular expressions be used?
PS: Remove the JavaScript tag, it's out of place here.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question