我想更改输入内容,以确保用户输入的是整数而不是字母,并且程序可以将数字理解为机器上的数字(它将理解数字并将其识别为特定的GPA等级) 。我只了解如何输入字母和正负号(被枚举者识别为某些年级数字),但是我想颠倒此过程并输入数字(并被识别为年级字母)。有没有办法做到这一点?
import java.util.Scanner;
//imports scanner
public class Part1 {
enum LetterGrades {
A_PLUS ("A+", 4.33),
A ("A" , 4 ),
A_MINUS("A-", 3.67),
B_PLUS ("B+", 3.33),
B ("B" , 3 ),
B_MINUS("B-", 2.67),
C_PLUS ("C+", 2.33),
C ("C" , 2 ),
C_MINUS("C-", 1.67),
D ("D" , 1 ),
F ("F" , 0 ),
FX ("FX", 0 );
private final String grade_;
private final double value_;
//CONSTRUCTOR
private LetterGrades(String grade, double value) {
grade_ = grade;
value_ = value;
}
public static double getValueFromGrade(String grade) {
for (LetterGrades letterGrade : LetterGrades.values()) {
if (letterGrade.grade_.equals(grade)) {
return letterGrade.value_;
}
}
System.out.println("Invalid grade");
return 0.0;
}
}
public static void main (String args[]){
String grade = "";
double credit1;
double credit2;
double credit3;
double credit4;
double gradeValue=0;
//Creates 4 credits
double totPtsClass1=0;
double totPtsClass2=0;
double totPtsClass3=0;
double totPtsClass4=0;
double totPts=0;
double totalCredits= 0;
double gpa;
//Points in classes, GPA
System.out.println("Welcome to the UNG GPA Calculator!");
String message;
Scanner scan = new Scanner(System.in);
System.out.println("Enter your first name: ");
message = scan.nextLine();
System.out.println("Enter your last name: ");
message = scan.nextLine();
System.out.println("Enter your major: ");
message = scan.nextLine();
System.out.println("Enter the class number of your first class: ");
message = scan.nextLine();
Scanner console = new Scanner (System.in);
System.out.println("Please enter the number of credits of the class 1 (A number)");
credit1 = console.nextDouble();
System.out.println("Now you will enter the grade from the chart: 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. Please enter your grades for the class 1(Capital letters such as A,B+, C-)");
grade = console.next();
gradeValue = LetterGrades.getValueFromGrade(grade);
totPtsClass1 = gradeValue * credit1;
System.out.println("Enter the class number of your second class: ");
message = scan.nextLine();
System.out.println("Please enter the number of credits of the class 2 (A number)");
credit2 = console.nextDouble();
System.out.println("Please enter your grades for the class 2 (Capital letters such as A,B+, C-)");
grade = console.next();
gradeValue = LetterGrades.getValueFromGrade(grade);
totPtsClass2 = gradeValue * credit1;
System.out.println("Please enter the number of credits of the class 3 (A number)");
credit3 = console.nextDouble();
System.out.println("Please enter your grades for the class 3 (Capital letters such as A,B+, C-)");
grade = console.next();
gradeValue = LetterGrades.getValueFromGrade(grade);
totPtsClass3 = gradeValue * credit1;
System.out.println("Please enter the number of credits of the class 4 (A number)");
credit4 = console.nextDouble();
System.out.println("Please enter your grades for the class 4 (Capital letters such as A,B+, C-)");
grade = console.next();
gradeValue = LetterGrades.getValueFromGrade(grade);
totPtsClass4 = gradeValue * credit1;
totPts= totPtsClass1 + totPtsClass2 + totPtsClass3 + totPtsClass4;
totalCredits = credit1 + credit2 + credit3 + credit4;
gpa= totPts / totalCredits;
System.out.printf("Your GPA is: %.2f\n", + gpa);
}
}
我需要确保用户可以从枚举数列表中输入浮点数(输入的小数对应于某些GPA)如何将用户输入分配给String变量(程序将其读入并分配给正确命名的变量)?
答案 0 :(得分:0)
您可以在LetterGrades
枚举中添加以下内容:
public static LetterGrades fromValue(double value) {
Optional<LetterGrades> result =
Arrays.stream(LetterGrades.values())
.filter(grade -> Math.abs(grade.value_ - value) < 0.01)
.findFirst();
if (!result.isPresent()) {
System.out.println("Invalid grade value");
}
return result.orElse(FX);
}
答案 1 :(得分:0)
这是一个可能的解决方案:
LetterGrade.java
public enum LetterGrade {
A_PLUS ("A+", 4.33),
A ("A" , 4 ),
A_MINUS("A-", 3.67),
B_PLUS ("B+", 3.33),
B ("B" , 3 ),
B_MINUS("B-", 2.67),
C_PLUS ("C+", 2.33),
C ("C" , 2 ),
C_MINUS("C-", 1.67),
D ("D" , 1 ),
F ("F" , 0 ),
FX ("FX", 0 );
private final String grade;
private final double value;
public String getGrade() {
return grade;
}
public double getValue() {
return value;
}
private LetterGrade(String grade, double value) {
this.grade = grade;
this.value = value;
}
public static LetterGrade getLetterGrade(String grade) {
for (LetterGrade letterGrade : LetterGrade.values()) {
if (letterGrade.grade.equals(grade)) {
return letterGrade;
}
}
return null;
}
public static LetterGrade getLetterGrade(double value) {
for (LetterGrade letterGrade : LetterGrade.values()) {
if (letterGrade.value <= value) {
return letterGrade;
}
}
return null;
}
}