Java 我的试抓块有问题吗?
所以我们必须做一个抵押计算项目,我们必须要求用户再次计算,现在我们必须让它在每次用户输入任何输入的字符串值时打印出一条错误消息。我认为我做得对,但每次我运行它时都会发生一些奇怪的事情,我不知道为什么,我知道Try-Catch块出了问题 以下是我的产出: 正如你看到的,我第三次运行这个程序时,我输入了一个“2”作为第二个输入,它仍然进行计算。然后,第三次尝试时,我输入了一个负数,然后输入了一个“2”,一切都按照我想要的方式进行。然后,上次我运行它的时候,我把一个正数作为第一个输入,它仍然进行计算,你们看到有什么东西可能在这样做吗?另外,我想我可能使用了错误的异常,我不确定它是什么意思,我只是猜测。我是否应该使用用户NumberFormatException,并且在nfe下还有一行表示该值未被使用 这是我的密码:Java 我的试抓块有问题吗?,java,try-catch,Java,Try Catch,所以我们必须做一个抵押计算项目,我们必须要求用户再次计算,现在我们必须让它在每次用户输入任何输入的字符串值时打印出一条错误消息。我认为我做得对,但每次我运行它时都会发生一些奇怪的事情,我不知道为什么,我知道Try-Catch块出了问题 以下是我的产出: 正如你看到的,我第三次运行这个程序时,我输入了一个“2”作为第二个输入,它仍然进行计算。然后,第三次尝试时,我输入了一个负数,然后输入了一个“2”,一切都按照我想要的方式进行。然后,上次我运行它的时候,我把一个正数作为第一个输入,它仍然进行计算,
package MortgageCalculation2c;
import java.text.NumberFormat;
import java.util.Scanner;
/**
*
* @author Akira
*/
public class MortgageCalculation2c {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double loanAmount = 0;
double interestRate = 0;
double numberYears = 0;
double months;
double monthlyPayment;
double numerator;
double denominator;
double formula;
boolean userInput = true;
String answer = ("y");
while (userInput) {
try {
loanAmount = 0;
interestRate = 0;
numberYears = 0;
//prompt user for the loan amount
System.out.print("Enter the loan amount: ");
loanAmount = Double.parseDouble(in.nextLine());
//prompt the user for the interest rate
System.out.print("Enter the rate: ");
interestRate = Double.parseDouble(in.nextLine());
//prompt the user for thenumber of years
System.out.print("Enter the number of years: ");
numberYears = Double.parseDouble(in.nextLine());
} catch (NumberFormatException nfe) {
System.out.println("You must enter positive numerical data!");
}
//if the user enters a negative number print out a error message, if not, continue calculations
if ((loanAmount <= 0) || (interestRate <= 0) || (numberYears <= 0)) {
System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!");
} else {
//convert the interest rate
interestRate = interestRate / 100 / 12;
//the number of years must be converted to months
months = numberYears * 12;
//numerator of the monthly payment formula
numerator = (Math.pow(1 + interestRate, months));
//denominator of the monthly payment formula
denominator = ((numerator)-1);
//the formula equals the numerator divided by the denominator
formula = ( numerator / denominator );
//monthly payment calculation
monthlyPayment = (interestRate * loanAmount * formula);
//sytem output
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
System.out.println("The monthly payment is: " + defaultFormat.format(monthlyPayment));
}
//prompt the user if they would like to calculate the program again.
System.out.println("Would you like to calculate again (y/n) : ");
//if the user enters "y" the program will run again and if the user enters anything else it ends
answer = in.nextLine();
answer = answer.toLowerCase();
if ( answer.equals("y")){
userInput = true; //tests the program if it needs to run again
}else{
break; //ends the program
}
}
}
}
包抵押计算2c;
导入java.text.NumberFormat;
导入java.util.Scanner;
/**
*
*@作者Akira
*/
公共类抵押计算2C{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
双loanAmount=0;
双酯=0;
双耳数=0;
两个月;
双月付款;
双分子;
双分母;
双公式;
布尔userInput=true;
字符串答案=(“y”);
while(用户输入){
试一试{
loanAmount=0;
利率=0;
numberYears=0;
//提示用户输入贷款金额
系统输出打印(“输入贷款金额:”);
loanAmount=Double.parseDouble(在.nextLine()中);
//提示用户输入利率
系统输出打印(“输入速率:”);
interestRate=Double.parseDouble(in.nextLine());
//提示用户输入年数
System.out.print(“输入年份:”);
numberYears=Double.parseDouble(in.nextLine());
}捕获(NumberFormatException nfe){
System.out.println(“必须输入正数值数据!”);
}
//如果用户输入负数,请打印错误消息,否则,请继续计算
如果((loanAmount在catch
块中似乎需要continue
,则程序流返回循环
...
} catch (NumberFormatException nfe) {
System.out.println("You must enter positive numerical data!");
continue; // <---------- here
}
...
您将有一个特殊的静态
方法enterPositiveDouble
,如下所示:
static void enterPositiveDouble(String prompt) {
Scanner in = new Scanner(System.in);
boolean ok = true;
double result = -1;
do {
System.out.print(prompt);
ok = (in.HasNextDouble() && (result = in.NextDouble()) > 0)
if ! ok
System.out.println("You must enter positive numerical data!");
} while (!ok);
}
以上不是最佳代码,只是一个可能的解决方案的示例。请在将代码放在此处之前缩进代码。如果代码过长,则很难阅读和理解。我无法复制您的输出…请重新编译并重试;)我不确定try catch是否特定,但如果用户输入字符串值,程序应该打印错误消息。此外,我应该将scanner方法放在哪里(它看起来像什么?)。此外,我希望它看起来像我的图片上的输入4。我现在使用的方式是在添加“继续”后,直接返回到要求用户输入贷款金额,而不是要求用户再次运行。您输入的内容是什么导致NumberFormatException
?负数是不够的。您需要“1x2”(不是一个数字)。这就是为什么我说我不确定要使用什么异常以及后面应该放什么,因为nfe下面有一行表示它没有被使用
static void enterPositiveDouble(String prompt) {
Scanner in = new Scanner(System.in);
boolean ok = true;
double result = -1;
do {
System.out.print(prompt);
ok = (in.HasNextDouble() && (result = in.NextDouble()) > 0)
if ! ok
System.out.println("You must enter positive numerical data!");
} while (!ok);
}