Java 无法打印正确的总和

Java 无法打印正确的总和,java,loops,Java,Loops,我已经根据每个学生的成绩给他/她分配了一个字母等级。我想计算每个字母等级的学生总数。但由于某些原因,结果完全错了。谁能告诉我哪里做错了?谢谢大家! String letter ="A"; int letterA=0; int letterB=0; int letterC=0; int letterD=0; int letterF=0; int a=0; int b=0; int c=0; int d=0; int f=0; for (int row=0;row<100;row++){ //

我已经根据每个学生的成绩给他/她分配了一个字母等级。我想计算每个字母等级的学生总数。但由于某些原因,结果完全错了。谁能告诉我哪里做错了?谢谢大家!

String letter ="A";
int letterA=0;
int letterB=0;
int letterC=0;
int letterD=0;
int letterF=0;
int a=0;
int b=0;
int c=0;
int d=0;
int f=0;
for (int row=0;row<100;row++){ //the outer loop, have 100 rows to go through 
   for ( int column=0;column<words.length;column++) { 
       if(words[column].equals(table[row][column])){ // compare two arrays
            count++; }// add the score for each student if he/she is right
   }
     if (count >=18)
         letter=("A");
     if(count>=16 && count<18)
         letter=("B");
     if(count>=14 && count<16)
         letter=("C");
     if(count>=12 && count<14)
         letter=("D");      
     if(count<12)
         letter=("F");    
    System.out.println("Student Grade: "+letter+"\t");   
     count=0; // make sure the count will go back to 0, and run the loop again 

if (letter.equals("A"))
     letterA++;
     a+=letterA;}  
if (letter.equals("B"))
     letterB++;
     b+=letterB;
if (letter.equals("C"))
     letterC++;
     c+=letterC;
if (letter.equals("D"))
     letterD++;
     d+=letterD;
if (letter.equals("F"))
     letterF++;
     f+=letterF;
System.out.print("Question A "+a);
System.out.print("Question B "+b);
System.out.print("Question C "+c);
System.out.print("Question D "+d);
System.out.print("Question F "+f);
 }
String letter=“A”;
int-letterA=0;
int-letterB=0;
int-letterC=0;
int-letted=0;
int-letterF=0;
int a=0;
int b=0;
int c=0;
int d=0;
int f=0;

