如何在while循环中添加多个用户提示?

时间:2014-11-28 13:18:09

标签: java loops if-statement while-loop user-input

我必须编写一个程序,询问用户的姓名,地址和电话号码。输入数据后,程序将打印数据并要求用户输入yes或no来验证数据。重复此过程直到用户满意并对问题回答“是”。

现在,此时我可以弹出一个提示符(在我的情况下只询问用户的名字)。但是,如果我想添加多个问题(即询问地址和电话号码)并发生相同的事情呢?我怎么能这样做?

我的代码:

    package userinfo;

import java.util.Scanner;
import sun.security.krb5.SCDynamicStoreConfig;

public class UserInfo {

    public static void main(String[] args) {

        String varify;
        String yes = "yes";
        String no = "no";

        Scanner input = new Scanner(System.in);
        System.out.println("Enter your name: ");
        String name = input.next();
        System.out.println("Your input was: "+name);
        System.out.println("Varify by yes or no: ");

        while (true) {
            varify = input.next();

            if (varify.equalsIgnoreCase(yes)) {
                System.out.println("Varified! Your name is: " + name);

            } else if (varify.equalsIgnoreCase(no)) {
                System.out.println("Type your name again: ");

            }

        }

    }

}

2 个答案:

答案 0 :(得分:1)

编辑为更多问题添加了逻辑...以类似的方式扩展它以满足您的所有需求。您可以将此代码扩展为单个方法,以避免代码复制。查看用户alaster的答案以获取示例。

试试这个。它将存储name变量,以防您想进一步使用它。

我们使用boolean继续要求用户输入他的名字,直到他验证它为止。

当然,如果名称有效,您仍然可以使用while(true)然后使用break,但我更喜欢这种方法,因为代码更清晰,更易于理解。

private static boolean isVerified(String verify) {
    if (verify.equalsIgnoreCase("yes")) {
        return true;
    } else if (verify.equalsIgnoreCase("no")) {
        return false;
    } else
        return false;
}

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    boolean validName = false;
    boolean validTelephoneNo = false;
    boolean validAddress = false;
    String name="";
    String telephoneNo="";
    String address="";

    while (!validName) {
        System.out.print("Enter your name: ");
        name = input.next();

        System.out.println("Are you sure your name is " + name + "?");
        final String verify = input.next();

        if (isVerified(verify)) {
            System.out.println("Verified! Your name is: " + name);
            validName = true;

        } else {
            System.out.println("Not verified! Please type your name again.");
        }
    }

    while (!validTelephoneNo) {
        System.out.print("Enter your telephone nummber: ");
        telephoneNo = input.next();

        System.out.println("Are you sure your telephone number is " + telephoneNo + "?");
        final String verify = input.next();

        if (isVerified(verify)) {
            System.out.println("Verified! Your telephone number is: " + telephoneNo);
            validTelephoneNo = true;
        }

        else {
            System.out.println("Not verified! Please type your telephone number again.");
        }
    }

    while (!validAddress) {
        System.out.print("Enter your address: ");
        address = input.next();

        System.out.println("Are you sure your address is " + address + "?");
        final String verify = input.next();

        if (isVerified(verify)) {
            System.out.println("Verified! Your address is: " + address);
            validAddress = true;
        }

        else {
            System.out.println("Not verified! Please type your address again.");
        }
    }

    System.out.println("Done, here is your info:");
    System.out.println("Name: " + name);
    System.out.println("Telephone Number: "+telephoneNo);
    System.out.println("Address: "+address);
}

答案 1 :(得分:1)

您可以将此代码提取到方法:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String userName = readFieldAndVerify(input, "Enter your name: ");
    String userAddress = readFieldAndVerify(input, "Enter your address: ");
    String userPhoneNumber = readFieldAndVerify(input, "Enter your phone number: ");
}

private static String readFieldAndVerify(Scanner input, String prompt) {
    while (true) {
        System.out.print(prompt);
        String field = input.next();

        System.out.println("Are you sure (yes / no)?");
        String verify = input.next();

        if (verify.equalsIgnoreCase("yes")) {
            System.out.println("Verified!");
            return field;
        } else {
            System.out.println("Canceled");
        }
    }
}