2013-02-12 111 views
1

我一直在與這個程序戰鬥一段時間了。它已經開始正確編譯,但是當我按原樣運行它時,它會無限地顯示「您的賬單是__」。當我改變初始響應值時,它拒絕運行。我對如何解決這個問題不知所措。無限循環或提前終止做while循環

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

回答

5

循環變得無限,因爲用戶將響應更改爲'否'的機會在case語句中。因此,當你的案件陳述中出現break;時,你立即跳回'while'檢查,並且因爲回覆檢查都是'是',它再次執行。

只要在案件陳述中的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();兩次。

+0

我有響應= kb.nextLine();行兩次,因爲程序不會因爲某種原因暫停用戶輸入。但是當我按照你所說的改變它時,它並沒有在一開始就回到while語句。 – SMoore 2013-02-12 04:29:56

+0

沒關係,我意識到我需要移動閱讀用戶輸入。十分感謝你的幫助! – SMoore 2013-02-12 04:59:12

0

這應該做的伎倆

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

問題是,如果用戶輸入不是,我將需要響應爲假。 – SMoore 2013-02-12 04:30:42

+0

我編輯了我的答案....如果問題得到解決,請將答案標記爲已解決以備將來參考。 – wcraft 2013-02-12 05:06:36