我一直在與這個程序戰鬥一段時間了。它已經開始正確編譯,但是當我按原樣運行它時,它會無限地顯示「您的賬單是__」。當我改變初始響應值時,它拒絕運行。我對如何解決這個問題不知所措。無限循環或提前終止做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!");
}
}
我有響應= kb.nextLine();行兩次,因爲程序不會因爲某種原因暫停用戶輸入。但是當我按照你所說的改變它時,它並沒有在一開始就回到while語句。 – SMoore 2013-02-12 04:29:56
沒關係,我意識到我需要移動閱讀用戶輸入。十分感謝你的幫助! – SMoore 2013-02-12 04:59:12