0

我正在尝试使用一个 println 设置多个字符串变量。这是我所拥有的:

Scanner scanner = new Scanner(System.in);

String teacher = "";
String classSelect = "";
String location = "";

System.out.println("Enter a teacher: ");
teacher = scanner.next();

System.out.println("Enter a class name: ");
classSelect = scanner.next();

System.out.println("Enter a location: ");
location = scanner.next();

这是我想要完成的事情:

String section = "";
String instructor = "";
String location = "";

System.out.printf("Only fill in applicable criteria, leave the rest blank :\n"
        + "Section://Sets section var// \n"
        + "Instructor://Sets instructor var// \n"
        + "Location://Sets location var// \n");
4

2 回答 2

1

这可能不是一种道德的方式,但试试吧

System.out.printf("Only fill in applicable criteria, leave the rest blank and use | after every field:\n"
        + "Section://Sets section var// \n"
        + "Instructor://Sets instructor var// \n"
        + "Location://Sets location var// \n");

如果用户输入

abc|xyz|loc

在字符串中接受它

String input = scanner.nextLine();
String[] inputs=input.split("|");
inputs[0]//<--section
inputs[1]//<--instructor
inputs[2]//<--location
于 2012-12-06T12:43:19.753 回答
0

您是否正在寻找这个:

   String message = 
        String.format("Only fill in applicable criteria, leave the rest blank :\n"
        + "Section: %s \n"
        + "Instructor: %s \n"
        + "Location:%s \n", 
        section ,instructor,location );

或这个?

    System.out.printf("Only fill in applicable criteria, leave the rest blank :\n"
            + "Section: %s \n"
            + "Instructor: %s \n"
            + "Location:%s \n", 
            section ,instructor,location );
于 2012-12-06T12:30:19.223 回答