下面的代码询问您要回答的名称数量,然后告诉您输入您拥有的所有名称。我想要它,以便它将名称打印成两列,以便名称配对在一起。
Scanner scan = new Scanner(System.in);
System.out.println("How many names would you like to enter?");
int n = scan.nextInt(); //Ensures you get an integer
System.out.println("Enter the " + n + " names: ");
int N=n+1;
String[] names = new String[N];
//Store the names in an array
for (int i = 0; i < names.length; i++){
names[i] = scan.nextLine();
}
//Prints the names in a list
List<String> AllNames;
AllNames = new ArrayList<>(Arrays.asList(names));
Random rand = new Random();
Collections.shuffle(AllNames, rand);
System.out.println(AllNames);
}}
答案 0 :(得分:0)
这样的东西?
String[] names = {"pete", "ryan", "bob", "cynthia", "ulga", "sarah", "madison"};
for(int i = 0; i < names.length; i = i + 2)
{
System.out.print(names[i]);
if(i + 1 < names.length)
{
System.out.print("\t" + names[i + 1]);
}
System.out.println();
}
输出
pete ryan
bob cynthia
ulga sarah
madison
输出2
String[] names = {"pete", "ryan", "bob", "cynthia"};
生成以下内容
pete ryan
bob cynthia
答案 1 :(得分:0)
@RAZ_Muh_Taz 这就是我到目前为止所做的:
`Scanner scan = new Scanner(System.in);
System.out.println("How many names would you like to enter?");
int n= scan.nextInt(); //Ensures you take an integer
System.out.println("Enter the " + n + " names: ");
String[] names = new String[n];
// Store the names in an array
for (int i = 0; i < names.length; i=i+1){
names[i] = scan.nextLine();}
List<String> AllNames;
// Prints the names in a list
AllNames = new ArrayList<>(Arrays.asList(names));
for(int i = 0; i < names.length; i = i + 2) {
System.out.print(names[i]);
if(i + 1 < names.length)
{
System.out.print(" " +"&"+ " " + names[i + 1] );
}
System.out.println();
}
Random rand = new Random();
Collections.shuffle(AllNames, rand);
scan.close();
}}`
我总是得到的输出是:
` & a
b & c`