2017-04-24 89 views
1

我想设置“numberOfItems”为一个大数字,但想要中途停止循环。我需要时间部分的帮助。没有ArrayList请,我还不熟悉。如何使用“停止”作为关键字来停止for循环?

do 
{ 
    for(int i=0; i<=numberOfItems; i++) 
    { 
     System.out.println("Enter product name"); 
     productName[i]=input.nextLine(); 
     System.out.println("Enter price of product"); 
     productPrice[i]=input.nextDouble(); 
     System.out.printf("%s,%n,%.2f",productName[i],productPrice[i]); 
    } 
} 
while (! (productName[i]= input.nextLine("stop"))); 
+1

使用'break'来循环出来。 – yogidilip

+0

你需要break语句吗? https://www.tutorialspoint.com/java/java_break_statement.htm – user3685285

+0

你有两个循环,你想停止哪一个循环? – Adam

回答

1

解释看你的代码是如何工作的,最明智的地方,打破可能是输入产品名称后。这意味着你不能存储停止产品...我已经把它作为大写(如果你不关心案例,你可以使用equalsIgnoreCase)。

事情是这样的:

for(int i=0; i<=numberOfItems; i++) 
    { 
     System.out.println("Enter product name (or STOP to stop)"); 
     String tmpProduct = input.nextLine(); 

     //trim to avoid whitespace 
     if ("STOP".equals(tmpProduct.trim())) { 
      break; //we stop the loop here 
     } 

     //they didn't type STOP, guess they wanted a product. 
     productName[i]=tmpProduct; 


     System.out.println("Enter price of product"); 
     productPrice[i]=input.nextDouble(); 
     System.out.printf("%s,%n,%.2f",productName[i],productPrice[i]); 
    } 

这也避免了外循环的需要。如果你想问问每件产品(这可能会在一段时间后变得烦人),那么你可以在请求双倍数的情况下进行检查并提示。无论你想

for(int i=0; i<=numberOfItems; i++) 
    { 
     System.out.println("Enter product name"); 

     //they didn't type STOP, guess they wanted a product. 
     productName[i]=input.nextLine(); 

     System.out.println("Enter price of product"); 
     productPrice[i]=input.nextDouble(); 
     System.out.printf("%s,%n,%.2f",productName[i],productPrice[i]); 

     System.out.println("type STOP to stop or anything else to continue"); 
     String tmp = input.nextLine(); 
     //trim to avoid whitespace problems 
     if ("STOP".equals(tmp.trim())) { 
      break; //we stop the loop here 
     } 
    } 
+1

非常感谢! –

+0

这就是为什么我提供了两个循环 - 请解释程序如何很容易崩溃 - 我现在可以预见的发生的唯一方法是无效输入 - 捕获异常会阻止这种情况,但这不是问问者要求的。如果你能提供这样一个例子,我会很乐意修改我的答案。 有**正好**两种方式(不包括异常终止)终止每个循环,如我的答案中所述。 – WebPigeon

+0

顺便说一句,nextDouble()不会读直到newLine char,所以它会在第一次循环后搞乱一切。下次请在提交前尝试运行您的代码! – Yahya

2

你可以把一个if声明您for循环内决定何时停止;停止循环的指令是break

请注意,这意味着您不需要附上do循环。

0

UPDATE这增强了答案细节

// this method to validate the input after reading the entire line 
static public Object interpretedInput(String line){ 
    if(line.replace(" ", "").equalsIgnoreCase("stop")){ // if stop detected 
     return null; 
    } 
    else {// if it's not stop 
     for(char c : line.toCharArray()){ // read every char in the line 
      if(!Character.isDigit(c) && c!='.'){ // if any non digit is detected that means it should be considered as a string 
       //(note if you want the product name to consist of digits only -> this won't work) 
       return line; // return line 
      } 
     } 
    } 
    try{return Double.valueOf(line);} // else try to parse the line to extract the double value and return it 
    catch(NumberFormatException e){return null;} 
} 


Scanner input = new Scanner(System.in); 
int numberOfItems = 10; // for example 
String[]productName = new String[10]; 
double[] productPrice = new double[10]; 
for(int i=0; i<numberOfItems; i++){ 
    System.out.println("Enter product name"); 
    Object theInput = interpretedInput(input.nextLine()); // the method will return either null or string or double 
    if(theInput==null){ // if it's null that means to stop 
     break; 
    } 
    else if (theInput instanceof String){ // if it's instance of string accept it 
      productName[i]=(String)theInput; 
    } 
    while(!(theInput instanceof Double)){ // this will repeat until a valid double input is entered 
     //then it will assign it and display the content 
     System.out.println("Enter price of product"); 
     theInput = interpretedInput(input.nextLine()); 
     if(theInput==null){ 
       i=numberOfItems; // to terminate the parent for loop as well 
       break; 
     } 
     else if (theInput instanceof Double){ 
      productPrice[i]=(Double)theInput; 
      System.out.printf("%s, %.2f\n",productName[i],productPrice[i]); 
     } 
     else{System.out.println("Invalid Price");} 
    } 
}