Все, что мне нужно, это повторить цикл, чтобы пользователь мог постоянно использовать программу, если она есть. Дайте мне знать, если есть какая-либо ссылка, которую я могу прочитать, чтобы помочь мне понять больше об этой проблеме. Заранее спасибо.Как исправить ошибку «java.util.InputMismatchException»?
import java.util.Scanner;
public class Module3Assignment1 {
// public variables
public static String letterChosen;
public static int loop = 0;
public static double radius, area;
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// tells user what the program is about
System.out.println("Welcome to the Round Object Calculator");
System.out.println("This program will calculate the area of a circle of the colume of a sphere.");
System.out.println("The calculations will be based on the user input radius.");
System.out.println("");
// loops while the user wants to calculate information
while (loop == 0){
Input();
System.out.print(Answer());
System.out.println("Do you want to calculate another round object (Y/N)? ");
String input = scanner.next().toUpperCase();
if (input == "N"){
loop = 1;
}
}
// ending message/goodbye
Goodbye();
scanner.close();
}
private static void Input(){
// prompts user for input
System.out.print("Enter C for circle or S for sphere: ");
letterChosen = scanner.nextLine().toUpperCase();
System.out.print("Thank you. What is the radius of the circle (in inches): ");
radius = scanner.nextDouble();
}
private static double AreaCircle(){
// calculates the area of a circle
area = Math.PI * Math.pow(radius, 2);
return area;
}
private static double AreaSphere(){
// calculates the area of a sphere
area = (4/3) * (Math.PI * Math.pow(radius, 3));
return area;
}
private static String Answer(){
//local variables
String answer;
if(letterChosen == "C"){
// builds a string with the circle answer and sends it back
answer = String.format("%s %f %s %.3f %s %n", "The volume of a circle with a radius of", radius, "inches is:", AreaCircle(), "inches");
return answer;
}else{
// builds a string with the sphere answer and sends it back
answer = String.format("%s %f %s %.3f %s %n", "The volume of a sphere with a radius of", radius, "inches is:", AreaSphere(), "cubic inches");
return answer;
}
}
private static String Goodbye(){
// local variables
String goodbye;
// says and returns the goodbye message
goodbye = String.format("%s", "Thank you for using the Round Object Calculator. Goodbye");
return goodbye;
}
}
Внизу консольный вывод и ошибка я получаю после выполнения
Welcome to the Round Object Calculator
This program will calculate the area of a circle of the colume of a sphere.
The calculations will be based on the user input radius.
Enter C for circle or S for sphere: C
Thank you. What is the radius of the circle (in inches): 12
The volume of a sphere with a radius of 12.000000 inches is: 5428.672 cubic inches
Do you want to calculate another round object (Y/N)?
Y
Enter C for circle or S for sphere: Thank you. What is the radius of the circle (in inches): C
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at Module3Assignment1.Input(Module3Assignment1.java:48)
at Module3Assignment1.main(Module3Assignment1.java:24)
Читать http://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html – Sneh
Что ** ** точно ваша проблема? Что происходит? Чего вы хотите сделать? Можете ли вы привести пример неудачи? – TDG
ah sorry забыл вставить это. Во втором цикле после первого утверждения, которое у меня есть, я получаю это .... Исключение в потоке «main» java.util.InputMismatchException \t at java.util.Scanner.throwFor (Unknown Source) \t на java.util.Scanner.next (Unknown Source) \t в java.util.Scanner.nextDouble (Unknown Source) \t в Module3Assignment1.Input (Module3Assignment1.java:47) \t в Module3Assignment1 .main (Module3Assignment1.java:25) – Joe