如果我输入字母,下面的程序不应该崩溃(错误粘贴在底部);相反,它应该要求用户输入一个有效的数字。
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;
int principal = 0;
float monthlyInterest = 0;
int numberOfPayments = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number between 1,000 and 1,000,000.");
while(true) {
System.out.print("Principal: ");
principal = scanner.nextInt();
if (principal >= 1000 && principal <= 1000_000)
break;
System.out.println("Enter a valid input");
}
while(true) {
System.out.print("Annual Interest Rate: ");
float annualInterest = scanner.nextFloat();
if (annualInterest > 0 && annualInterest <= 15) {
monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a value between 1 and 15");
}
while(true) {
System.out.print("Period (Years): ");
byte years = scanner.nextByte();
if (years > 0 && years <= 30) {
numberOfPayments = years * MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a value between 1 and 30");
}
double mortgage = principal * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.println("Mortgage: " + mortgageFormatted);
}
}
**输出#1//当用户输入时,句号为abc
Enter a number between 1,000 and 1,000,000.
Principal: 600
Enter a valid input
Principal: 2000
Annual Interest Rate: 0
Enter a value between 1 and 15
Annual Interest Rate: 3.5
Period (Years): abc
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextByte(Scanner.java:2002)
at java.base/java.util.Scanner.nextByte(Scanner.java:1956)
at com.company.Main.main(Main.java:39)
Process finished with exit code 1
输出#2//当年份数据类型为字节,但用户输入300,而不是崩溃时,程序应要求用户输入有效数字
Enter a number between 1,000 and 1,000,000.
Principal: 1000000
Annual Interest Rate: 3.9
Period (Years): 300
Exception in thread "main" java.util.InputMismatchException: Value out of range. Value:"300"
Radix:10
at java.base/java.util.Scanner.nextByte(Scanner.java:2008)
at java.base/java.util.Scanner.nextByte(Scanner.java:1956)
at com.company.Main.main(Main.java:39)
Process finished with exit code 1
您应该使用try Catch,如下所示:
while(true) {
System.out.print("Period (Years): ");
try{
byte years = scanner.nextByte();
if (years > 0 && years <= 30) {
numberOfPayments = years * MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a value between 1 and 30");
catch (Exception e){
System.out.println("Value must be numeric");
}
}
请解释为什么输出 是。
尝试执行以下代码 JVM崩溃,错误日志如下
我想深入了解Jmeter输出。 > 我对吞吐量率的概念感到困惑。这是否意味着服务器在给定负载下只能处理48.1个请求/分钟,还是意味着其他什么。总吞吐量速率和单个请求显示的吞吐量速率之间的差异是什么。在我的情况下,发送了8个请求,单个请求显示吞吐量为6.1/min。请解释一下。 我需要建议服务器端的任何更改/解释jmeter报告,请建议我如何解释需要做什么。 总的总结报告如下: 总用户: 100上
错误: log4j:WARN找不到记录器(org.hibernate.cfg.annotations.Version)的appender。log4j:警告请正确初始化log4j系统。线程“main”组织中出现异常。冬眠例外GenericJDBCException:无法在组织上打开连接。冬眠例外SQLStateConverter。在org上处理非特定异常(sqlstatecoverter.java:
我开始学习android,我的第一个练习是计算器。在做适当的计算器之前,我计划创建一个非常基本的应用程序,用户只需通过EditText输入2个数字,用按钮选择算术运算,并在按下“igual”按钮时在TextView中获得结果。 我的想法是声明2个字符串(sinput1 在我开始编写操作的工作方式之前,所有布局看起来都运行良好,但是每次我运行应用程序(使用手机或模拟器)时,当我按EditText输入
任务是: 编写一个程序,为在XYZ书店购买任意两本书的会员提供20%的折扣。(提示:将常量变量用于20%的折扣。) 我已经做了编码,但是不能提示书名,然后显示折扣价,请在下面看到我的编码,根据需要修改。