需要帮助利用 while 循环重复部分代码
Need help utilising the while loop for repeating parts of code
我试图在我的简单程序中使用 while 循环重新 运行 部分代码。在这个程序中,用户创建一个简单的帐户,输入验证码,然后登录。登录后,我希望允许用户退出、编辑他的个人资料设置等。但是我有一个小问题。
假设用户刚刚编辑了他们的帐户设置。我希望他们能够 return 到 "menu",而不是程序终止。
问题在于如何做到这一点。由于 java 中没有 goto
语句,根据我的阅读,我必须使用 while
循环。我不知道该怎么做。我就是无法理解它。循环总是让我感到困惑。另外,我是否应该使用 while
循环?使用 for
或 do-while
循环会更好吗?我应该用什么表达方式?我需要 break
声明吗?
我知道这不是一个具体的问题,但是非常感谢任何帮助我走上正确道路的帮助。
下面是完整的代码供参考。
public static void main(String[] args) {
System.out.println("Hi! To begin please choose your username.");
System.out.println("To do this, please enter your username below. ");
System.out.println("This name must be at least three characters.");
Scanner userInput = new Scanner(System.in);
String userName = userInput.next();
int nameLength = userName.length();
if (nameLength > 2) {
System.out.println("Now please enter your password.");
System.out.println("This password must be at lease five characters");
String passWord = userInput.next();
int passLength = passWord.length();
if (passLength > 4) {
System.out.println("Signup alost complete.");
Random rand = new Random();
int randm = rand.nextInt(100000) + 1;
System.out.println("To confirm you are not a robot, please enter this code: " + randm);
int code = userInput.nextInt();
if (code == randm) {
System.out.println("Thank you, " + userName);
System.out.println("You may now login. Begin by entering your username");
//Where I would go if the user signed out
String name = userInput.next();
if (name.equals(userName)) {
System.out.println("Now please enter you password");
String pass = userInput.next();
if (pass.equals(passWord)) {
System.out.println("Thank you. You have now successfully logged in");
//Where the "main menu" will be
//Rest of code will also go here
}
else {
System.out.println("Password is incorrect");
}
}
else {
System.out.println("Username is incorrect");
}
}
else {
System.out.println("The code entered is incorrect.");
}
}
else {
System.out.println("Invalid Password");
}
}
else {
System.out.println("Invalid Username");
}
}
你可以把你的代码放在do里面。它不会中断并会继续循环。
do{
}
while(true);
您发表了评论//where would I go if the user signed out?
答案是,您将在 sign in
注销时向他显示消息,以便他可以再次登录。您可以通过使用 for loop 或 loop 或任何您想要的循环来做到这一点。这意味着用户登录的部分将处于循环状态,如果用户登录,则菜单将显示出来。如果用户注销,登录表单将无限显示。
我试图在我的简单程序中使用 while 循环重新 运行 部分代码。在这个程序中,用户创建一个简单的帐户,输入验证码,然后登录。登录后,我希望允许用户退出、编辑他的个人资料设置等。但是我有一个小问题。
假设用户刚刚编辑了他们的帐户设置。我希望他们能够 return 到 "menu",而不是程序终止。
问题在于如何做到这一点。由于 java 中没有 goto
语句,根据我的阅读,我必须使用 while
循环。我不知道该怎么做。我就是无法理解它。循环总是让我感到困惑。另外,我是否应该使用 while
循环?使用 for
或 do-while
循环会更好吗?我应该用什么表达方式?我需要 break
声明吗?
我知道这不是一个具体的问题,但是非常感谢任何帮助我走上正确道路的帮助。
下面是完整的代码供参考。
public static void main(String[] args) {
System.out.println("Hi! To begin please choose your username.");
System.out.println("To do this, please enter your username below. ");
System.out.println("This name must be at least three characters.");
Scanner userInput = new Scanner(System.in);
String userName = userInput.next();
int nameLength = userName.length();
if (nameLength > 2) {
System.out.println("Now please enter your password.");
System.out.println("This password must be at lease five characters");
String passWord = userInput.next();
int passLength = passWord.length();
if (passLength > 4) {
System.out.println("Signup alost complete.");
Random rand = new Random();
int randm = rand.nextInt(100000) + 1;
System.out.println("To confirm you are not a robot, please enter this code: " + randm);
int code = userInput.nextInt();
if (code == randm) {
System.out.println("Thank you, " + userName);
System.out.println("You may now login. Begin by entering your username");
//Where I would go if the user signed out
String name = userInput.next();
if (name.equals(userName)) {
System.out.println("Now please enter you password");
String pass = userInput.next();
if (pass.equals(passWord)) {
System.out.println("Thank you. You have now successfully logged in");
//Where the "main menu" will be
//Rest of code will also go here
}
else {
System.out.println("Password is incorrect");
}
}
else {
System.out.println("Username is incorrect");
}
}
else {
System.out.println("The code entered is incorrect.");
}
}
else {
System.out.println("Invalid Password");
}
}
else {
System.out.println("Invalid Username");
}
}
你可以把你的代码放在do里面。它不会中断并会继续循环。
do{
}
while(true);
您发表了评论//where would I go if the user signed out?
答案是,您将在 sign in
注销时向他显示消息,以便他可以再次登录。您可以通过使用 for loop 或 loop 或任何您想要的循环来做到这一点。这意味着用户登录的部分将处于循环状态,如果用户登录,则菜单将显示出来。如果用户注销,登录表单将无限显示。