Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无限循环或提前终止do while循环_Java_Infinite Loop_Do While - Fatal编程技术网

Java 无限循环或提前终止do while循环

Java 无限循环或提前终止do while循环,java,infinite-loop,do-while,Java,Infinite Loop,Do While,我为这个项目奋斗了一段时间。它已开始正确编译,但当我按原样运行它时,它无限地显示“您的账单是”。当我更改初始响应值时,它拒绝运行。我不知道如何修理它 /* Savannah Moore CSCI 1301 2/11/2013 Internet Package Code: Code determines customer's final pricing. */ import java.util.Scanner; public class internet{ publ

我为这个项目奋斗了一段时间。它已开始正确编译,但当我按原样运行它时,它无限地显示“您的账单是”。当我更改初始响应值时,它拒绝运行。我不知道如何修理它

/* 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语句中。因此,当您
中断时在case语句之外,您立即跳回“while”检查,因为响应和检查仍然是“Yes”,所以它会再次执行所有操作

只需将这些语句放在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();   
  }
}
致:


我不知道你为什么要做
response=kb.nextLine()两次。

循环无限,因为用户将响应更改为“否”的机会在case语句中。因此,当您
中断时在case语句之外,您立即跳回“while”检查,因为响应和检查仍然是“Yes”,所以它会再次执行所有操作

只需将这些语句放在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();   
  }
}
致:


我不知道你为什么要做
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!");
    }
}

这应该能奏效

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

我有一个响应=kb.nextLine();行两次,因为由于某种原因,程序不会暂停用户输入,而不暂停用户输入。但是,当我按照您所说的更改}时,它并没有返回到开始时的while语句。更不用说我意识到我需要移动对用户输入的读取。非常感谢你的帮助!我有一个响应=kb.nextLine();行两次,因为由于某种原因,程序不会暂停用户输入,而不暂停用户输入。但是,当我按照您所说的更改}时,它并没有返回到开始时的while语句。更不用说我意识到我需要移动对用户输入的读取。非常感谢你的帮助!问题是,如果用户输入的编号为“否”,则我需要将响应设置为“假”。我已编辑我的答案…如果问题已解决,则将答案标记为“已解决”,以供将来参考。问题是如果用户输入的编号为“否”,则我需要将响应设置为“假”。我已编辑我的答案…如果问题已解决,则将答案标记为“已解决”,以供将来参考参考资料。