我想将“numberOfItems”设置为一个大数字但是想要在中途停止循环。我需要帮助的部分时间。请不要ArrayList,我还不熟悉。
$(".pin-input").keyup(function () {
this.value = this.value.replace(/[^0-9\.]/g,'');
if (this.value.length == this.maxLength) {
var $next = $(this).parent().next('.input-container').find('.pin-input');
if ($next.length) {
$next.focus();
}
else {
$(this).blur();
}
}
});
$('button.show-password').each(function(){
var selected_container = $(this).data("container"); // Get button container value
var selected_input = $(selected_container).find("input"); // Selected input withing container
var selected_icon = $(this).find('i');
$(this).mousedown(function(){
$(this).toggleClass('active');
$(this).attr('value', 'Hide');
selected_input.attr('type', 'text');
if(selected_icon.length > 0) {
selected_icon.toggleClass('icon-show-password');
}
else {
$(this).text("Hide");
}
}).mouseup(function(){
$(this).toggleClass('active');
$(this).attr('value', 'Show');
selected_input.attr('type', 'password');
if(selected_icon.length > 0) {
selected_icon.toggleClass('icon-show-password');
}
else {
$(this).text("Show");
}
});
});
答案 0 :(得分:2)
您可以在if
循环内放置for
语句,以决定何时停止它;停止循环的指令是break
。
请注意,这意味着您不需要封闭的do
循环。
答案 1 :(得分:1)
查看您的代码是如何工作的,最明智的地方可能是在输入产品名称之后。这意味着您无法存储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
}
}
答案 2 :(得分:0)
更新这是详细解释的增强型答案
// 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");}
}
}