一个带有“如果”条件的“do-while循环”?

huangapple go评论64阅读模式
英文:

An "If" condition with a "do-while loop"?

问题

我有这个方法,它要求用户输入多个值,并根据这些4个值创建一个对象。不允许输入空格或只按回车键,问题应该一直循环,直到满足条件。

此外,每次不接受输入时,应打印出消息“错误:字段不能为空”。我的do-while循环似乎正常工作,只是我不知道在哪里实现错误消息,以使其在正确的条件下出现?

提前感谢。

public void registerDog() {
    String name = null;
    do {
        System.out.print("Name? > ");
        name = input.nextLine();
        if (name.trim().isEmpty()) {
            System.out.println("Error: field cannot be empty");
        }
    } while (name.trim().isEmpty());
    name = name.substring(0,1).toUpperCase() + name.substring(1).toLowerCase();

    System.out.print("Breed? > ");
    String breed = input.nextLine();
    breed = breed.substring(0,1).toUpperCase() + breed.substring(1).toLowerCase();

    System.out.print("Weight? > ");
    int weight = input.nextInt();

    System.out.print("Age? > ");
    int age = input.nextInt();

    Dog dog = new Dog(name, breed, age, weight);
    doggoList.add(dog);
    System.out.println("\n" + dog.getName() + " has been added to the register.\n" + dog.toString());
}
英文:

I have this method which asks the user for multiple inputs and based on these 4 values an object is created at the end.
Entering whitespaces or just hitting return should not be allowed and the question should loop until the conditions are met.

Also, each time an input is not accepted, the message "Error: field cannot be empty" should be printed out. My do-while loop seems to be working correctly except that I don't know where to implement my error message to make it appear under the right condition?

Thanks in advance.

public void registerDog() {
	String name = null;
	do {
		System.out.print("Name?> ");
		name = input.nextLine();
	} while (name.trim().isEmpty());
	name = name.substring(0,1).toUpperCase() + name.substring(1).toLowerCase();
	
	System.out.print("Breed?> ");
	String breed = input.nextLine();
	breed = breed.substring(0,1).toUpperCase() + breed.substring(1).toLowerCase();

	System.out.print("Weight?> ");
	int weight = input.nextInt();

	System.out.print("Age?> ");
	int age = input.nextInt();

	Dog dog = new Dog(name, breed, age, weight);
	doggoList.add(dog);
	System.out.println("\n" + dog.getName() + " has been added to the register.\n" + dog.toString());
}

答案1

得分: 1

你是否有坚持使用do-while循环的理由?我建议放弃do-while,改用if语句。可以这样做:

String name;

while (true) {
    System.out.print("Name? > ");
    name = input.nextLine();

    if (!name.trim().isEmpty()) {
        break;
    }

    System.out.println("%nError: field cannot be empty");
}

你还可以通过将这段代码封装成一个方法,每次需要从用户那里获取值时调用该方法,而不是为每个值都重写相同的代码。该方法可能如下所示:

public static String getValueFromUser(String prompt) {
    String value;

    while (true) {
        System.out.print(prompt);
        value = input.nextLine();

        if (!value.trim().isEmpty()) {
            return value;
        }

        System.out.println("%nError: field cannot be empty");
    }
}
英文:

Is there any reason you're stuck to using a do-while loop? I would just ditch the do-while, and use an if statement instead. Something like this:

String name;

while (true) {
    System.out.print("Name?> ");
    name = input.nextLine();

    if (!name.trim().isEmpty()) {
        break;
    }

    System.out.println("%nError: field cannot be empty");
}

You can also simplify your code by making this into a method, and calling it each time you need a value from the user, rather than rewriting the same code for each value. The method might look something like this:

public static String getValueFromUser(String prompt) {
    String value;

    while (true) {
        System.out.print(prompt);
        value = input.nextLine();

        if (!value.trim().isEmpty()) {
            return value;
        }

        System.out.println("%nError: field cannot be empty");
    }
}

huangapple
  • 本文由 发表于 2020年8月6日 06:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63274600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定