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

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

I can not increase the studentid value in For

问题

我正在使用Java进行一些编码,但它不起作用:

  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6. String[] studentId = new String[3];
  7. Student name;
  8. for (int i = 0; i < 3; i++) {
  9. System.out.print("输入姓名 => ");
  10. name = new Student(input.next());
  11. studentId[i] = name.toString();
  12. }
  13. for (int i = 0; i < 3; i++) {
  14. System.out.println(studentId[i]);
  15. }
  16. }
  17. }
  18. public class Student {
  19. private static int studentId;
  20. private String name;
  21. public Student(String name) {
  22. this.name = name;
  23. setStudentId(studentId++);
  24. }
  25. public int getStudentId() {
  26. return studentId;
  27. }
  28. public void setStudentId(int studentId) {
  29. this.studentId = studentId;
  30. }
  31. @Override
  32. public String toString() {
  33. return (this.studentId + " " + "学生是" + " " + "(" + this.studentId + ")" + this.name);
  34. }
  35. }

我需要在创建新条目时自动增加ID。
我尝试了各种方法,但仍然无法使其递增。

英文:

I am doing some coding in Java, but it doesn't work:

import java.util.Scanner;

import java.util.Arrays;

public class Main
{

  1. public static void main(String[] args)
  2. {
  3. Scanner input = new Scanner(System.in);
  4. String [] studentId = new String [3];
  5. Student name;
  6. for (int i = 0; i &lt; 3; i++)
  7. {
  8. System.out.print(&quot;Enter Name =&gt; &quot;);
  9. name = new Student(input.next());
  10. studentId[i] = name.toString();
  11. }
  12. for (int i = 0; i &lt; 3; i++)
  13. {
  14. System.out.println(studentId[i]);
  15. }
  16. }
  17. }
  1. public class Student
  2. {
  3. private static int studentId;
  4. private String name;
  5. public Student(String name)
  6. {
  7. this.name=name;
  8. setStudentId(studentId++);
  9. }
  10. public int getStudentId()
  11. {
  12. return studentId;
  13. }
  14. public void setStudentId(int studentId)
  15. {
  16. this.studentId = studentId;
  17. }
  18. @Override
  19. public String toString()
  20. {
  21. return (this.studentId +&quot; &quot;+ &quot;student is &quot;+ &quot; &quot; + &quot;(&quot; + this.studentId + &quot;)&quot;+ this.name);
  22. }
  23. }

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。

  1. public class Student
  2. {
  3. private static int nextAvailableStudentId = 1;
  4. private String name;
  5. private int studentId;
  6. public Student(String name)
  7. {
  8. this.name = name;
  9. setStudentId(nextAvailableStudentId++);
  10. }
  11. public int getStudentId()
  12. {
  13. return studentId;
  14. }
  15. public void setStudentId(int studentId)
  16. {
  17. this.studentId = studentId;
  18. }
  19. @Override
  20. public String toString()
  21. {
  22. return (this.studentId + " " + "student is " + " " + "(" + this.studentId + ")" + this.name);
  23. }
  24. }
英文:

You are using a static variable for everyone's Ids. You need a separate, non-static field to store each individual Id.

  1. public class Student
  2. {
  3. private static int nextAvailableStudentId = 1;
  4. private String name;
  5. private int studentId;
  6. public Student(String name)
  7. {
  8. this.name=name;
  9. setStudentId(nextAvailableStudentId++);
  10. }
  11. public int getStudentId()
  12. {
  13. return studentId;
  14. }
  15. public void setStudentId(int studentId)
  16. {
  17. this.studentId = studentId;
  18. }
  19. @Override
  20. public String toString()
  21. {
  22. return (this.studentId +&quot; &quot;+ &quot;student is &quot;+ &quot; &quot; + &quot;(&quot; + this.studentId + &quot;)&quot;+ this.name);
  23. }
  24. }

答案2

得分: -1

在构造函数中应该是:

  1. {
  2. public Student(String name)
  3. {
  4. this.name=name;
  5. setStudentId(studentId + 1);
  6. }
  7. }
英文:

In constructor it should be:

  1. {
  2. public Student(String name)
  3. {
  4. this.name=name;
  5. setStudentId(studentId + 1);
  6. }
  7. }

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:

确定