【发布时间】:2018-11-15 16:06:30
【问题描述】:
我需要从每个学生那里获得唯一的卷号。
package test;
import java.util.Scanner;
public class Studentform {
public static void main(String[] args) {
int length = 3;
Studb[] studs = new Studb[length];
for (int i = 0; i < length; i++) {
for (Studb s : studs) {
readStudent(i);
}
}
}
static void readStudent(int i) {
int rollno = 1001;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:");
String name = sc.next();
System.out.println("Enter your age:");
int age = sc.nextInt();
Studb stud = new Studb(name, age, rollno++);
System.out.println(stud);
}
}
【问题讨论】:
-
生成的方式有很多种——AtomicInteger、UUID等,如果是int,可以使用AtomicInteger
-
你已经在循环使用
int i,你已经传入了它。只需使用它,它最终会是独一无二的。 -
我只想打印具有唯一卷号的扫描值
-
使用 AtomicInteger docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/…
-
为什么会有多余的 for 循环?
标签: java arrays function class object