[英]How do I use “stop” as a keyword to stop the for loop?
我想將“numberOfItems”設置為一個大數字,但想要中途停止循環。 我需要幫助的部分時間。 請不要ArrayList,我還不熟悉。
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")));
你可以在for
循環中放入一個if
語句來決定何時停止它; 停止循環的指令是break
。
請注意,這意味着您不需要封閉的do
循環。
看看你的代碼是如何工作的,最明智的地方就是在輸入產品名稱之后。 這意味着您無法存儲STOP產品......我將其保留為大寫(如果您不關心大小寫,可以使用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
}
}
更新這是增強的答案,詳細解釋
// 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");}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.