gpt4 book ai didi

java - 如何修复我的 "java.util.InputMismatchException"错误?

转载 作者:行者123 更新时间:2023-12-02 11:02:53 25 4
gpt4 key购买 nike

我需要做的就是再次循环,以便用户可以继续使用该程序。让我知道是否有任何我可以阅读的引用资料,以帮助我更多地了解这个问题。提前致谢。

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)

最佳答案

import java.util.Scanner;

public class Module3Assignment1 {
// public static variables are discouraged...
private static char letterChosen; //char takes less memory
private static char useAgain = 'Y'; //just use the answer to loop...
private static double radius, area;
private static String answer;
private static Scanner scanner = new Scanner(System.in);


//you might want to clear the screen after the user gave an answer to another round object
private static void clearScreen(){
for(int i =0;i<50;i++){System.out.print("\n");}
}

public void input(){

// prompts user for input
System.out.print("Enter C for circle or S for sphere: ");
letterChosen = scanner.next().charAt(0);
System.out.print("Thank you. What is the radius of the circle (in inches): ");
radius = scanner.nextDouble();
this.answer= answer(letterChosen);

}

public double areaCircle(double radius){

// calculates the area of a circle
area = Math.PI * Math.pow(radius, 2);
return area;

}

public double areaSphere(double radius){

// calculates the area of a sphere
area = (4/3) * (Math.PI * Math.pow(radius, 3));
return area;

}

public String answer(char letterChosen){

//local variables
String answer = "";
if(letterChosen=='c'||letterChosen=='C'){
answer = String.format("%s %f %s %.3f %s %n", "The volume of a circle with a radius of", radius, "inches is:", areaCircle(radius), "inches");
}else{
answer = String.format("%s %f %s %.3f %s %n", "The volume of a sphere with a radius of", radius, "inches is:", areaSphere(radius), "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;
}

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("");
Module3Assignment1 ass1 = new Module3Assignment1();

// loops while the user wants to calculate a round object
while (useAgain == 'Y'||useAgain=='y'){

ass1.input();
System.out.print(answer);
System.out.println("Do you want to calculate another round object (Y/N)? ");
useAgain = scanner.next().charAt(0);
System.out.println(useAgain);
clearScreen();
}

// ending message/goodbye
System.out.println(goodbye());
scanner.close();

}

}

我改变的一些事情:
  • 我用了字符 而不是 字符串 . 字符串 占用更多内存字符 .
  • 添加了 clearScreen() 方法,当您使用控制台时“清除”屏幕。
  • 我在 areaSphere 和 areaCircle 方法中添加了一个参数 radius。这使得方法可重用。
  • 我将所有公共(public)静态变量更改为私有(private)静态。使用 公共(public)静态变量 强烈反对 .您可以阅读 this找出原因。
  • 为了防止公共(public)静态变量,我创建了一个 Module3Assignment1 的实例,而不是将所有内容都放在静态中。
  • 更改了方法名称的大小写。请遵循驼峰式,即方法首字母小写,其他单词首字母大写(如 input()、areaSphere() )

  • 关于比较字符串的评论:

    == compares REFERENCES TO THE OBJECT , NOT VALUES



    使用 .equals() .equalsIgnoreCase() 如果要比较两个字符串的值。这是一个示例语法:
    if(string1.equals(string2)){
    //do something
    }

    关于java - 如何修复我的 "java.util.InputMismatchException"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31289641/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com