Java InputMismatchException 错误
Java InputMismatchException Error
我接到了一项任务,要将英尺和英寸转换为厘米。我已经了解了其中的大部分内容,并且转换确实有效。我还想在输入字符串时包含 A negative number...
或 A non-digit...
的语句,例如,或负数,以显示所述消息。
我遇到的问题是,当我输入字符串或负数时,例如,当我输入 -9
时,我得到 testProgram.NegativeNumberException
的输出。而testProgram.NonDigitNumberException
,当我输入joe
,例如
我认为 catch
有问题,但不确定它不会点击的确切位置。
package testProgram;
import java.util.InputMismatchException;
import java.util.Scanner;
public class conversion{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double cm = -1;
while(cm == -1){
cm = convertToCentimeters(scan);
if(cm!=-1){
System.out.println("Your result = " +cm);
}
else{
System.out.println("Please enter the values again.");
}
scan.nextLine();
}
}
public static double convertToCentimeters(Scanner scan){
double centimeters = -1;
try{
double foot = getFootValue(scan);
double inch = getInchValue(scan);
double totalInches = foot * 12 + inch;
centimeters = totalInches * 2.54;
}catch(NegativeNumberException e1){
System.out.println(e1);
}
catch(NonDigitNumberException e2){
System.out.println(e2);
}
return centimeters;
}
public static double getFootValue(Scanner scan) throws NegativeNumberException, NonDigitNumberException{
try{
System.out.println("Enter the foot value: ");
double foot = scan.nextDouble();
if(foot <= 0){
throw new NegativeNumberException ("A negative foot value has been entered.");
}
return foot;
}
catch(InputMismatchException e){
throw new NonDigitNumberException ("A non-digit foot value has been entered.");
}
}
public static double getInchValue(Scanner scan)throws NegativeNumberException, NonDigitNumberException{
try{
System.out.println("Enter the inch value: ");
double inch = scan.nextDouble();
if(inch <= 0){
throw new NegativeNumberException ("A negative inch value has been entered.");
}
return inch;
}
catch(InputMismatchException e){
throw new NonDigitNumberException ("A non-digit inch value has been entered.");
}
}
}
替代@Scary 的建议,您可以将构造函数添加到您的自定义异常中,如 -
NegativeNumberException(String str) {
System.out.println(str);
}
这将帮助您在
时打印消息
throw new NegativeNumberException ("A n....");
我接到了一项任务,要将英尺和英寸转换为厘米。我已经了解了其中的大部分内容,并且转换确实有效。我还想在输入字符串时包含 A negative number...
或 A non-digit...
的语句,例如,或负数,以显示所述消息。
我遇到的问题是,当我输入字符串或负数时,例如,当我输入 -9
时,我得到 testProgram.NegativeNumberException
的输出。而testProgram.NonDigitNumberException
,当我输入joe
,例如
我认为 catch
有问题,但不确定它不会点击的确切位置。
package testProgram;
import java.util.InputMismatchException;
import java.util.Scanner;
public class conversion{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double cm = -1;
while(cm == -1){
cm = convertToCentimeters(scan);
if(cm!=-1){
System.out.println("Your result = " +cm);
}
else{
System.out.println("Please enter the values again.");
}
scan.nextLine();
}
}
public static double convertToCentimeters(Scanner scan){
double centimeters = -1;
try{
double foot = getFootValue(scan);
double inch = getInchValue(scan);
double totalInches = foot * 12 + inch;
centimeters = totalInches * 2.54;
}catch(NegativeNumberException e1){
System.out.println(e1);
}
catch(NonDigitNumberException e2){
System.out.println(e2);
}
return centimeters;
}
public static double getFootValue(Scanner scan) throws NegativeNumberException, NonDigitNumberException{
try{
System.out.println("Enter the foot value: ");
double foot = scan.nextDouble();
if(foot <= 0){
throw new NegativeNumberException ("A negative foot value has been entered.");
}
return foot;
}
catch(InputMismatchException e){
throw new NonDigitNumberException ("A non-digit foot value has been entered.");
}
}
public static double getInchValue(Scanner scan)throws NegativeNumberException, NonDigitNumberException{
try{
System.out.println("Enter the inch value: ");
double inch = scan.nextDouble();
if(inch <= 0){
throw new NegativeNumberException ("A negative inch value has been entered.");
}
return inch;
}
catch(InputMismatchException e){
throw new NonDigitNumberException ("A non-digit inch value has been entered.");
}
}
}
替代@Scary 的建议,您可以将构造函数添加到您的自定义异常中,如 -
NegativeNumberException(String str) {
System.out.println(str);
}
这将帮助您在
时打印消息throw new NegativeNumberException ("A n....");