簡體   English   中英

無限循環或盡早終止while循環

[英]Infinite Loop or Early terminating do while loop

我已經和這個程序戰斗了一段時間了。 它已經開始正確編譯,但是當我按原樣運行時,它會無限顯示“您的帳單是__”。 當我更改初始響應值時,它拒絕運行。 我對如何解決它一無所知。

/* 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!");
    }
    }

循環是無限的,因為用戶將響應更改為“否”的機會在case語句內。 因此,當您break; 從case語句中退出后,您立即跳回“ while”檢查,並且由於responsecheck仍為“ Yes”,因此將再次執行所有操作。

只需將這些語句放在default: case后面的case語句中,它就可以工作。

換句話說,更改:

   ...
   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(); 兩次。

這應該可以解決問題

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!");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM