gpt4 book ai didi

Java字符串输入比较

转载 作者:行者123 更新时间:2023-12-01 18:40:22 24 4
gpt4 key购买 nike

想通了。多谢你们。我知道这不是一个完美的答案,但是我是java新手。

包Dr._Coles_Practice_Problems;

导入java.util.Scanner;

公共(public)类 GpaCalc {

public static void main(String[] args) {

// Needed variables

double credits = 0.0;

double totalcredits = 0.0;

double grade = 0.0;

double qualitypoints = 0;

int classes;

String lettergrade;

Scanner input = new Scanner(System.in);

System.out.println("Enter the amount of classes for this semester");

classes = input.nextInt();

while (classes > 0) {

System.out.println("Enter the amount of credits for this class");

credits = input.nextDouble();

System.out.println("Enter the grade achieved for this class");

lettergrade = input.next();

if (lettergrade.equalsIgnoreCase("a")) {

grade += 4.00;

}

if (lettergrade.equalsIgnoreCase("a-")) {

grade += 3.67;

}

if (lettergrade.equalsIgnoreCase("b+")) {

grade += 3.33;

}

if (lettergrade.equalsIgnoreCase("b")) {

grade += 3.00;

}

if (lettergrade.equalsIgnoreCase("b-")) {

grade += 2.67;

}

if (lettergrade.equalsIgnoreCase("c+")) {

grade += 2.33;

}

if (lettergrade.equalsIgnoreCase("c")) {

grade += 2.00;

}

if (lettergrade.equalsIgnoreCase("c-")) {

grade += 1.67;

}

if (lettergrade.equalsIgnoreCase("d+")) {

grade += 1.33;

}

if (lettergrade.equalsIgnoreCase("d")) {

grade += 1.00;

}

if (lettergrade.equalsIgnoreCase("f")) {

grade += 0.00;

}

classes--;

totalcredits += credits;

}

qualitypoints = grade * credits;

// GPA is calculated via total credits / total quality points
//of which are calculated by multiplying the credit(s) * the quality point value


System.out.println("Your total credit(s)are: " + totalcredits);

System.out.println("Your total quality points are: " + qualitypoints);

System.out.println("Your GPA is: " + (qualitypoints / totalcredits));
}

}

/*

布鲁姆斯堡大学的评分标准

A = 4.00 卓越成就

A- = 3.67

B+ = 3.33

B = 3.00 高于平均水平

B-=2.67

C+ = 2.33

C = 2.00 平均成绩

C- = 1.67

D+=1.33

D = 1.00 最低达到

F = 0.00 失败

*/

最佳答案

 if (grade.toLowerCase() == "a") {

由于 "a"grade.toLowerCase() 是对象(String 类型),因此将它们与 == 进行比较 比较对象的引用,而不是字符串的内容。

使用equals()方法。

if (grade.toLowerCase().equals("a")) {

更多地使用equalsIgnoreCase方法来忽略大小写。

if (grade.equalsIgnoreCase("a")) {

关于Java字符串输入比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20160707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com