public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
for (i= 1; i <= n; i++) {
String g = a + i;
System.out.println(g);
}
}这是我的节目。它获取课堂的用户输入,并打印学生的学籍。
例如:如果这个类是10A,而学生数是10,它会打印出一个像10A1、10A2、10A3…10A10这样的系列
如何使程序将这些元素存储在数组中?
例如:
array[0] = 10A1;
array[1] = 10A2;
array[2] = 10A3; 等。
发布于 2014-11-13 16:31:33
您的代码应该如下所示:
public static void main (String args[])
{
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
String []strings = new String[n]; // Creating an are of string with the given number
for(i= 0; i < n ;){
strings[i] = a + ++i; // Storing strings on to the array !
System.out.println(strings[i-1]);
}
}发布于 2014-11-13 16:32:30
您只需编辑当前for循环中的每个索引:
String[] arr;
for(i=0; i < n ; i++){
int j = i+1;
String g = a + j;
System.out.println(g);
arr[i] = g;
}因此,所有打印的g都将是数组arr的一部分。
发布于 2014-11-13 16:31:09
首先,声明一个大小适当的String数组。
其次,在for循环中,将当前正在打印的字符串分配到数组中的位置。
String[] things = new String[n];
for (i=1; i <= n; i++) {
String g = a + i;
System.out.println(g);
things[i-1] = g;
}字符串现在在数组中。
https://stackoverflow.com/questions/26913572
复制相似问题