for(int row=0;row=16&&count=14&&count=12&&count@Buddy位于右侧机架上,但不止此。具有多行的所有
if
语句都需要大括号

否则编译器只读取第一行。根据Oracle:

决定何时省略大括号是个人喜好的问题。省略大括号会使代码更加脆弱。如果在“then”子句中添加第二条语句,常见的错误是忘记添加新需要的大括号。编译器无法捕获此类错误;您只会得到错误的结果

例如:


@Buddy在正确的框架上,但不仅仅如此。所有包含多行的
if
语句都需要大括号

否则编译器只读取第一行。根据Oracle:

决定何时省略大括号是个人喜好的问题。省略大括号会使代码更加脆弱。如果在“then”子句中添加第二条语句,常见的错误是忘记添加新需要的大括号。编译器无法捕获此类错误;您只会得到错误的结果

例如:

始终使用大括号,即使对于单个语句: 应始终:

if ("A".equals(letter)) { letterA++; }
else if ("B".equals(letter)) { letterB++; }
else if ("C".equals(letter)) { letterC++; }
else if ("D".equals(letter)) { letterD++; }
else if ("F".equals(letter)) { letterF++; }
else { throw new RuntimeException("Invalid Letter " + letter); }
如果希望语句在视觉上紧凑,请使用单行语句,但 仍然使用大括号,因为它们可以保证所要表达的意图 在给定的块中完成,它们还充当该意图的文档 让未来的人们(人们包括你)知道发生了什么 开

我看不到单字母变量从未被使用的正当理由,我也不明白它们为什么存在

一般评论: 始终使用大括号: 省略大括号意味着当
if
匹配时,只执行
if
后的第一个语句,下一行总是执行

if (letter.equals("A"))
     letterA++;
     a+=letterA;
实际上是

if (letter.equals("A")) { letterA++; }
a+=letterA;
这意味着,无论
if
测试中的表达式计算结果是什么,大括号外的行始终会被执行。第二行的缩进将该语句合并为
if
块的一部分,而不是

去掉大括号和括号绝对不会有任何好处 失去一切

简洁的代码易于阅读和维护,并显示出您的关注: 始终保持代码格式的一致性,并且不要过于密集,这样您就可以知道大括号缺少版本时发生了什么以及哪里出了问题

看看最好的广告,它有很多,干净的格式代码也应该有很多一致的,这样我们的大脑可以快速模式匹配和扫描相关的东西,如匹配的括号对

在所有值得使用的IDE中,干净的格式化代码只需一次按键

干净的代码表明你关心你在做什么,并使你的问题更有吸引力,这意味着其他人也同样关心他们的答案

干净的代码能让你赢得同事们的尊重,他们知道你在看什么,让你与那些不在乎或不知道他们在看什么的人区别开来

最重要的是,干净的代码更容易推理,并且具有更少的可用性 bug,没有细微的bug,并且更易于维护

始终涵盖所有条件: 始终将
if/else if/else
与互斥测试一起使用

如果所有的
If
子句都是互斥的,并且您匹配了第一个子句,那么所有其余的子句仍然会使用
If/If/If/If
结构无理由地进行求值。
If/else If/else If/else If/else
只求值直到有匹配或没有匹配为止

如果没有
else
则不能涵盖所有可能不匹配的情况,这通常是在没有
else
的情况下无声发生的错误

不要只覆盖,覆盖例外路径,而是要以正确的方式进行

避免
==null
检查;完全避免
null
: 始终使用
.equals()
将文本与变量进行比较,以避免可能的
NullPointerException

避免完全使用
null
引用,这在任何情况下都是可能的,甚至在看起来是合法原因的情况下也是可能的

如果是,它们的发明者是谁 谁来争论

始终以描述性的方式命名变量! 我不确定
字母a
应该代表什么,也不确定
字母a
应该代表什么。因此,没有人能告诉你这些是否正确,因为没有人确切知道它们在语义上代表什么

没有未命名的数值常量(): 考虑到它们的使用方式,这些名字可能会更好 像
最低分数
,但这是基于意见的,教训是避免的
神奇的数字

在同一方向进行范围检查:

在同一个方向上进行范围检查,以便变量在比较的中间可见。 这使得以后打破逻辑变得更加困难,并且自我证明这是一个范围检查

if (count >= A_GRADE) { /* omitted */ }
else if (B_GRADE <= count && count < A_GRADE) { /* omitted */ }
else if (C_GRADE <= count && count < B_GRADE) { /* omitted */ }
else if (D_GRADE <= count && count < C_GRADE) { /* omitted */ }
else /* isF */ { /* omitted */ }
哪一个更明显和自我记录,因此更重要 可维护的

干燥-: 逻辑上,
if(count>=A_GRADE){letter=“A”}
if(“A”.equals(letter)){/*do stuff*/}
完全相同,因此这是重复的逻辑

与其分配一个
字母
,不如再次检查,只需将逻辑放入原始检查中即可

if (count >= A_GRADE) { /* do stuff */ }
else if (B_GRADE <= count && count < A_GRADE) { /* do stuff */ }
else if (C_GRADE <= count && count < B_GRADE) { /* do stuff */ }
else if (D_GRADE <= count && count < C_GRADE) { /* do stuff */ }
else { /* omitted */ }
使用描述性名称的小方法越多越好
final int A_GRADE = 18;
final int B_GRADE = 16;
final int C_GRADE = 14;
final int D_GRADE = 12;
if (count >= A_GRADE) { /* omitted */ }
else if (B_GRADE <= count && count < A_GRADE) { /* omitted */ }
else if (C_GRADE <= count && count < B_GRADE) { /* omitted */ }
else if (D_GRADE <= count && count < C_GRADE) { /* omitted */ }
else /* isF */ { /* omitted */ }
private static boolean isA(final int count) { return count >= A_GRADE; }
private static boolean isB(final int count) { return B_GRADE <= count && count < A_GRADE; }
private static boolean isC(final int count) { return C_GRADE <= count && count < B_GRADE; }
private static boolean isD(final int count) { return D_GRADE <= count && count < C_GRADE; }
if (isA(count)) { /* omitted */ }
else if (isB(count)) { /* omitted */ }
else if (isC(count)) { /* omitted */ }
else if (isD(count)) { /* omitted */ }
else /* isF */ { /* omitted */ }
if (count >= A_GRADE) { /* do stuff */ }
else if (B_GRADE <= count && count < A_GRADE) { /* do stuff */ }
else if (C_GRADE <= count && count < B_GRADE) { /* do stuff */ }
else if (D_GRADE <= count && count < C_GRADE) { /* do stuff */ }
else { /* omitted */ }
if (count >= A_GRADE) { recordMarkA(); }
else if (B_GRADE <= count && count < A_GRADE) { recordMarkB(); }
else if (C_GRADE <= count && count < B_GRADE) { recordMarkC(); }
else if (D_GRADE <= count && count < C_GRADE) { recordMarkD(); }
else { recordMarkF(); }
public class Q34081279
{
    final static int A_GRADE = 18;
    final static int B_GRADE = 16;
    final static int C_GRADE = 14;
    final static int D_GRADE = 12;

    public static void main(final String[] args)
    {
        final String[] words = new String[]{}; /* this is just a placeholer, not provided in question */
        final String[][] table = new String[][]{}; /* this is just a placehoder, not provided in question */

        int markA = 0;
        int markB = 0;
        int markC = 0;
        int markD = 0;
        int markF = 0;

        for (int row = 0; row < 100; row++)
        {
            int count = 0;
            for (int column = 0; column < words.length; column++)
            {
                if (words[column].equals(table[row][column])) { count++; }
            }
            if (count >= A_GRADE) { System.out.format("%d = A", count); }
            else if (B_GRADE <= count && count < A_GRADE) { System.out.format("%d = B", count); }
            else if (C_GRADE <= count && count < B_GRADE) { System.out.format("%d = C", count); }
            else if (D_GRADE <= count && count < C_GRADE) { System.out.format("%d = D", count); }
            else { System.out.format("%d = F", count); }
            System.out.println();
        }

        System.out.println(String.format("Question A %d", markA));
        System.out.println(String.format("Question B %d", markB));
        System.out.println(String.format("Question C %d", markC));
        System.out.println(String.format("Question D %d", markD));
        System.out.println(String.format("Question F %d", markF));
    }
}