如果我输入以下内容:
student #1
id number: 1
name: aaa
course: cs
student#2
id number: 1
name: aaa
course: cs
树形图应该提示按ID号排序。
然后,它应该打印如下:
student id number course
aaa 1 cs
aaa 1 cs
但就我而言,它看起来像这样:
student id number course
aaa 1 cs
这是我的代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.TreeMap;
public class studentreport {
static Scanner j = new Scanner(System.in);
static Scanner a = new Scanner(System.in);
public static void main(String[] args) {
List<String> studentnumberA = new ArrayList<String>();
List<String> studentnameA = new ArrayList<String>();
List<String> courseA = new ArrayList<String>();
List<Double> prelimA = new ArrayList<Double>();
List<Double> midtermA = new ArrayList<Double>();
List<Double> finalsA = new ArrayList<Double>();
List<Double> sumA = new ArrayList<Double>();
double prelim, midterm, finals;
String menu, menu2;
String transaction = null, transaction2 = null, transaction3 = null;
String studentname = null, course = null, studentnumber = null;
do {
System.out.print("\t\t\tDi Natuto University \n\t\t\t " +
"Pililla,Rizal \n\n\t\t\t" +
"Main Menu \n\t\tSM\tStudent Maintenance" +
"\n\t\tSR\tStudent Report \n\t\tSGR\t" +
"Student Grade Report \n\t\tX\tExit \n\n\t\tProcess? ");
menu = j.nextLine();
if (menu.equals("x") || menu.equals("X")) {
System.exit(0);
}
if (menu.equals("SM") || menu.equals("sm")) {
do {
System.out.print("Student Number: ");
studentnumber = j.nextLine();
studentnumberA.add(studentnumber);
System.out.print("Student Name: ");
studentname = j.nextLine();
studentnameA.add(studentname);
System.out.print("Course : ");
course = j.nextLine();
courseA.add(course);
System.out.print("Prelim Grade: ");
prelim = j.nextDouble();
prelimA.add(prelim);
System.out.print("Midterm Grade: ");
midterm = j.nextDouble();
midtermA.add(midterm);
System.out.print("Finals Grade: ");
finals = j.nextDouble();
finalsA.add(finals);
j.nextLine();
System.out.println("Continue? (Y/N): ");
transaction = a.nextLine();
} while (transaction.equalsIgnoreCase("Y"));
}
if (menu.equalsIgnoreCase("sr")) {
do {
System.out
.println("\t\t\tDi Natuto University \n\t\t\t" +
"Pililla,Rizal \n\n\t\t\tStudent Summary Report" +
"\n\n\t\t\tSNo\tStudent no." +
"\n\t\t\tSna\tStudent Name \n\t\t\tCo\tCourse" +
"\n\t\t\tX\tExit \n\n\t\tProcess? ");
menu2 = j.nextLine();
System.out.println("\n");
if (menu2.equalsIgnoreCase("X")) {
System.exit(0);
}
if (menu2.equalsIgnoreCase("sno")) {
String[] array = studentnumberA
.toArray(new String[studentnumberA.size()]);
String[] array2 = studentnameA
.toArray(new String[studentnameA.size()]);
String[] array3 = courseA.toArray(new String[courseA
.size()]);
String[][] storeAllArray = { array, array2, array3 };
TreeMap<String, String> map = new TreeMap<String, String>();
TreeMap<String, String> map2 = new TreeMap<String, String>();
System.out
.println("\t\t\t\tDi Natuto University \n\t\t\t\t" +
"Pililla,Rizal \n\n\t\t\t\t" +
"Student Summary Report \n\n\t\t\tStudent no."
+ "Student Name\t\tCourse ");
for (int i = 0; i < array.length; i++) {
map2.put(array[i], array3[i]);
map.put(array[i], array2[i]);
}
for (String key : map.keySet())
System.out.println("\t\t\t" + key + "\t\t "
+ map.get(key) + "\t\t\t" + map2.get(key));
System.out.println("\n");
}
if (menu2.equalsIgnoreCase("sna")) {
String[] array = studentnumberA
.toArray(new String[studentnumberA.size()]);
String[] array2 = studentnameA
.toArray(new String[studentnameA.size()]);
String[] array3 = courseA.toArray(new String[courseA
.size()]);
String[][][] storeAllArray = { { array }, { array2 },
{ array3 } };
TreeMap<String, String> map = new TreeMap<String, String>();
TreeMap<String, String> map2 = new TreeMap<String, String>();
System.out
.println("\t\t\t\tDi Natuto University \n\t\t\t\t" +
"Pililla,Rizal \n\n\t\t\t\t" +
"Student Summary Report \n\n\t\t\tStudent no."
+ " Student Name" + "\tCourse ");
for (int i = 0; i < array.length; i++) {
map2.put(array2[i], array3[i]);
map.put(array2[i], array[i]);
}
for (String key : map.keySet())
System.out.println("\t\t\t" + map.get(key)
+ "\t\t " + key + "\t\t" + map2.get(key));
System.out.println("\n");
}
if (menu2.equalsIgnoreCase("co")) {
String[] array = studentnumberA
.toArray(new String[studentnumberA.size()]);
String[] array2 = studentnameA
.toArray(new String[studentnameA.size()]);
String[] array3 = courseA.toArray(new String[courseA
.size()]);
TreeMap<String, String> map = new TreeMap<String, String>();
TreeMap<String, String> map2 = new TreeMap<String, String>();
System.out
.println("\t\t\t\tDi Natuto University \n\t\t\t\t" +
"Pililla,Rizal \n\n\t\t\t\t" +
"Student Summary Report \n\n\t\t\tStudent no."
+ " Student Name" + "\tCourse ");
for (int i = 0; i < array.length; i++) {
map2.put(array3[i], array2[i]);
map.put(array3[i], array[i]);
}
for (String key : map.keySet())
System.out.println("\t\t\t" + map.get(key)
+ "\t\t " + map2.get(key) + "\t\t" + key);
System.out.println("\n");
}
System.out.print("Continue? (Y/N)");
transaction2 = j.nextLine();
} while (transaction2.equalsIgnoreCase("Y"));
}
if (menu.equalsIgnoreCase("sgr")) {
String[] array = studentnumberA
.toArray(new String[studentnumberA.size()]);
String[] array2 = studentnameA.toArray(new String[studentnameA
.size()]);
String[] array3 = courseA.toArray(new String[courseA.size()]);
Double[] array4 = prelimA.toArray(new Double[prelimA.size()]);
Double[] array5 = midtermA.toArray(new Double[midtermA.size()]);
Double[] array6 = finalsA.toArray(new Double[finalsA.size()]);
Double[] array7 = sumA.toArray(new Double[sumA.size()]);
String[][] storeAllArray = { array, array2, array3 };
TreeMap<Double, String> map = new TreeMap<Double, String>();
TreeMap<Double, String> map2 = new TreeMap<Double, String>();
TreeMap<Double, String> map3 = new TreeMap<Double, String>();
System.out
.println("\t\t\t\tDi Natuto University \n\t\t\t\t" +
"Pililla,Rizal \n\n\t\t\t\t" +
"Student Grade Report \n\n\t\t\tStudent no."
+ " Student Name"
+ "\tCourse"
+ "\t\t\tAverage");
for (int i = 0; i < array.length; i++) {
double sum = (array4[i] + array5[i] + array6[i]) / 3;
sumA.add(sum);
map.put(array7[i], array[i]);
map2.put(array7[i], array2[i]);
map3.put(array7[i], array3[i]);
}
for (Double key : map.keySet())
System.out.println("\t\t\t" + map.get(key) + "\t\t "
+ map2.get(key) + "\t\t" + map2.get(key) + "\t\t"
+ key);
System.out
.print("\n\n\t\t\tM\t\tMain Menu\n\t\t\tX\t\tExit\nChoice: ");
transaction3 = j.nextLine();
if (transaction3.equalsIgnoreCase("X")) {
System.out
.print("\t\t\t\tDi Natuto University \n\t\t\t\t" +
"Pililla,Rizal \n\n\t\t\t\t" +
"Thank you for using Di Natuto Grading System...");
System.exit(0);
}
}
} while (transaction.equalsIgnoreCase("N")
|| transaction2.equalsIgnoreCase("N")
|| transaction3.equalsIgnoreCase("M"));
}
}
答案 0 :(得分:1)
从我的观察和测试来看,这个问题似乎不存在。您遇到此问题,因为您正在尝试打印重复的条目。使用相同的键时,TreeMap
会假定它不再需要打印。
因为你试图在TreeMap中存储两个相同的键,所以它不会像你想要的那样工作。
建议修复它: 您可以尝试切换存储键和值的顺序。您可以存储它们,以便学生姓名是关键,他们的学号是值(或任何其他不同组合,以避免重复键)。
代码建议: 我注意到你的代码很长,有几行可以缩短。
一个人:
studentnumber=j.nextLine();
studentnumberA.add(studentnumber);
这可以简化为studentnumberA.add(j.nextLine());
与菜单代码中的所有其他变量一起使用。
如果您仍然不了解问题所在,请随时提出。我和其他许多人都愿意帮助你。
答案 1 :(得分:0)
嗯,问题是TreeMap(因为每张地图)都没有重复的键值。当你这样做时:
map.put("aaa", "Something");
map.put("aaa", "Another stuff");
你基本上用第二个覆盖你的第一个“作业”。所以这是一个坏主意。
我知道您使用TreeMap
作为对结果进行排序的方式,因此请允许我提出建议。如果您定义包含名称,ID和课程属性的对象Student
(假设这些属性是您在模型中定义为学生的属性),该怎么办?然后只需使用Comparable
接口(http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html)来定义您的排序标准。
从您的Scanner
阅读或属于学生的数据后,您只需创建一个包含所有这些内容的学生实例,然后将其添加到TreeMap
但不添加到任何SortedSet
(如TreeSet
)。
之后,您只需迭代您的设置并打印每个学生数据。
答案 2 :(得分:0)
学生&#34;身份证号码&#34;是Treemap的唯一键;添加副本会替换以前的值。
这发生在这里:
map2.put(array[i], array3[i]);
如果您希望两名具有相同ID的学生需要使用不同的唯一键存储它们。
答案 3 :(得分:0)
此代码中存在大量错误。尝试将其分解为更小的片段并将重复的代码提取到常用方法中。
这里你已经复制粘贴了,并且打印map2.get(密钥)两次而不是(推测)该密钥的map3的内容:
System.out.println("\t\t\t"+map.get(key)+ "\t\t "+map2.get(key)+"\t\t"+map2.get(key)+"\t\t"+key);
\t
的群众使这条线不可读,其含义不清楚。相反,如果您提取printStudent
方法,请考虑它的外观:
public static void printStudent(String id, String name, String course, int key) {
System.out.println("\t\t\t"+ id + "\t\t "+ name +"\t\t"+ course +"\t\t" + key);
}
main中的行现在显示为:
printStudent(map.get(key), map2.get(key), map2.get(key), key);
希望能够更容易地看到线路正在做什么并发现复制粘贴错误。