【问题标题】:Infinite Loop or Early terminating do while loop无限循环或提前终止 do while 循环
【发布时间】:2013-02-12 03:53:02
【问题描述】:

我已经和这个程序斗争了一段时间了。它已经开始正确编译,但是当我按原样运行它时,它会无限显示“你的账单是__”。当我更改初始响应值时,它拒绝运行。我不知道如何解决它。

/* Savannah Moore
   CSCI 1301
    2/11/2013
    Internet Package Code: Code determines customer's final pricing. */
import java.util.Scanner;

public class internet{
    public static void main(String[] args){
      double hours, price;
      int internet;
      String response, check="Yes";


      Scanner kb = new Scanner(System.in); /* This sections questions the user as to which package they have bought. */
       System.out.println("Our internet packages include:");
        System.out.println("Package 1: For $9.95 per month, 10 hours of access are provided. Additional hours are $2.00 per hour.");
        System.out.println("Package 2: For $13.95 per month, 20 hours of access are provided. Additional hours are $1.00 per hour.");
        System.out.println("Package 3: For $19.95 per month, unlimited access is provided.");
        System.out.println("Please enter your package number:");
       internet=kb.nextInt();
        System.out.println("Please enter your total hours used:");
       hours=kb.nextDouble();   
        response = "Yes";

while(check.compareToIgnoreCase(response)== 0)
  {
      switch (internet)
      {
      case 1: 
              {  if (hours > 10)
                       price = (hours-10)*2 + 9.95; /* This adds the additional hours to the user's price at the current overage rate. */
                    else    
                        price = 9.95;
                }
              System.out.println("Your bill is $" +price);  
               break; 
      case 2: 
              {  if (hours > 20)
                       price = (hours-20) + 13.95;
                    else    
                        price = 13.95;
                }
              System.out.println("Your bill is $" +price);  
               break;        
     case 3:System.out.println("Your bill is $19.95");  
                break; 
      default: System.out.println("This package choice is invalid.");

      System.out.println("Would you like to compare your price with that of another package? Yes or No"); /* This gives the user the ability to stop the loop. */
      response=kb.nextLine();
      response=kb.nextLine();   
     }
    }

    System.out.println("Thank you for using us as your internet service provider.  Have a nice day!");
    }
    }

【问题讨论】:

    标签: java infinite-loop do-while


    【解决方案1】:

    循环进入无限循环,因为用户将响应更改为“否”的机会在 case 语句中。因此,当您 break; 退出 case 语句时,您会立即跳回 'while' 检查,并且因为 response 和 check 都仍然是 'Yes',它会再次执行所有操作。

    只需将这些语句放在 case 语句中的 default: 案例之后,它应该可以工作。

    换句话说,改变:

       ...
       default: System.out.println("This package choice is invalid.");
    
       System.out.println("Would you like to compare your price with that of another package? 
           Yes or No"); /* This gives the user the ability to stop the loop. */
       response=kb.nextLine();
       response=kb.nextLine();   
      }
    }
    

    到:

         ...
         default: System.out.println("This package choice is invalid.");
       }
       System.out.println("Would you like to compare your price with that of another package? 
            Yes or No"); /* This gives the user the ability to stop the loop. */
       response=kb.nextLine();
       response=kb.nextLine();   
    }
    

    我不确定你为什么要两次response=kb.nextLine();。

    【讨论】:

    • 我有 response=kb.nextLine();行两次,因为由于某种原因,程序不会因用户输入而暂停。但是,当我按照您所说的更改 } 时,它并没有回到开头的 while 语句。
    • 没关系,我意识到我需要移动对用户输入的阅读。非常感谢您的帮助!
    【解决方案2】:

    这应该可以解决问题

    import java.lang.*;
    import java.util.Scanner;
    
    public class Program
    {
        /**
         * This is the main entry point for the application
         */
      public static void main(String[] args){
          double hours, price;
          int internet;
          String response, check="Yes";
    
    
            response = "Yes";
    
    while(check.compareToIgnoreCase(response)== 0)
      {
    
          Scanner kb = new Scanner(System.in); /* This sections questions the user as to which package they have bought. */
           System.out.println("Our internet packages include:");
            System.out.println("Package 1: For $9.95 per month, 10 hours of access are provided. Additional hours are $2.00 per hour.");
            System.out.println("Package 2: For $13.95 per month, 20 hours of access are provided. Additional hours are $1.00 per hour.");
            System.out.println("Package 3: For $19.95 per month, unlimited access is provided.");
            System.out.println("Please enter your package number:");
           internet=kb.nextInt();
            System.out.println("Please enter your total hours used:");
           hours=kb.nextDouble();   
          switch (internet)
          {
          case 1: 
                  {  if (hours > 10)
                           price = (hours-10)*2 + 9.95; /* This adds the additional hours to the user's price at the current overage rate. */
                        else    
                            price = 9.95;
                    }
                  System.out.println("Your bill is $" +price);  
                   break; 
          case 2: 
                  {  if (hours > 20)
                           price = (hours-20) + 13.95;
                        else    
                            price = 13.95;
                    }
                  System.out.println("Your bill is $" +price);  
                   break;        
         case 3:System.out.println("Your bill is $19.95");  
                    break; 
          default: System.out.println("This package choice is invalid.");
          }
          System.out.println("Would you like to compare your price with that of another package? Yes or No"); /* This gives the user the ability to stop the loop. */
          response=kb.nextLine();
          response=kb.nextLine();   
    
        }
    
        System.out.println("Thank you for using us as your internet service provider.  Have a nice day!");
        }
    }
    

    【讨论】:

    • 问题是如果用户输入的不是,我需要响应为假。
    • 我已经编辑了我的答案....如果问题已解决,请将答案标记为已解决以供将来参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2011-12-16
    • 2021-07-27
    相关资源
    最近更新 更多