【问题标题】:While loop: negative input checking and adding try catch for stringsWhile循环:负输入检查并为字符串添加try catch
【发布时间】:2015-10-20 05:49:57
【问题描述】:

我必须做一个计算器

  1. 以正数输入运行程序,显示输出并询问用户是否要循环。

  2. 使用字符串输入运行程序(两个而不是 2 个),使用 try 和 catch 显示一条消息告诉用户错误,并询问用户是否要循环。

  3. 以负数输入运行程序,显示错误消息而不显示输出,并询问用户是否要循环。

当我尝试 catch 时,我会在 try catch 之后得到 variable not have been initialized 变量。

try {
    System.out.print("Enter loan amount: ");
    loanNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter rate: ");
    rateNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter number years: ");
    yearNum = Double.parseDouble(in.nextLine());
}
catch(NumberFormatException e) { 
    System.out.println ("You must enter positive numeric data!");
}

如果没有try catch,我无法弄清楚如何在不使用System.exit(0); 不显示输出的情况下执行第 2 项。这结束了用户能够输入循环。

我不确定如何修复我的 while 循环。当我运行程序时,它只会运行一次然后循环一次。之后它将自动循环而不提示并在一行中吐出一行,例如:

Would you like to calculate again (y/n):
Enter loan amount:

代码:

package whileloopyn;

import java.util.Scanner;

public class Whileloopyn {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    //Declare Variables
    double loanNum;
    double rateNum;
    double trateNum;
    double yearNum;
    double monthNum;
    double equNum;
    double outputNum;

    //Prompt for input and try catch for exception error (rate inputed as two)

    System.out.print("Enter loan amount: ");
    loanNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter rate: ");
    rateNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter number years: ");
    yearNum = Double.parseDouble(in.nextLine());


    //Postive input data check (negative then ask again)
    if(loanNum <= 0 || rateNum <=0 || yearNum <=0)
    {
        System.out.println("You must enter positive numeric data! ");
        System.exit(0);
    }


    //Convert years to months 
    monthNum = yearNum * 12;

    //Convert rate to percent and monthly
    trateNum= (rateNum / 100) /12; 

    //Complete numerator 
    equNum = Math.pow((1+ trateNum), monthNum );

    //Plug equNum into formula
    outputNum = trateNum * loanNum * (equNum / (equNum - 1));

    //output answer
    System.out.printf("The monthly payment is: $ %.2f%n", outputNum);

    //prompt user y n to run program again
    System.out.println("Would you like to calculate again (y/n): ");
    String loop = in.nextLine();

    //while loop with (input

    boolean isContinuing = true;

    while (isContinuing) {
        while(loop.equals("y")){
        System.out.print("Enter loan amount: ");
        loanNum = in.nextDouble();

        System.out.print("Enter rate: ");
        rateNum = in.nextDouble();

        System.out.print("Enter number years: ");
        yearNum = in.nextDouble();

        //Postive input data check (negative then loop again)
        if(loanNum <= 0 || rateNum <=0 || yearNum <=0)
        {
           System.out.println("You must enter positive numeric data! ");
           System.exit(0);
        }


        //Convert years to months 
        monthNum = yearNum * 12;

        //Convert rate to percent and monthly
        trateNum= (rateNum / 100) /12; 

        //Complete numerator 
        equNum = Math.pow((1+ trateNum), monthNum );

        //Plug equNum into formula
        outputNum = trateNum * loanNum * (equNum / (equNum - 1));

        //output answer
        System.out.printf("The monthly payment is: $ %.2f%n", outputNum);
        //ask user if you want to use program again
        System.out.printf("Would you like to calculate again (y/n): ");

        if(loop.equals("n"))
        {
            System.exit(0);
        }
    }
    }

}

}

有人建议 (input.equals(String)) 是我应该如何比较 while 循环的字符串并使用 in.nextLine();

【问题讨论】:

  • 如果您询问用户,您实际上需要获取输入,而不仅仅是直接进入 if 条件。您还需要通过调用nextLine 在使用任何nextFoo(不包括nextLine)方法后捕获回车符。供此检查参考this

标签: java while-loop try-catch


【解决方案1】:

如果用户按下Y,您必须继续该循环:

        System.out.printf("Would you like to calculate again (y/n): ");
        Scanner s = new Scanner(System.in);
        if(s.nextLine().equalsIgnoreCase("y")) {
            continue;
        } else {
            break;
        }

你应该写while(true) {而不是while(loop.equals("y")){,因为你已经在那个循环中检查过了

【讨论】:

    猜你喜欢
    • 2012-10-05
    • 2018-09-08
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 2015-04-24
    • 2014-09-11
    相关资源
    最近更新 更多