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



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

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

我的代码:

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

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

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

编辑 为更多问题添加了逻辑...以类似的方式扩展它,以满足您需要的一切。您也可以将此代码扩展到单个方法中,以避免代码复制。以用户身份为例查看答案。

试试这个。它将存储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);
}

相关内容

  • 没有找到相关文章

最新更新