如何一次打印输出而不是一次打印 - JAVA

时间:2016-11-29 09:32:50

标签: java

IllegalArgumentException

上面的代码按照我的预期工作,但我需要更改最终打印输出的内容。我想要的是在输入所有细节后立即打印输出。

3 个答案:

答案 0 :(得分:3)

使用StringBuilder构建输出:

StringBuilder sb = new StringBuilder();
sb.append("Thank you for registering.\n");
sb.append("Please verify your data:\n");
//...

System.out.println(sb.toString());

答案 1 :(得分:1)

您可以使用以下代码根据您的代码将输出作为单个字符串输出。在这里,我使用了StringBuilder并将相关输出附加到它。然后在最后我打印出它的内容。

import java.util.Scanner;

public class Validation {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String regex = "^[a-zA-Z ]+$";
    String regex1 = "\\d[0-9]|[1-9]";
    String regex2 = "^[a-zA-Z0-9 ]+$";
    String Char;
    String num;
    String chars;
    System.out.println("WELCOME TO Mixed Martial Art's CLUB REGISTRATION");

    StringBuilder output = new StringBuilder();

    do {

        System.out.print("\nWhat Is Your Name:");
        Char = input.nextLine();

        if (Char.matches(regex))
            // System.out.println("\nWelcome:"+" "+Char);
            output.append("\nWelcome:" + " " + Char);
        else if (Char.isEmpty())
            System.out.println("String field should not be Empty.");
        else if (!Char.matches(regex))
            System.out.println("Please Enter A Valid String!");
    } while (!Char.matches(regex));

    do {

        System.out.print("\nHow Old Are you:");
        num = input.nextLine();
        if (num.isEmpty())
            System.out.println("Number field should not be Empty.");
        else if (!num.matches(regex1)) {
            System.out.println("Please Enter A Valid Number!");
        } else {
            if (Integer.parseInt(num) <= 18) {
                // System.out.println("Sorry But You Are Underage.");
                output.append("\nSorry But You Are Underage.");

            } else {
                if (Integer.parseInt(num) <= 39) {
                    // System.out.println("You Are Qualified");
                    output.append("\nYou Are Qualified");
                } else {
                    // System.out.println("Opps! You Are OVER AGE!");
                    output.append("\nOpps! You Are OVER AGE!");
                }
            }
        }

    } while (!num.matches(regex1));

    System.out.println(output);

}
}

答案 2 :(得分:0)

import java.util.*;

public class Validation {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String regex = "^[a-zA-Z ]+$";
        String regex1 = "\\d[0-9]|[1-9]";
        String nameChar;
        String num;
        String chars;
        String name = "";
        int age = 0;
        System.out.println("WELCOME TO Mixed Martial Art's CLUB REGISTRATION");

        do {
            System.out.print("\nWhat Is Your Name:");
            nameChar = input.nextLine();
            if (nameChar.isEmpty())
                System.out.println("String field should not be Empty.");
            else if (!nameChar.matches(regex))
                System.out.println("Please Enter A Valid String!");
            else if (nameChar.matches(regex))
                name = nameChar;
        } while (!nameChar.matches(regex));

        do {
            System.out.print("\nHow Old Are you:");
            num = input.nextLine();
            if (num.isEmpty())
                System.out.println("Number field should not be Empty.");
            else if (!num.matches(regex1)) {
                System.out.println("Please Enter A Valid Number!");
            } else {
                try {
                    int ageInput = Integer.parseInt(num);
                    if (ageInput <= 18) {
                        System.out.println("Sorry But You Are Underage.");
                    } else {
                        if (ageInput <= 39) {
                            age = ageInput;
                            break;
                        } else {
                            System.out.println("Opps! You Are OVER AGE!");
                        }
                    }
                } catch (Exception e) {
                }
            }

        } while (!num.matches(regex1));
        if(!name.isEmpty() && age>0){
            System.out.println("Thank you for registering");
            System.out.println("Name is "+name);
            System.out.println("Age is"+age);
            System.out.println("You Are Qualified");
        }

    }
}