【问题标题】:Assigning and returning objects in javajava中对象的赋值和返回
【发布时间】:2016-07-26 23:16:54
【问题描述】:

我正在尝试将 temp 数组中的当前数组元素分配给调用 getStudent 方法后返回的 Student 对象...。我调用了 getStudent 方法(步骤 2)并让 temp[i] = to分配临时数组中的当前元素,但无法弄清楚它应该与返回的学生对象配对。使用 getStudent() 并运行程序时,输出是输入学生人数,用户输入数字,就是这样,它不要求用户输入姓名等,我不是确定是第 2 步的问题还是完全有其他问题。

import java.util.Scanner;

public class Students
{
  private static Scanner input = new Scanner(System.in);

  public static void main(String[] args)
  {
    Student[] students;

    students = getStudents();
    printStudents(students);
  }

  private static Student[] getStudents()
  {
    Student[] temp;
    int       how_many;

    System.out.print("How many students? ");
    how_many = input.nextInt();
    purgeInputBuffer();
    temp =  new Student[input.nextInt()];  // Step 1 ???
    for (int i = 0; i < temp.length; i++)
    {
      getStudent();         // Step 2
      temp[i] =      ;    //  <----------    
    }
    return temp;    // Step 3
  }

  private static Student getStudent()
 {
    String name,
        address,
          major;
     double gpa;

    System.out.print("Enter name: ");
    name = input.nextLine();
    System.out.print("Enter address: ");
    address = input.nextLine();
    System.out.print("Enter major: ");
    major = input.nextLine();
    System.out.print("Enter GPA: ");
    gpa = input.nextDouble();
    purgeInputBuffer();

    return new Student (name, address, major, gpa);     // Step 4
  }

  private static void printStudents(Student[] s)
  {
    System.out.println();
    for (int i = 0; i < s.length; i++)    // Step 5
    {
      System.out.println(getStudent());     // Step 6
    }
  }

  private static void purgeInputBuffer()
  {
    // ----------------------------------------------------
    // Purge input buffer by reading and ignoring remaining
    // characters in input buffer including the newline
    // ----------------------------------------------------
    input.nextLine();
  }
}

【问题讨论】:

  • 为什么不使用这个:temp[i] = getStudent();// Step 2
  • 当使用它并运行程序时,输出是输入学生人数,用户输入数字,就是这样,它不要求用户输入姓名和等
  • 我很确定@Cootri 所说的是你应该做的。但也请务必查看您的private static void printStudents(Student[] s) 方法,并准确地在线//step 6 我不相信这就是您想要这样做的方式。相反,您希望 System.out.println(s[i]); 而不是 System.out.println(getStudent()); 要我的代码替换工作,尽管您需要覆盖 toString 方法,以便它可以准确地显示信息
  • 您的代码无法正常工作,因为该行中的 temp = new Student[input.nextInt()]; 行您已经向用户询问了这里有多少学生的整数 how_many = input.nextInt(); 所以您应该简单地执行 @987654332 @

标签: java object return assign


【解决方案1】:

所以第一个问题首先上线:

temp = new Student[input.nextInt()];

在该行中,您已经要求用户输入学生人数并将其存储在how_many 中。所以我假设你想改为:

temp = new Student[how_many];

也是我在评论中所说的:

但也请查看您的private static void printStudents(Student[] s) 方法,并准确地说是//第 6 步,我不相信这就是您想要这样做的方式。相反,您希望 System.out.println(s[i]); 而不是 System.out.println(getStudent()); 为了我的代码替换工作,尽管您需要重写 toString 方法,以便它可以实际显示信息

【讨论】:

  • 这确实允许程序继续,但在输入了多少数字之后继续询问问题
  • @Brent 你有没有改变我提到的第二件事
  • 是的,我改变了第二件事....一定是for语句的问题?不完全确定
  • @Brent 你的 for 循环看起来是正确的。学生课堂上是否有您要求提供信息的内容,如果有,您应该删除它。
  • 我加了一张结果图是@3kings
猜你喜欢
  • 2019-02-14
  • 2012-06-16
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
相关资源
最近更新 更多