while循环:负输入检查并添加try catch for strings

时间:2015-10-20 05:49:58

标签: java while-loop try-catch

我必须制作一个

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

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

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

  4. 当我尝试抓住时,我会在variable not have been initialized之后获取变量try catch

    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();

1 个答案:

答案 0 :(得分: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(loop.equals("y")){你应该写while(true) {,因为你已经在那个循环中检查过了