我正在编写一个简单的程序,允许用户输入两个单独的 double 以进行英尺和英寸测量。该程序旨在获取这些值并将其转换为厘米并输出。另外,我要包括两个异常(exception):一个是确保数值是正数而不是负数(我已经完成了这个),另一个是确保输入的输入是 double 值而不是字符串值(我有这个)很难过)。因此,如果用户输入一个输入...例如“Bill”而不是数字,则会显示一条错误消息并要求用户再次重新输入输入值。
看来我最好将用户输入收集为字符串(而不是像我现在这样的 double ),我将其转换为 double 并将它们作为 double 返回到相应的方法: getFootValue() 和 getInchValue( ) -- 但我不太确定。
我应该如何通过自定义异常来实现这一点?我不能简单地利用 InputMismatchException,我需要制作自己的标题 NonDigitNumberException()。
这是我到目前为止所拥有的......
import java.util.Scanner;
public class Converter
{
private double feet;
private double inches;
public Converter(double feet, double inches)
{
this.feet = feet;
this.inches = inches;
}
public double getFootValue()
{
return feet;
}
public double getInchValue()
{
return inches;
}
public double convertToCentimeters()
{
double inchTotal;
inchTotal = (getFootValue() * 12) + getInchValue();
return inchTotal * 2.54;
}
public String toString()
{
return ("Your result is: " + convertToCentimeters());
}
}
import java.util.Scanner;
import java.util.InputMismatchException;
public class TestConverter
{
public static void main(String[] args)
{
/* Create new scanner for user input */
Scanner keyboard = new Scanner(System.in);
do
{
try
{
/* Get the feet value */
System.out.print("Enter the foot value: ");
double feet = keyboard.nextDouble();
if (feet < 0) throw new NegativeNumberException();
/* Get the inches value */
System.out.print("Enter the inch value: ");
double inches = keyboard.nextDouble();
if (inches < 0) throw new NegativeNumberException();
else
{
Converter conversion = new Converter(feet, inches);
/* Print the converted result */
System.out.println(conversion);
break;
}
} catch(InputMismatchException ignore){}
catch(NegativeNumberException error)
{
System.out.println("A negative-numeric value was entered, please enter only positive-numeric values...");
}
}while(true);
/* Close the keyboard */
keyboard.close();
}
}
class NegativeNumberException extends Exception
{
public NegativeNumberException()
{
super();
}
public NegativeNumberException(String errorMessage)
{
super(errorMessage);
}
}
感谢您的帮助!
最佳答案
你把事情搞得太复杂了。您只需使用 Scanner.hasNextDouble()
方法即可。
示例:
假设此代码位于您的主方法内。
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter value");
double myValue = 0;
if(scanner.hasNextDouble()){
myValue = scanner.nextDouble();
}else{
System.out.println("Wrong value entered");
}
}
}
然后您可以继续将 myValue
与您的 Converter
类一起使用。
更新
看来您必须根据您在评论中告诉我的内容创建自己的异常
类
。因此,我决定为您实现这一点,希望您能够从这里继续。
自定义异常类
public class NonDigitNumberException extends InputMismatchException {
public NonDigitNumberException(String message){ // you can pass in your own message
super(message);
}
public NonDigitNumberException(){ // or use the default message
super("input is not a digit");
}
}
负数异常类
public class NegativeNumberException extends IllegalArgumentException {
public NegativeNumberException(String message){ // you can pass in your own message
super(message);
}
public NegativeNumberException(){ // or use the default message
super("negative number is not valid");
}
}
validator 方法
public static double inputValidator(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter a value"); // prompt user for input
String getData = scanner.next(); // get input
if(getData.length() >= 1){
if(!Character.isDigit(getData.charAt(0)) && getData.charAt(0) != '-') throw new NonDigitNumberException();
}
for (int i = 1; i < getData.length(); i++) {
if(!Character.isDigit(getData.charAt(i))) throw new NonDigitNumberException();
}
return Double.parseDouble(getData); // at this point the input data is correct
}
负数 validator
public static boolean isNegative(double value){
if(value < 0) throw new NegativeNumberException();
return false;
}
主要方法
public static void main(String[] args) {
try {
double myValue = inputValidator();
System.out.println(isNegative(myValue)); // check if number is negative
}catch (NegativeNumberException e){
e.printStackTrace();
}
catch (NonDigitNumberException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
关于Java:使用 Try/Catch Exception 检查用户输入是否为 Double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42519055/