英文:
How do I make my code loop back to the beginning in Java?
问题
我读过,我需要在一个“if”语句后面加上“continue”,但是每次我在我的if语句中尝试这样做,它都会显示“无法在循环外使用continue”。
英文:
I've read that I would need to put "continue" after an "if" statement, but every I tried this with my if statement and it states that "continue cannot be used outside of a loop."
答案1
得分: 2
在循环中设置它。例如:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true){
System.out.print("输入密码:密码必须至少包含八个字符,仅限字母和数字,并且至少包含两个数字。");
String s = input.nextLine();
if (thepassword(s)) {
System.out.println("有效密码");
break;
} else {
System.out.println("无效密码");
}
}
}
了解更多:Java中的Break和Continue
英文:
Set it in the loop. EX:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true){
System.out.print("Enter a password: The password must have at least eight characters, only letters and digits, and at least two digits. ");
String s = input.nextLine();
if (thepassword(s)) {
System.out.println("Valid Password");
break;
} else {
System.out.println("Invalid Password");
}
}
}
See more : Java Break and Continute
答案2
得分: 1
Here's the translated code:
使用一个带有标签的外部无限循环,然后使用 <code>break loop_lable</code> 退出循环,直到输入有效。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入密码:密码必须至少有八个字符,只能包含字母和数字,并且至少有两个数字。");
loop: for (;;) {
String s = input.nextLine();
if (thepassword(s)) {
System.out.println("有效密码");
break loop;
} else {
System.out.println("无效密码");
continue loop;
}
}
input.close();
}
public static boolean thepassword(String password) {
boolean thepassword = true;
if (password.length() < 8) {
thepassword = false;
} else {
int totaldigits = 0;
for (int n = 0; n < password.length(); n++) {
if (thedigit(password.charAt(n)) || theletter(password.charAt(n))) {
if (thedigit(password.charAt(n))) {
totaldigits++;
}
} else {
thepassword = false;
break;
}
}
if (totaldigits < 2) {
thepassword = false;
}
}
return thepassword;
}
public static boolean theletter(char c) {
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}
public static boolean thedigit(char c) {
return (c >= '0' && c <= '9');
}
}
Please note that I've translated the code as requested, excluding the code-related portions.
英文:
Use an outer infinite loop with a lable, then break the loop using <code>break loop_lable</code>. Check until a valid input is made.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a password: The password must have at least eight characters, only letters and digits, and at least two digits. ");
loop:for(;;)
{
String s = input.nextLine();
if (thepassword(s))
{
System.out.println("Valid Password");
break loop;
}
else
{
System.out.println("Invalid Password");
continue loop;
}
}
input.close();
}
public static boolean thepassword(String password) {
boolean thepassword = true;
if (password.length() < 8) {
thepassword = false;
} else {
int totaldigits = 0;
for (int n = 0; n < password.length(); n++) {
if (thedigit(password.charAt(n)) || theletter(password.charAt(n))) {
if (thedigit(password.charAt(n))) {
totaldigits++;
}
} else {
thepassword = false;
break;
}
}
if (totaldigits < 2) {
thepassword = false;
}
}
return thepassword;
}
public static boolean theletter(char c) {
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}
public static boolean thedigit(char c) {
return (c >= '0' && c <= '9');
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论