简体   繁体   English

打印所有学生记录时,如何使我的程序恢复正常?

[英]How can I get my program back on track when printing out all student records?

 import java.util.Scanner; public class MainBinaryTreeArray { public static void main(String[] args) { int choice; Scanner scan= new Scanner(System.in); BinaryTreeArray data = new BinaryTreeArray(); Listing l1 = new Listing("Carol", 4354, 3.2); Listing l2 = new Listing("Timothy", 2353, 4.0); Listing l3 = new Listing("Dean", 4544, 2.4); Listing l4 = new Listing("Sue", 3445, 3.0); data.insert(l1); data.insert(l2); data.insert(l3); data.insert(l4); do { // Choose which operation by entering a number System.out.println("*****************(Menu Operations:)******************"); System.out.println(" (Press 1). Insert."); System.out.println(" (Press 2). Fetch."); System.out.println(" (Press 5). Output all student records."); System.out.println(" (Press 6). Exit the program."); System.out.println("Enter your choice: "); choice = scan.nextInt(); switch(choice) { case 1: System.out.println("Are students inserted: " + data.insert(l1)); break; case 2: System.out.println("The student's info that's fetched: "); System.out.print(data.fetch("Timothy")); break; case 5: System.out.print("Output all the student's records: "); data.showAll(); } }while(choice!=6); } } 

 public class BinaryTreeArray { private Listing[] data; private int size; public BinaryTreeArray() { size = 100; data = new Listing[size]; } public void showAll() { for(int i=0; i<size; i++) System.out.print(data[i] + " "); } public boolean insert(Listing newListing) { int i = 0; while(i < size && data[i]!= null) { if(data[i].getKey().compareTo(newListing.getKey()) > 0) i = 2 * i + 1; else i = 2 * i + 2; } if(i >= size) return false; else { data[i] = newListing.deepCopy(); return true; } } public Listing fetch(String targetKey) { int i= 0; while(i< size && data[i]!= null && data[i].getKey()!=targetKey) { if(data[i].getKey().compareTo(targetKey) > 0) i = 2 * i + 1; else i = 2 * i + 2; } if(i >= size || data[i] == null) return null; else return data[i].deepCopy(); } } 

 public class Listing implements Comparable<Listing> { private String name; // key field private int ID; private double GPA; public Listing(String n, int id, double gpa) { name = n; ID = id; GPA = gpa; } public String toString() { return("Name is " + " " + name + "\\nID is" + " " + ID + "\\nGPA is" + " " + GPA + "\\n"); } public Listing deepCopy() { Listing clone = new Listing(name, ID, GPA); return clone; } public int compareTo(Listing other) { return this.name.compareTo(other.getKey()); } public String getKey() { return name; } }// end of class Listing 

Hello All, 大家好,

My java program compiles fine, but I am having a terrible and miserable time with getting my program to stop printing all those nulls when I output all student records in my BinaryTreeArray. 我的java程序可以很好地编译,但是当我在BinaryTreeArray中输出所有学生记录时,让程序停止打印所有这些null时,我经历了一个可怕而痛苦的时刻 Here is complete program. 这是完整的程序。 Any suggestions? 有什么建议么? Please do give any advice. 请提供任何建议。 So to make what I am saying clear, I need help with understanding why when I print out student records it includes a whole bunch of extra nulls that really have no purpose and just make my program look crazy. 因此,为了使我的发言更清楚,我需要帮助理解为什么在打印学生记录时,它会包含一堆额外的空值,这些空值实际上是没有目的的,只会使我的程序看起来很疯狂。 Any solutions to this problem? 这个问题有解决方案吗?

When you initialize the BinaryTreeArray(), you set the field variable "size" to 100 and use this to initialize the data Listing[] array. 初始化BinaryTreeArray()时,将字段变量“ size”设置为100,然后使用它来初始化数据Listing []数组。 When you print out the data, the loop uses the same "size" variable so you get all 100 entries, including the null entries. 当您打印出数据时,循环使用相同的“ size”变量,因此您将获得所有100个条目,包括空条目。 A simple solution would be to filter the data for null when you show all. 一种简单的解决方案是在显示全部内容时将数据过滤为空。

public class BinaryTreeArray
{ 

  ...

  public void showAll()
  { 
      for(int i=0; i<size; i++)
      {
          if (data[i] != null)
              System.out.print(data[i] + " "); 
      }
  } 

  ...

}

An alternate solution would be to change your use of size to maxSize and then create a variable size that is incremented as you insert listings. 另一种解决方案是将size的使用更改为maxSize,然后创建一个可变大小,该大小在插入列表时会增加。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得我的学生成绩计算器程序中类似成绩字母的总数 - how can i get the total number of similar grade letters in my student grade calculator program 我无法弄清楚为什么我的程序在运行时没有打印 - I can't figure out why my program isn't printing when I run it 如何设计我的 Spring 程序,以便当用户登录时,我可以判断他们是教职员工、学生还是教职员工? - How can I design my Spring program so that when a user logs in I can tell if they are a Staff member, student or faculty member? 为什么不打印我的学生字符串? - Why arent my Student strings printing out? 如何让我的程序从 do while 循环中打印出我的所有输入? - How do I get my program to print out all of my input from a do while loop? 如何在SQL中使用存储过程从所有表中获取所有记录 - How can i get all records from all tables using stored procedure in my sql 为什么我不能将组件从JFrame中取出? - Why can I not get my components back out of JFrame? 如何获得程序以将每个字符打印为“字符1 :(字符),字符2 :(字符)等”? - How can I get my program to print out each character as “Character #1: (character) , Character #2: (character), etc”? 无法弄清楚为什么我的程序不打印数组内容 - Can't figure out why my program is not printing contents of array 我怎样才能打印程序? 尝试访问.toString()时收到NullPointerException - How can I get my program to print? I get a NullPointerException when I try to access .toString()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM