加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 兴趣计算器不显示结果

发布时间:2020-12-15 02:17:03 所属栏目:Java 来源:网络整理
导读:我试图在我的 Java类的最后一分钟完成这个,当我运行程序后询问用户信息是否正确,它只是循环回到第一个问题,无论如何.这是代码: import java.util.Scanner;public class InterestCalculator { public static void main(String[] args) { Scanner input = new
我试图在我的 Java类的最后一分钟完成这个,当我运行程序后询问用户信息是否正确,它只是循环回到第一个问题,无论如何.这是代码:

import java.util.Scanner;

public class InterestCalculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String userResponse = null;

        do {
            int quartersDisplayed = -1;
            double startingBalance = -1,interestRate = -1;

            do {
                System.out.println("Enter the numbers of quarters you wish to display that is greater than zero and less or equal to 10: ");
                userResponse  = input.next();

                try{
                    quartersDisplayed = Integer.parseInt(userResponse);
                } catch(NumberFormatException e) {

                }

                if(quartersDisplayed <= 0 || quartersDisplayed > 10) {
                    System.out.println("Sorry,that value is not valid.");
                } else {
                    break;
                }
            } while(true);


            do {
                System.out.println("Enter the starting balance (must be greater than zero): ");
                userResponse  = input.next();

                try {
                    startingBalance = Double.parseDouble(userResponse);
                } catch(NumberFormatException e) {

                }

                if(startingBalance <= 0) {
                    System.out.println("Sorry,that value is not valid.");
                } else {
                    break;
                }
            } while(true);


            do {
                System.out.println("Enter the interest rate (greater than zero less than twenty percent): ");
                userResponse  = input.next();

                try {
                    interestRate = Double.parseDouble(userResponse);
                } catch(NumberFormatException e) {

                }

                if(interestRate <= 0 || interestRate > 20){
                    System.out.println("Sorry,that value is not valid.");
                } else {
                    break;
                } 
            } while(true);


            System.out.println("You have entered the following amount of quarters: "
                            + quartersDisplayed);
            System.out.println("You also entered the starting balance of: " + startingBalance);
            System.out.println("Finally,you entered the following of interest rate: "
                            + interestRate);
            System.out.println("If this information is not correct,please exit the program and enter the correct information.");

            double quarterlyEndingBalance = startingBalance + (startingBalance * interestRate / 100 * .25);
            System.out.println("Your ending balance for your quarters is "
                    + quarterlyEndingBalance);
            System.out.println("Do you want to continue?");
            userResponse = input.next();

            if("y".equalsIgnoreCase(userResponse) || "yes".equalsIgnoreCase(userResponse))
                continue;
            else
                break;

        } while(true);
    }
}

我正在寻找的样本输出:

Enter number of quarters from 1 to 10
5
Enter the beginning principal balance greater than zero
4500
Enter the interest rate percentage without the percent sign,greater than 0 percent and less than/equal to 20%
3.5
You entered a principal balance of $4500.0 for 5 quarters at 3.5% interest.
Is this correct? (y/n)
y
Quarter       Beginning       Interest       Ending
Number        Balance         Earned         Balance
1             4500.00          39.38         4539.38

解决方法

如果输入错误,你可能要做的就是重启循环.在这种情况下,您只需要切换继续和中断.

continue语句使循环立即开始下一次迭代.你应该用空语句删除它;或者否定if条件,删除else并使其为true.此外,您的代码不做任何其他事情,然后获取输入并询问它是否正确.因为它是在do … while(true)循环中编写的,所以这个循环永远不会结束.因此要么删除该循环,要么添加一个选项以中止该过程.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读