提问人:daniirp 提问时间:7/13/2021 更新时间:7/13/2021 访问量:96
如何循环我的 java 税计算器,使其在抛出异常后重新启动?
How can I loop my java tax calculator so it restarts after throwing an exception?
问:
我正在上Java课程,我坚持练习。我创建了一个税收计算器,该计算器根据金额所在的范围,对用户输入的金额应用不同的税率。计算器完全按预期工作。我添加了一个 try/catch 语句,因此如果有人输入文本而不是整数,它会抛出 NumberFormatException 并打印“请引入有效的数字行”。我希望我的计算器循环回第一个,并在触发 NumberFormatException 时重新启动程序。我尝试使用布尔值使用 do/while 循环来做到这一点,但我无法让它工作。这是我没有循环的计算器:System.out.println()
public class Application {
public static void main(String[] args) {
System.out.println("Por favor introduce el importe de las ganancias en euros.");
Scanner reader = new Scanner(System.in);
int minimumAmount = 0;
int lowerLimit1 = 5000;
int higherLimit1 = 25000;
int lowerLimit2 = 25000;
int higherLimit2 = 55000;
int lowerLimit3 = 55000;
int higherLimit3 = 200000;
int lowerLimit4 = 200000;
int higherLimit4 = 1000000;
int gains = Integer.parseInt(reader.nextLine());
try {
if (gains > minimumAmount && gains < lowerLimit1) {
System.out.println("Tus ganancias están libres de impuestos.");
} else if (gains >= lowerLimit1 && gains < higherLimit1) {
float taxes = 8F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit1) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit1) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit2 && gains < higherLimit2) {
float taxes = 10F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit2) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit2) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit3 && gains < higherLimit3) {
float taxes = 12F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit3) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit3) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit4 && gains < higherLimit4) {
float taxes = 15F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit4) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit4) * (taxes / 100) + " euros."));
} else if (gains > higherLimit4) {
float taxes = 17F;
System.out.println("Tus ganancias han sido de " + (gains - higherLimit4) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - higherLimit4) * (taxes / 100) + " euros."));
}
} catch (NumberFormatException e) {
System.out.println("Por favor introduce un número válido.");
}
}
这就是我尝试循环的方式:
public class Application {
public static void main(String[] args) {
System.out.println("Por favor introduce el importe de las ganancias en euros.");
Scanner reader = new Scanner(System.in);
int minimumAmount = 0;
int lowerLimit1 = 5000;
int higherLimit1 = 25000;
int lowerLimit2 = 25000;
int higherLimit2 = 55000;
int lowerLimit3 = 55000;
int higherLimit3 = 200000;
int lowerLimit4 = 200000;
int higherLimit4 = 1000000;
int gains = Integer.parseInt(reader.nextLine());
boolean isValid = true;
do {
try {
if (gains > minimumAmount && gains < lowerLimit1) {
System.out.println("Tus ganancias están libres de impuestos.");
} else if (gains >= lowerLimit1 && gains < higherLimit1) {
float taxes = 8F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit1) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit1) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit2 && gains < higherLimit2) {
float taxes = 10F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit2) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit2) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit3 && gains < higherLimit3) {
float taxes = 12F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit3) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit3) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit4 && gains < higherLimit4) {
float taxes = 15F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit4) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit4) * (taxes / 100) + " euros."));
} else if (gains > higherLimit4) {
float taxes = 17F;
System.out.println("Tus ganancias han sido de " + (gains - higherLimit4) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - higherLimit4) * (taxes / 100) + " euros."));
}
} catch (NumberFormatException e) {
System.out.println("Por favor introduce un número válido.");
isValid = false;
}
} while (isValid != false);
}
使用此循环,该部分可以完美运行,但是一旦我输入文本控制台,就会抛出此错误:try
Exception in thread "main" java.lang.NumberFormatException: For input string: "text"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:660)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at Application.main(Application.java:209)
我做错了什么?
答:
0赞
sanjeevRm
7/13/2021
#1
您在 try/catch 之前获得 NumberFormatException,您需要移动到 try/catch 块,如下所示int gains = Integer.parseInt(reader.nextLine());
do {
try {
int gains = Integer.parseInt(reader.nextLine());
if (gains > minimumAmount && gains < lowerLimit1) {
System.out.println("Tus ganancias están libres de impuestos.");
} else if (gains >= lowerLimit1 && gains < higherLimit1) {
float taxes = 8F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit1) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit1) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit2 && gains < higherLimit2) {
float taxes = 10F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit2) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit2) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit3 && gains < higherLimit3) {
float taxes = 12F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit3) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit3) * (taxes / 100)) + " euros.");
} else if (gains >= lowerLimit4 && gains < higherLimit4) {
float taxes = 15F;
System.out.println("Tus ganancias han sido de " + (gains - lowerLimit4) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - lowerLimit4) * (taxes / 100) + " euros."));
} else if (gains > higherLimit4) {
float taxes = 17F;
System.out.println("Tus ganancias han sido de " + (gains - higherLimit4) + " euros. Por tanto, con un " + taxes + "% de impuestos tendrás que pagar " + ((gains - higherLimit4) * (taxes / 100) + " euros."));
}
} catch (NumberFormatException e) {
System.out.println("Por favor introduce un número válido.");
isValid = false;
}
} while (isValid != false);
您可以通过将此增益类型更改为 long 并从扫描仪读取 long 来避免此数字格式异常
do {
...
long gains = reader.nextLong();
...
} while(reader.hasNextLong());
评论
0赞
daniirp
7/13/2021
如果您输入有效数字,这可以让您多次使用计算器,但如果您输入文本或任何其他无效字符,则会执行捕获器,程序以退出代码 0 结束。我希望它在输入无效字符时重新启动程序System.out.println()
0赞
sanjeevRm
7/13/2021
由于更改为 nextLong 后不会出现 NumberFormat 异常,因此您需要将 catch 块中的行设置 isValid 移动,而是检查是否reader.hasNextLong()
0赞
WJS
7/13/2021
#2
你可以这样做。使用并继续处理,直到您获得有效的输入。Scanner.nextInt()
public static void main(String[] args) {
boolean notValid = true;
Scanner reader = new Scanner(System.in);
int minimumAmount = 0;
int lowerLimit1 = 5000;
int higherLimit1 = 25000;
int lowerLimit2 = 25000;
int higherLimit2 = 55000;
int lowerLimit3 = 55000;
int higherLimit3 = 200000;
int lowerLimit4 = 200000;
int higherLimit4 = 1000000;
int gains = 0;
while (notValid) {
try {
System.out.println("Por favor introduce el importe de las ganancias en euros.");
gains = reader.nextInt();
notValid = false;
} catch (InputMismatchException e) {
System.out.println("Por favor introduce un número válido.");
reader.nextLine(); // clear input buffer
}
}
// rest of your code (conditionals and output) goes here.
您也可以将 try/catch 替换为以下内容:
while (notValid) {
System.out.println("Por favor introduce el importe de las ganancias en euros.");
if (reader.hasNextInt()) {
gains = reader.nextInt();
notValid = false;
} else {
System.out.println("Por favor introduce un número válido.");
reader.nextLine(); // clear input buffer
}
}
评论
0赞
WJS
7/13/2021
如果您有任何问题,请告诉我。
评论
int gains = Integer.parseInt(reader.nextLine());
nextLong