尝试计算GPA

发布于 2024-12-12 17:14:58 字数 1891 浏览 2 评论 0原文

到目前为止,这是我的代码(已更新):

import java.util.Scanner; 

public class gpa {

   public static void main(String[] args) {
      double gpa=0;
      double input = 0;
      String grade= "";
      int classes =0;
      double fail =0;
      do
      {
         System.out.print("Enter Grade (n to quit): "); 
         Scanner sc = new Scanner(System.in); 
          grade = sc.nextLine(); 
          grade = grade.toLowerCase(); 
          if (grade.equals("a"))
          {
             input = 4.0;
          }
          if (grade.equals("b"))
          {
             input = 3.0;
          }
          if (grade.equals("c"))
          {
             input = 2.0;
          }
          if (grade.equals("d"))
          {
             input = 1.0;
          }
          if (grade.equals("f"))
          {
             fail = 1;
          }

          gpa+=input;
          classes++;
      } while(grade!="n");
      System.out.print("GPA: " + gpa + "   "); 

      gpa/=classes;
      if (gpa>=2 && classes >=4 && fail !=1)
      {
         System.out.print("Eligible");    
      }
      else if(classes <4)
      {
         System.out.print("Ineligible. less than 4 classes"); 
      }
      else if (gpa < 2.0)
      {
         System.out.print("Ineligible. GPA is less than 2.0"); 
      }
      else if (gpa >=2.0 && fail == 1)
      {
         System.out.print("Ineligible. GPA is above 2.0, but has an F");
      }
      else if (gpa <2.0 && fail ==1)
      {
         System.out.print("Ineligible. GPA is below 2.0 and has F");
      }
   }

}

这是我需要能够输出的内容:

  1. 合格
  2. 不合格,参加少于 4 门课程
  3. 不合格,gpa 低于 2.0
  4. 不合格,gpa 高于 2.0 但具有 F 级(注意:gpa >= 2.0)
  5. 不合格,gpa低于2.0且成绩为F

一直要求输入。我该如何阻止这个? 我尝试将成绩转换为字符,但结果对我来说非常糟糕。 我认为这可能是编译器错误或其他原因。 任何帮助将不胜感激。虽然有用的帮助会降低我的血压。

here's my code so far(it's been updated):

import java.util.Scanner; 

public class gpa {

   public static void main(String[] args) {
      double gpa=0;
      double input = 0;
      String grade= "";
      int classes =0;
      double fail =0;
      do
      {
         System.out.print("Enter Grade (n to quit): "); 
         Scanner sc = new Scanner(System.in); 
          grade = sc.nextLine(); 
          grade = grade.toLowerCase(); 
          if (grade.equals("a"))
          {
             input = 4.0;
          }
          if (grade.equals("b"))
          {
             input = 3.0;
          }
          if (grade.equals("c"))
          {
             input = 2.0;
          }
          if (grade.equals("d"))
          {
             input = 1.0;
          }
          if (grade.equals("f"))
          {
             fail = 1;
          }

          gpa+=input;
          classes++;
      } while(grade!="n");
      System.out.print("GPA: " + gpa + "   "); 

      gpa/=classes;
      if (gpa>=2 && classes >=4 && fail !=1)
      {
         System.out.print("Eligible");    
      }
      else if(classes <4)
      {
         System.out.print("Ineligible. less than 4 classes"); 
      }
      else if (gpa < 2.0)
      {
         System.out.print("Ineligible. GPA is less than 2.0"); 
      }
      else if (gpa >=2.0 && fail == 1)
      {
         System.out.print("Ineligible. GPA is above 2.0, but has an F");
      }
      else if (gpa <2.0 && fail ==1)
      {
         System.out.print("Ineligible. GPA is below 2.0 and has F");
      }
   }

}

Here's what I need to be able to output:

  1. Eligible
  2. Ineligible, taking less than 4 classes
  3. Ineligible, gpa below 2.0
  4. Ineligible, gpa above 2.0 but has F grade (note: gpa >= 2.0)
  5. Ineligible, gpa below 2.0 and has F grade

It keeps asking for input. How do I stop this?
I tried converting grade into a char but that ended horribly for me.
I think it might be a compiler error or something.
Any help would be appreciated. Though helpful help would keep my blood pressure down.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无法言说的痛 2024-12-19 17:14:59

除了损坏的比较(测试两个字符串是否引用同一个对象,而不是它们的内容在逻辑上是否相等)之外,您还无法将成绩的 input 设置为 0 F。因此,单个 f 计入 GPA 与之前的成绩相同。

