在Java中对数组中的条目进行排序和显示
我编写了一个程序,将数字等级分为字母等级:在Java中对数组中的条目进行排序和显示,java,sorting,Java,Sorting,我编写了一个程序,将数字等级分为字母等级: int [] a = new int[20]; Scanner keyboard = new Scanner(System.in); System.out.println("Enter 20 Grades to be Counted:"); for (int i = 0; i < a.length; i++) { a[i] = keyboard.nextInt(); }
int [] a = new int[20];
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 20 Grades to be Counted:");
for (int i = 0; i < a.length; i++)
{
a[i] = keyboard.nextInt();
}
for (int i = 0; i < a.length; i++)
{
if (a[i] >= 90)
aCount++;
else if(a[i] < 90 && a[i] >=80)
bCount++;
else if (a[i] < 80 && a[i] >= 70)
cCount++;
else if (a[i] < 70 && a[i] >=65)
dCount++;
else
fCount++;
}
System.out.println("Number of A's: " + aCount);
System.out.println("Number of B's: " + bCount);
System.out.println("Number of C's: " + cCount);
System.out.println("Number of D's: " + dCount);
System.out.println("Number of FAILURES: " + fCount);
}
int[]a=新的int[20];
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入要计数的20个等级:”);
for(int i=0;i=90)
aCount++;
如果(a[i]<90&&a[i]>=80)
bCount++;
如果(a[i]<80&&a[i]>=70)
cCount++;
如果(a[i]<70&&a[i]>=65)
dCount++;
其他的
fCount++;
}
System.out.println(“A的数量:+A计数);
System.out.println(“B的数量:+B计数);
System.out.println(“C的数量:+Account”);
System.out.println(“D的数量:+D计数);
System.out.println(“故障数:+fCount”);
}
}
我想做的是获取输入的分数,并在它们各自的字母中显示实际的分数。有点像这样:
A的:
100
95
九十
B的:
88
八十五
C的:
79
77
。。。等等如何做到这一点?您可以使用集合来存储各个等级。要按排序顺序存储等级,您可以使用
树集
。然后在集合上迭代
int [] a = new int[20];
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 20 Grades to be Counted:");
for (int i = 0; i < a.length; i++)
{
a[i] = keyboard.nextInt();
}
TreeSet<Integer> setA = new TreeSet<Integer>();
TreeSet<Integer> setB = new TreeSet<Integer>();
TreeSet<Integer> setC = new TreeSet<Integer>();
TreeSet<Integer> setD = new TreeSet<Integer>();
TreeSet<Integer> setF = new TreeSet<Integer>();
for (int i = 0; i < a.length; i++)
{
if (a[i] >= 90){
aCount++;
setA.add(a[i]);
}
else if(a[i] < 90 && a[i] >=80){
bCount++;
setB.add(a[i]);
}
else if (a[i] < 80 && a[i] >= 70){
cCount++;
setC.add(a[i]);
}
else if (a[i] < 70 && a[i] >=65){
dCount++;
setD.add(a[i]);
}
else{
fCount++;
setF.add(a[i]);
}
}
System.out.println("Number of A's: " + aCount);
System.out.print("A's: ");
for(Integer x : setA){
System.out.print(x + " ");
}
System.out.println();
System.out.println("Number of B's: " + bCount);
System.out.print("A's: ");
for(Integer x : setA){
System.out.print(x + " ");
}
System.out.println();
System.out.println("Number of C's: " + cCount);
System.out.print("B's: ");
for(Integer x : setB){
System.out.print(x + " ");
}
System.out.println();
System.out.println("Number of D's: " + dCount);
System.out.print("C's: ");
for(Integer x : setC){
System.out.print(x + " ");
}
System.out.println();
System.out.println("Number of FAILURES: " + fCount)
System.out.print("D's: ");
for(Integer x : setD){
System.out.print(x + " ");
}
System.out.println();
int[]a=新的int[20];
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入要计数的20个等级:”);
for(int i=0;i=90){
aCount++;
setA.添加(a[i]);
}
如果(a[i]<90&&a[i]>=80){
bCount++;
增加(a[i]);
}
如果(a[i]<80&&a[i]>=70){
cCount++;
增加(a[i]);
}
如果(a[i]<70&&a[i]>=65){
dCount++;
增加(a[i]);
}
否则{
fCount++;
增加(a[i]);
}
}
System.out.println(“A的数量:+A计数);
系统输出打印(“A的:”);
用于(整数x:setA){
系统输出打印(x+“”);
}
System.out.println();
System.out.println(“B的数量:+B计数);
系统输出打印(“A的:”);
用于(整数x:setA){
系统输出打印(x+“”);
}
System.out.println();
System.out.println(“C的数量:+Account”);
系统输出打印(“B的:”);
对于(整数x:setB){
系统输出打印(x+“”);
}
System.out.println();
System.out.println(“D的数量:+D计数);
系统输出打印(“C的:”);
用于(整数x:setC){
系统输出打印(x+“”);
}
System.out.println();
System.out.println(“故障数:+fCount”)
系统输出打印(“D的:”);
for(整数x:setD){
系统输出打印(x+“”);
}
System.out.println();
编辑:如果是重复的,您可以使用存储值,然后在显示结果之前使用对ArrayList进行排序。您可以使用集合存储各个等级。要按排序顺序存储等级,您可以使用
树集
。然后在集合上迭代
int [] a = new int[20];
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 20 Grades to be Counted:");
for (int i = 0; i < a.length; i++)
{
a[i] = keyboard.nextInt();
}
TreeSet<Integer> setA = new TreeSet<Integer>();
TreeSet<Integer> setB = new TreeSet<Integer>();
TreeSet<Integer> setC = new TreeSet<Integer>();
TreeSet<Integer> setD = new TreeSet<Integer>();
TreeSet<Integer> setF = new TreeSet<Integer>();
for (int i = 0; i < a.length; i++)
{
if (a[i] >= 90){
aCount++;
setA.add(a[i]);
}
else if(a[i] < 90 && a[i] >=80){
bCount++;
setB.add(a[i]);
}
else if (a[i] < 80 && a[i] >= 70){
cCount++;
setC.add(a[i]);
}
else if (a[i] < 70 && a[i] >=65){
dCount++;
setD.add(a[i]);
}
else{
fCount++;
setF.add(a[i]);
}
}
System.out.println("Number of A's: " + aCount);
System.out.print("A's: ");
for(Integer x : setA){
System.out.print(x + " ");
}
System.out.println();
System.out.println("Number of B's: " + bCount);
System.out.print("A's: ");
for(Integer x : setA){
System.out.print(x + " ");
}
System.out.println();
System.out.println("Number of C's: " + cCount);
System.out.print("B's: ");
for(Integer x : setB){
System.out.print(x + " ");
}
System.out.println();
System.out.println("Number of D's: " + dCount);
System.out.print("C's: ");
for(Integer x : setC){
System.out.print(x + " ");
}
System.out.println();
System.out.println("Number of FAILURES: " + fCount)
System.out.print("D's: ");
for(Integer x : setD){
System.out.print(x + " ");
}
System.out.println();
int[]a=新的int[20];
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入要计数的20个等级:”);
for(int i=0;i=90){
aCount++;
setA.添加(a[i]);
}
如果(a[i]<90&&a[i]>=80){
bCount++;
增加(a[i]);
}
如果(a[i]<80&&a[i]>=70){
cCount++;
增加(a[i]);
}
如果(a[i]<70&&a[i]>=65){
dCount++;
增加(a[i]);
}
否则{
fCount++;
增加(a[i]);
}
}
System.out.println(“A的数量:+A计数);
系统输出打印(“A的:”);
用于(整数x:setA){
系统输出打印(x+“”);
}
System.out.println();
System.out.println(“B的数量:+B计数);
系统输出打印(“A的:”);
用于(整数x:setA){
系统输出打印(x+“”);
}
System.out.println();
System.out.println(“C的数量:+Account”);
系统输出打印(“B的:”);
对于(整数x:setB){
系统输出打印(x+“”);
}
System.out.println();
System.out.println(“D的数量:+D计数);
系统输出打印(“C的:”);
用于(整数x:setC){
系统输出打印(x+“”);
}
System.out.println();
System.out.println(“故障数:+fCount”)
系统输出打印(“D的:”);
for(整数x:setD){
系统输出打印(x+“”);
}
System.out.println();
编辑:如果是重复的,您可以使用存储值,然后在显示结果之前使用对ArrayList进行排序。我想这不会起作用,因为它会消除重复的结果。他可能需要使用一个列表,而不是一个集合。我编辑了一篇文章,以防出现重复。我想这不会起作用,因为它会消除重复的结果。他可能需要使用一个列表,而不是一个集合。我已经编辑了这篇文章,以便在出现重复的情况下使用