我无法在循环中增加studentid的值。

huangapple go评论91阅读模式
英文:

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 &lt; 3; i++) 
{
System.out.print(&quot;Enter Name =&gt; &quot;);
name = new Student(input.next());
studentId[i] = name.toString();
}
for (int i = 0; i &lt; 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 +&quot; &quot;+ &quot;student is &quot;+ &quot; &quot; + &quot;(&quot; + this.studentId + &quot;)&quot;+ 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 +&quot; &quot;+ &quot;student is &quot;+ &quot; &quot; + &quot;(&quot; + this.studentId + &quot;)&quot;+ 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);
}
}

huangapple
  • 本文由 发表于 2020年4月4日 01:30:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61017321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定