2017-04-24 59 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");} 
    } 
}