簡體   English   中英

do-while循環后代碼沒有繼續?

[英]Code doesn't continue after the do-while loop?

我已經開始在AS級別的計算機課程中學習Java,並真正地着手完成了我們設定的第一個DIY任務。

我使用了一個do-while語句來查看用戶輸入的用戶名是否在數組“名稱”中-如果不是,它將請求重新輸入用戶名,直到插入正確的用戶名為止。 我還設置了一個布爾值,因此當輸入正確的用戶名時,它將取消do-while循環並繼續執行代碼-但事實並非如此。

String[] names = {"mckeownl", "heardj", "williamsc"};
String[] attendance = {"yes", "no", "yes"};
int[] grade = {96, 66, 73};

boolean loggedin = false;

Scanner user_input = new Scanner(System.in);
String login;
login = user_input.next();

do {   // beginning of while - login

    System.out.println("Insert student's surname followed by the first letter");
    System.out.print("of their first name (e.g John Smith = smithj): ");

    if (Arrays.asList(names).contains(login)) {
       System.out.println("Student selected: "+login+".");
       loggedin = true;
    }
    else { 
       System.out.println("Incorrect student name! Please try again.");
       loggedin = false;
    }

} while ( ! loggedin);

if (login.equals(names[0])) {
    System.out.println("Attend today: "+attendance[0]);
    System.out.println("Grade: ");
}   
else {
    System.out.println("poo");
}   

    }
}

正確名稱的輸出為;

“插入學生的姓氏,后跟名字的首字母(例如John Smith = smithj):mckeownl選擇的學生:mckeownl。”

為什么最終的if語句不輸出?

您應該在循環內要求登錄。

如果loggedin變量失敗,則不應將其設置為false。 只需輸入失敗的文本,然后它將返回頂部並再次要求登錄。

方法調用中可以包含多行。 因此,您可以:

System.out.println("Insert student's surname followed by the first letter " + 
"of their first name (e.g John Smith = smithj): ");

代替:

System.out.println("Insert student's surname followed by the first letter");
System.out.print("of their first name (e.g John Smith = smithj): ");

放置login = user_input.next(); 在循環中。為了使用戶再次loggedin

do {   // beginning of while - login

System.out.println("Insert student's surname followed by the first letter");
System.out.print("of their first name (e.g John Smith = smithj): ")

 login = user_input.next(); // <--- you should ask for login here
...

更新:

//for the final if statement
if (loggedin) { //just use this boolean variable since you used it as an indicator if it is valid name or not
    System.out.println("Attend today: "+attendance[0]);
    System.out.println("Grade: ");
}   
else {
    System.out.println("poo");
}

實際上,您不需要在循環后放置條件,因為您已經在循環內對其進行了過濾,因此,如果名稱無效,則在用戶輸入有效名稱之前,它不會退出循環。循環后

do{
..
}while(..)

System.out.println("Attend today: "+attendance[0]);
System.out.println("Grade: ");

我從頭開始,並創建了它,它使用相同的原理,但是解決了我的答案。

有三個用戶,每個用戶都有自己的登錄名,密碼創建和密碼輸入(如果您有更好的方法,請說一下)。

package codeyom9a;

import java.util.Scanner;
public class Codeyom9a {

public static void main(String[] args) {

String[] names = {"Luke", "Jack", "Brad" };
String[] surnames = {"Mckeown", "Heard", "Reed" };


Scanner user_input = new Scanner(System.in);

boolean firstloggedin;
boolean passloggedin;



String firstlogin;

do {                                                                        //login first name
    firstloggedin = false;
    System.out.print("Enter your first name: ");
    firstlogin = user_input.next();

    if (firstlogin.equals(names[0]) || firstlogin.equals(names[1]) || firstlogin.equals(names[2])) {
       firstloggedin = true;

    }
    else {
        System.out.println("Please try again.");
    }

} while (! firstloggedin);

 boolean secondloggedin;


String secondlogin;

do {                                                                        //login surname
    secondloggedin = false;
    System.out.print("Enter your surname: ");
    secondlogin = user_input.next();

    if (secondlogin.equals(surnames[0]) & firstlogin.equals(names[0])|| secondlogin.equals(surnames[1]) & firstlogin.equals(names[1]) || secondlogin.equals(surnames[2]) & firstlogin.equals(names[2])) {
       secondloggedin = true;

    }
    else {
        System.out.println("Please try again.");
    }

} while (! secondloggedin);  

if (secondlogin.equals(surnames[0]) & firstlogin.equals(names[0])) {        //pass login user 1

    String password1;                                                       //pass create user 1
    System.out.print("Create a password (no spaces): ");
    password1 = user_input.next();

    boolean passloggedin1 = false;

    do{ 

        String passwordenter1;                                              //pass enter user 1
    System.out.print("Enter your password now: ");
    passwordenter1 = user_input.next();

    if (passwordenter1.equals(password1)) {
        passloggedin1 = true;
        System.out.println("Correct! You have now logged in.");
    }
    else {
        System.out.println("Incorrect password!");
    }
    } while (! passloggedin1);


}                                                                           //end user 1


if (secondlogin.equals(surnames[1]) & firstlogin.equals(names[1])) {        //pass login user 2

    String password2;                                                       //pass create user 2
    System.out.print("Create a password (no spaces): ");
    password2 = user_input.next();

    boolean passloggedin2 = false;

    do{ 

        String passwordenter2;                                              //pass enter user 2
    System.out.print("Enter your password now: ");
    passwordenter2 = user_input.next();

    if (passwordenter2.equals(password2)) {
        passloggedin2 = true;
        System.out.println("Correct! You have now logged in.");
    }
    else {
        System.out.println("Incorrect password!");
    }
    } while (! passloggedin2);


}                                                                           //end user 2

if (secondlogin.equals(surnames[2]) & firstlogin.equals(names[2])) {        //pass login user 3

    String password3;                                                       //pass create user 3
    System.out.print("Create a password (no spaces): ");
    password3 = user_input.next();

    boolean passloggedin3 = false;

    do{ 

        String passwordenter3;                                              //pass enter user 3
    System.out.print("Enter your password now: ");
    passwordenter3 = user_input.next();

    if (passwordenter3.equals(password3)) {
        passloggedin3 = true;
        System.out.println("Correct! You have now logged in.");
    }
    else {
        System.out.println("Incorrect password!");
    }
    } while (! passloggedin3);


}                                                                           //end user 3

    }
}

如果鍵入“ Luke”,“ Jack”或“ Brad”,則它將要求輸入姓氏(該姓氏在“姓氏”數組中位於相同的索引中)。 如果兩者都正確,它將要求創建密碼,然后要求用戶輸入創建的密碼。

關於我的第一個代碼,我不知道為什么這樣行得通,而另一個不行,為什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM