我只是想知道如果语句有任何goto或continue命令吗?我用continue;但是if语句不起作用。
if ( typess == 1){
***OUTER:***
System.out.println("Type: Current Minimum of 1000.00 ");
System.out.print("Amount: " );
currenttype = Float.parseFloat(in.readLine());
if ( currenttype >= 1000 ){
balance += currenttype;
System.out.print("Successful " + currenttype + " Added to your Account");
}
else
{
**continue OUTER;**
System.out.print("MINIMUM IS 1000.00 ");
}
可以用简单的递归方法求解。
只需将逻辑部分包含到方法中,验证输入,如果输入不正确,则进行递归调用,如以下示例:
public class Project {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
float float_val = getInput();
System.out.println("You did input: " + float_val);
}
public static float getInput() {
System.out.println("Please input variable");
float input = scanner.nextFloat();
if(input < 0) {
System.out.println("Invalid input!");
return getInput();
}
return input;
}
}
示例输入:
-5
-4
5
示例输出:
Please input variable
-5
Invalid input!
Please input variable
-4
Invalid input!
Please input variable
5
You did input: 5.0
有这种可能性,但您应该使用do-while循环,例如
boolean printMsg = false;
do {
if (printMsg) {
System.out.print("MINIMUM IS 1000.00 ");
}
printMsg = true;
System.out.println("Type: Current Minimum of 1000.00 ");
System.out.print("Amount: " );
currenttype = Float.parseFloat(in.readLine());
} while (currenttype < 1000);
balance += currenttype;
System.out.print("Successful " + currenttype + " Added to your Account");