【问题标题】:if statement is not utilizing values in the arrayif 语句没有使用数组中的值
【发布时间】:2021-03-12 00:41:38
【问题描述】:

这段代码应该:

  1. 使用扫描仪输入创建一个数组并使用String.split
  2. 计算 GPA 并根据 GPA 和参加的课程数量确定学生是否有资格参加体育运动。

按原样,代码运行正常,但它没有像预期的那样使用 Scanner 输入String.split

顶部被注释掉的代码是我一直在使用 String.split 的代码,但是当我这样做时,计算 GPA 的 if 语句不起作用。没有错误信息,GPA 只返回 0.0。

有人可以告诉我问题是什么或指出我正确的方向吗? 谢谢。

    import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    
    //Initialize array  
    double GPA = 0;
    boolean hasF = false;
        

    /*String str = "a,b";
    String[] arr = str.split(",");*/
    

        String [] arr = new String [] {"a", "a", "b", "a", "c"};  //change the grades accordingly
        double len = arr.length;
        System.out.println("Elements of given array: ");  
        //Loop through the array by incrementing value of i  
        for (int i = 0; i < len; i++) {  
            System.out.println(arr[i] + " "); 
        }
        for (String i : arr) {//goes through each value and adds to GPA accordingly
          if (i == "a") {
            GPA = GPA + 4.0;
            
          }
          else if (i == "b") {
            GPA = GPA + 3.0;
            
          }
          else if (i == "c") {
            GPA = GPA + 2.0;
            
          }
          else if (i == "d") {
            GPA = GPA + 1.0;
             
          }
          else if (i == "f"){
            GPA = GPA + 0.0;
            hasF = true;
          } 
          else {//invalid input?
            System.out.println("invalid input: " + i);
            return;
          }
        }
        
        GPA = GPA/len;//calculate GPA
        System.out.println("GPA: " + GPA);

        
        if (len < 4) {//determines elegebility
          System.out.println("You are ineligable to participate in activities and sport because you are not taking enough classes.");
        }
        else if (hasF == true && GPA >= 2.0){
          System.out.println("You are ineligable to participate in activities and sports because you have an F. Even though your GPA is greater than 2.0 and you are taking enough classes.");
        }
        else if (hasF == true && GPA <= 2.0) {
          System.out.println("You are ineligable to participate in activities and sports because you have an F and your GPA is less than a 2.0.");
        }
        else if (hasF == false && GPA <= 2.0){
          System.out.println("You are ineligable to participate in activities and sports because your GPA is less than a 2.0.");
        }

        else if (hasF == true){
          System.out.println("You are ineligable to participate in activities and sports because you have an F.");
        }
        else{
          System.out.println("You are Eligable to participate in activities and sports. GOOD JOB!!!");
        }
    }
}

*我已经交了作业,所以你不会帮我作弊

【问题讨论】:

  • 字符串比较在java中不能与==正常工作,你可以尝试使用i.equals("a")吗?或i.equalsIgnoreCase("a")
  • 另外,我建议在这种情况下使用switch case,这样代码会更简洁。
  • 我运行你的代码,它运行良好,GPA 是 3.4

标签: java arrays string split


【解决方案1】:

您的 if 条件中的问题是:您正在尝试使用 == 比较 String 值,但这将检查 String 值的引用,因为 String 是一个对象,而 ==(2 个等号) 检查它们的引用是否相同。 要比较String 值,您需要调用Stringequals() 方法:"a".equals(i)i.equals("a")"a".equals(i) 比较更好,因为它会阻止NullPointerException

if ("a".equals(i)) {
    GPA = GPA + 4.0;
    
} else if ("b".equals(i)) {
    GPA = GPA + 3.0;

} else if ("c".equals(i)) {
    GPA = GPA + 2.0;

} else if ("d".equals(i)) {
    GPA = GPA + 1.0;

} else if ("f".equals(i)) {
    GPA = GPA + 0.0;
    hasF = true;
} else {// invalid input?
    System.out.println("invalid input: " + i);
    return;
}

完成代码:

  1. 要从用户获取逗号分隔的 (,) 值,您需要定义一个 Scanner 对象并提示用户输入 grade 值。
public static void main(String[] args) {
    // Initialize array
    double GPA = 0;
    boolean hasF = false;

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the grades and seperate by (,): ");
    String str = input.nextLine();
    String[] arr = str.split(",");

//      String[] arr = new String[] { "a", "a", "b", "a", "c" }; // change the grades accordingly
    double len = arr.length;
    System.out.print("Elements of given array: ");
    // Loop through the array by incrementing value of i
    for (int i = 0; i < len; i++) {
        System.out.print(arr[i] + " ");
    }
    System.out.println();
    for (String i : arr) {// goes through each value and adds to GPA accordingly
        if ("a".equals(i)) {
            GPA = GPA + 4.0;

        } else if ("b".equals(i)) {
            GPA = GPA + 3.0;

        } else if ("c".equals(i)) {
            GPA = GPA + 2.0;

        } else if ("d".equals(i)) {
            GPA = GPA + 1.0;

        } else if ("f".equals(i)) {
            GPA = GPA + 0.0;
            hasF = true;
        } else {// invalid input?
            System.out.println("invalid input: " + i);
            return;
        }
    }

    GPA = GPA / len;// calculate GPA
    System.out.println("GPA: " + GPA);

    if (len < 4) {// determines elegebility
        System.out.println(
                "You are ineligable to participate in activities and sport because you are not taking enough classes.");
    } else if (hasF == true && GPA >= 2.0) {
        System.out.println(
                "You are ineligable to participate in activities and sports because you have an F. Even though your GPA is greater than 2.0 and you are taking enough classes.");
    } else if (hasF == true && GPA <= 2.0) {
        System.out.println(
                "You are ineligable to participate in activities and sports because you have an F and your GPA is less than a 2.0.");
    } else if (hasF == false && GPA <= 2.0) {
        System.out.println(
                "You are ineligable to participate in activities and sports because your GPA is less than a 2.0.");
    }

    else if (hasF == true) {
        System.out.println("You are ineligable to participate in activities and sports because you have an F.");
    } else {
        System.out.println("You are Eligable to participate in activities and sports. GOOD JOB!!!");
    }
}

【讨论】:

    【解决方案2】:

    虽然您正在导入 Scanner 对象,但您还必须创建一个 Scanner 对象。我建议将Scanner input = new Scanner(System.in); 放在主要方法的顶部。然后您可以使用String foo = input.nextLine(); 读取输入。提示用户输入类似System.out.print("Enter grades separated by commas with no spaces: ") 的内容也可能有意义。要拆分它,请执行foo.split(',');

    另外一条建议,hasF 是一个布尔值,所以它的值是truefalse。这意味着将 hasF 与 truefalse 进行比较是多余的。如果将 hasF 与 true 进行比较,则可以将 if 语句缩短为 if (hasF &amp;&amp; GPA &gt;= 2.0),如果将 hasF 与 false 进行比较,则可以缩短为 if (!hasF &amp;&amp; GPA &gt;= 2.0)

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多