java - 尝试计算GPA

标签 java string compiler-errors

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

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. 不符合资格,参加的类(class)少于 4 门
  3. 不符合资格,GPA 低于 2.0
  4. 不符合资格,gpa 高于 2.0,但成绩为 F(注:gpa >= 2.0)
  5. 不符合资格,GPA 低于 2.0,且成绩为 F

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

最佳答案

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

关于java - 尝试计算GPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961538/

相关文章:

java - 我可以在商业项目中使用 OrmLite 吗?

java - 在 Java String.replaceAll() 正则表达式中使用函数或方法

java - 在java中将String转换为int数组

c - 错误: variable (struct) has initializer but incomplete type (C)

java - 使用 Java 8 流运算符将两级映射列表减少为单个两级映射

java - Android 更改存储的 SharedPreferences 值?

C# - 比较字符串时显示差异

c++ - 在 C++ 中将包含二进制操作的字符串转换为 double

linux - Glibc编译错误

ios - 为 iOS 模拟器构建,但在为 OSX 构建的目标文件中链接,为 Xcode 7 中的架构 x86_64