在do-while循环之后代码不会继续?

时间:2015-01-31 01:21:13

标签: java

我已经开始在我的AS级计算课程中学习Java,并且已经完成了我们设置的第一个DIY任务。

我使用了do-while语句来查看用户的输入用户名是否在数组“names”中 - 如果不是,则请求重新输入用户名,直到插入正确的用户名为止。我还设置了一个布尔值,所以当输入正确的用户名时,它会取消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语句输出?

3 个答案:

答案 0 :(得分:0)

你应该要求在循环中登录。

如果失败,您不应将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): ");

答案 1 :(得分:0)

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: ");

答案 2 :(得分:0)

我从零开始,创造了这个,它使用相同的原则,但已经解决了我的答案。

有三个用户,每个用户都有自己的登录名,密码创建和密码输入(如果你有更好的方法,请说)。

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

    }
}

如果您输入&#34; Luke&#34;,&#34; Jack&#34;或&#34; Brad&#34;,则它会请求姓氏(在同一个索引中) &#39;姓氏&#39;阵列)。如果两者都正确,则请求创建密码,然后要求用户输入创建的密码。

关于我的第一个代码,我不知道为什么这个有用而另一个没有,任何想法为什么?