当用户最终输入n时,您仍然需要进行数学计算以添加成绩并增加成绩数量。而且,你从来没有真正计算过 GPA(你只是将成绩相加)。

In addition to the broken comparison (testing if two strings reference the same object rather than if their contents are logically equal), you also fail to set input to 0 for a grade of F. As a result, a single f counts towards the GPA as the same as the previous grade.

When the person finally does input n, you still do the math to add the grade and increment the number of grades. Also, you never actually compute the GPA (you just sum the grades).

妄想挽回 2024-12-19 17:14:59

首先,您不能使用 == 运算符来比较字符串。其作用是比较实际的对象引用,即。对于 grade == "a" 它会检查 grade"a" 是否是相同的对象,但事实并非如此。

为了检查字符串的内容是否相等,请使用.equals(),即。 grade.equals(“a”)

还有一点,GPA不应该是你总分的平均分吗? IE。完成循环后gpa /=classes

另外,不要使用 double 来存储失败(用于了解主题是否失败的标志变量),而是使用 boolean 并将其设置为如果成绩为 F,则为 true。说到 if 语句,它不会修改 inputinput< 的先前值/code> 将被添加到 gpa 中,而不是像不及格成绩那样0.0

那么无效输入怎么办?这还会导致 gpa 增加 input 的先前值。

编辑:并使用一些else if,而不是单独的if语句,因为即使一个条件通过,也会检查所有其他条件。 IE。如果 grade.equals("a") 为 true,您仍然不需要检查 grade 是否也是“b”、“c”、“d”和“ f”。

EDIT2:根据您刚刚添加的条件列表以及最后一个 if 语句块的方式,一般规则是将更具体的条件放在更通用的条件之上以便它们不会被广义的阴影所掩盖。 IE。如果按照 animuson 提到的那样按顺序排列,则数字 5(“不合格,gpa 低于 2.0,且成绩为 F”)将被数字 3(“不合格,gpa 低于 2.0”)遮盖。

For a start, you can't compare strings using the == operator. What that does is compare the actual object references, ie. for grade == "a" it's checking if grade and "a" are the same objects, which they're not.

In order to check the content of the strings for equality, use .equals(), ie. grade.equals("a").

Another thing, shouldn't GPA be the average of your total scores? ie. gpa /= classes after you finish your loop.

Also, rather than using a double to store fail, the flag variable for knowing if they failed a subject, use a boolean and set it to true if the grade is F. Speaking of that if statement, it doesn't modify input and the previous value of input will be added to gpa instead of a fail grade like 0.0.

And what about invalid input? That'll also cause gpa to be incremented by the previous value of input.

EDIT: And use some else ifs, instead of separate if statements, since every other condition will be checked even if one condition passes. ie. If grade.equals("a") is true, you'll still unnecessarily be checking if grade is also "b", "c", "d" and "f".

EDIT2: With that list of conditions you just added and the way you have your last block of if statements, a general rule is to have your more specific conditions above the more generalised ones so that they're not shadowed by the generalised ones. ie. Number 5 ("Ineligible, gpa below 2.0 and has F grade") is shadowed by number 3 ("Ineligible, gpa below 2.0") if you keep them in that order, as mentioned by animuson.

掩饰不了的爱 2024-12-19 17:14:59

您修复了除“while”子句之外的所有地方对相等运算符的误用。应该是

while(!grade.equals("n")

You fixed the misuse of the equality operator everywhere but in your "while" clause. It should be

while(!grade.equals("n")
嗼ふ静 2024-12-19 17:14:59

Java、JavaScript、PHP 或 C++,语言无关紧要,因为方法都是相同的。您需要认真考虑在代码中的这两行中使用相同变量的含义:

else if (gpa <2.0 && fail ==1)

Java, JavaScript, PHP or C++, the language is irrelevant as the methods are all the same. You need to think seriously about the implications of using the same variable in these two lines in your code:

else if (gpa <2.0 && fail ==1)
平定天下 2024-12-19 17:14:58

您使用的是 String (对象)而不是 char,因此您需要使用 .equals 方法。例如,您需要 grade.equals("a"),而不是 grade == "a"

You're using a String (an object) not a char, so you need to use the .equals method. e.g. instead of grade == "a", you need grade.equals("a").

高速公鹿 2024-12-19 17:14:58

这让很多新的 Java 程序员感到困惑,这是一个非常常见的错误。您需要使用 .equals 方法,而不是 == 运算符。请参阅 Java String.equals 与 ==

This trips a lot of new Java programmers up, it's a very common mistake. You need to use the .equals method, not the == operator. See Java String.equals versus ==.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文