JAVA:无限循环问题

时间:2016-10-11 16:09:31

标签: java

我是Java的新手,我已经为我的中级课做了一个作业。它应该提示用户提供信息,然后将其显示在表格中。该程序按预期工作,除非提示回答" y或n",如果回答除了" y"它无限循环。     import java.util.Scanner;

public class InterestCalculator
{
   public static void main(String[] args)
   {

      Scanner userInput = new Scanner(System.in);

      double startingBalance;
      double interestRate;
      double endingBalance;
      double interestEarned;
      char yesOrNo;
      int temp;
      int numberOfQuarters;


      while(true)
      {
          System.out.println("Enter number of quarters from 1 to 10");
          numberOfQuarters = userInput.nextInt();
          if (numberOfQuarters > 0 && numberOfQuarters <=10)
          {
              break;
          }
          else
          {
              System.out.println("Number of quarters must be between 1 and 10 inclusive");
          }
      }

      while(true)
      {
          System.out.println("Enter the beginning principal balance greater than zero");
          startingBalance = userInput.nextDouble();
          if (startingBalance > 0)
          {
              break;
          }
          else
          {
              System.out.println("Beginning balance must be greater than zero");
          }
      }


      while(true)
      {
          System.out.println("Enter the interest rate percentage without the percent sign, greater than 0 percent and less than/equal to 20%"
            );
          interestRate = userInput.nextDouble();
          if (interestRate > 0 && interestRate <= 20)
          {
              break;
          }
          else
          {
              System.out.println("Interest rate must be between 1 and 20 inclusive");
          }
      }

      System.out.println("You entered a principal balance of $" + startingBalance + " for " + numberOfQuarters + " quarters at " + interestRate + "% interest.");
      System.out.println("Is this correct? (y/n)");
      yesOrNo = userInput.next().charAt(0);

      while(true)
      {   
         if (yesOrNo == 'y' || yesOrNo == 'Y')
         {
            break;            
         }
         else
         {
            System.out.println("Please provide the information again");
         }
      }

      System.out.println("Quarter       Beginning       Interest       Ending");
      System.out.println("Number        Balance         Earned         Balance");
      //here is where I am doing my calculations 
      for (int i = 0; i < numberOfQuarters; ++i)
      {     
         interestEarned = (startingBalance * (interestRate / 100) * (0.25) );
         endingBalance = startingBalance + interestEarned;

         System.out.printf((i+1) + "%20.2f %14.2f %15.2f \n", startingBalance, interestEarned, endingBalance);



      }
   }
}

我真的是一个菜鸟,所以我很抱歉这可能是多么混乱,如果它是一些我应该知道的小事。

非常感谢你。

以下是具体部分:

   System.out.println("You entered a principal balance of $" + startingBalance + " for " + numberOfQuarters + " quarters at " + interestRate + "% interest.");
          System.out.println("Is this correct? (y/n)");
          yesOrNo = userInput.next().charAt(0);

          while(true)
          {   
             if (yesOrNo == 'y' || yesOrNo == 'Y')
             {
                break;            
             }
             else
             {
                System.out.println("Please provide the information again");
             }
          }

4 个答案:

答案 0 :(得分:1)

在上述代码中,起始余额每季度变化而不是一个。因此,起始余额必须是上一季度的期末余额。     import java.util.Scanner;

 class InterestCalculator
{
public static void main(String[] args)
{

         Scanner userInput = new Scanner(System.in);

        double startingBalance;
            double interestRate;
        double endingBalance;
         double interestEarned;
         char yesOrNo;
        int temp;
         int numberOfQuarters;


         while(true)
         {
                System.out.println("Enter number of quarters from 1 to 10");
             numberOfQuarters = userInput.nextInt();
                if (numberOfQuarters > 0 && numberOfQuarters <=10)
                {
                     break;
                }
                else
                {
                     System.out.println("Number of quarters must be between 1 and 10 inclusive");
                }
        }

         while(true)
        {
             System.out.println("Enter the beginning principal balance greater than zero");
             startingBalance = userInput.nextDouble();
                if (startingBalance > 0)
             {
                     break;
             }
             else
                {
                    System.out.println("Beginning balance must be greater than zero");
             }  
        }


         while(true)
         {
                System.out.println("Enter the interest rate percentage without the percent sign, greater than 0 percent and less than/equal to 20%"
             );
             interestRate = userInput.nextDouble();
            if (interestRate > 0 && interestRate <= 20)
                {
                 break;
             }
             else
                {
                     System.out.println("Interest rate must be between 1 and 20 inclusive");
             }
        }

        System.out.println("You entered a principal balance of $" + startingBalance + " for " + numberOfQuarters + " quarters at " + interestRate + "% interest.");
        System.out.println("Is this correct? (y/n)");
        yesOrNo = userInput.next().charAt(0);

        while(true)
        {   
             if (yesOrNo == 'y' || yesOrNo == 'Y')
            {
                    break;            
            }
            else
            {
                    System.out.println("Please provide the information again");
            }
        }

         System.out.println("Quarter       Beginning       Interest       Ending");
        System.out.println("Number        Balance         Earned         Balance");

        for (int i = 0; i < numberOfQuarters; ++i)
        {     
            interestEarned = (startingBalance * (interestRate / 100) * (0.25) );
            endingBalance = startingBalance + interestEarned;

            System.out.printf((i+1) + "%20.2f %14.2f %15.2f \n", startingBalance, interestEarned, endingBalance);
        startingBalance=endingBalance;


        }
}
}

答案 1 :(得分:0)

我不知道你真正想要达到的目标,但我认为你错过了一个变量。你总是根据你的startingBalance进行计算,而且你永远不会改变它的值,所以你总是得到相同的值。

看看这是否有帮助。

    double currentBalance = startingBalance;

    for (int i = 0; i < numberOfQuarters; ++i) {
        interestEarned = (currentBalance * (interestRate / 100) * (0.25));
        endingBalance = currentBalance + interestEarned;

        System.out.printf((i + 1) + "%20.2f %14.2f %15.2f \n", currentBalance, interestEarned, endingBalance);

        currentBalance = endingBalance;
    }

答案 2 :(得分:0)

你有一些无限循环。要解决你所拥有的每个其他clouse应该在最后一行中包含一个中断,这样它只会向控制台打印一条消息。以下是如何操作的一个示例:

    while(true)
    {
        if (yesOrNo == 'y' || yesOrNo == 'Y')
        {
            break;
        }
        else
        {
            System.out.println("Please provide the information again");
            break;
        }
    }

替换以下行:

System.out.println("Enter the interest rate percentage without the percent sign, greater than 0 percent and less than/equal to 20%");

行:

System.out.println("Enter the interest rate percentage without the percent sign, greater than 0 percent and less than/equal to " + startingBalance/numberOfQuarters );

错误的是,您将20%硬编码到打印行中。

答案 3 :(得分:0)

除了像硬编码值这样的小错误外,你还有严重的缺陷:

  1. 对于&#39; Y&#39;以外的值,以下代码将是无限循环或者&#39; y&#39;,
  2. 无限循环部分:

    while (true) {
        if (yesOrNo == 'y' || yesOrNo == 'Y') {
            break;
        } else {
            System.out.println("Please provide the information again");
        }
    }
    
    1. 正如djointster所述,您尚未对计算进行正确的初始化,因此发送零(0)会使interestEarned为零。