英文:
I can not increase the studentid value in For
问题
我正在使用Java进行一些编码,但它不起作用:
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] studentId = new String[3];
Student name;
for (int i = 0; i < 3; i++) {
System.out.print("输入姓名 => ");
name = new Student(input.next());
studentId[i] = name.toString();
}
for (int i = 0; i < 3; i++) {
System.out.println(studentId[i]);
}
}
}
public class Student {
private static int studentId;
private String name;
public Student(String name) {
this.name = name;
setStudentId(studentId++);
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
@Override
public String toString() {
return (this.studentId + " " + "学生是" + " " + "(" + this.studentId + ")" + this.name);
}
}
我需要在创建新条目时自动增加ID。
我尝试了各种方法,但仍然无法使其递增。
英文:
I am doing some coding in Java, but it doesn't work:
import java.util.Scanner;
import java.util.Arrays;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String [] studentId = new String [3];
Student name;
for (int i = 0; i < 3; i++)
{
System.out.print("Enter Name => ");
name = new Student(input.next());
studentId[i] = name.toString();
}
for (int i = 0; i < 3; i++)
{
System.out.println(studentId[i]);
}
}
}
public class Student
{
private static int studentId;
private String name;
public Student(String name)
{
this.name=name;
setStudentId(studentId++);
}
public int getStudentId()
{
return studentId;
}
public void setStudentId(int studentId)
{
this.studentId = studentId;
}
@Override
public String toString()
{
return (this.studentId +" "+ "student is "+ " " + "(" + this.studentId + ")"+ this.name);
}
}
I need to auto-increment the Id when a new entry is created.
I try everything but still cant increased it.
答案1
得分: 1
你正在为所有人的ID使用静态变量。你需要一个单独的、非静态字段来存储每个个体的ID。
public class Student
{
private static int nextAvailableStudentId = 1;
private String name;
private int studentId;
public Student(String name)
{
this.name = name;
setStudentId(nextAvailableStudentId++);
}
public int getStudentId()
{
return studentId;
}
public void setStudentId(int studentId)
{
this.studentId = studentId;
}
@Override
public String toString()
{
return (this.studentId + " " + "student is " + " " + "(" + this.studentId + ")" + this.name);
}
}
英文:
You are using a static variable for everyone's Ids. You need a separate, non-static field to store each individual Id.
public class Student
{
private static int nextAvailableStudentId = 1;
private String name;
private int studentId;
public Student(String name)
{
this.name=name;
setStudentId(nextAvailableStudentId++);
}
public int getStudentId()
{
return studentId;
}
public void setStudentId(int studentId)
{
this.studentId = studentId;
}
@Override
public String toString()
{
return (this.studentId +" "+ "student is "+ " " + "(" + this.studentId + ")"+ this.name);
}
}
答案2
得分: -1
在构造函数中应该是:
{
public Student(String name)
{
this.name=name;
setStudentId(studentId + 1);
}
}
英文:
In constructor it should be:
{
public Student(String name)
{
this.name=name;
setStudentId(studentId + 1);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论