我正在尝试完成这项Java任务,而且我最后一点陷入困境。当用户输入字母等级以计算他们的G.P.A时,我需要这样做。他们只能输入一个字母。
例如,如果他们输入aaa
而不是a
,则需要收到错误消息。
我被困在如何去做这件事。除了这一件事,一切都有效。我是Java的新手,所以任何帮助都会很棒。
这是我的班级:
public class Gpa {
private int sumCredits;
private int sumPoints;
public int getPointsForGrade(char letter) {
int gradepoints;
switch (letter)
{
case 'A':
case 'a':
gradepoints = 4;
break;
case 'B':
case 'b':
gradepoints = 3;
break;
case 'C':
case 'c':
gradepoints = 2;
break;
case 'D':
case 'd':
gradepoints = 1;
break;
case 'F':
case 'f':
gradepoints = 0;
break;
default:
gradepoints = -1;
break;
}
return gradepoints;
}
public void Constructor(){
sumCredits = 0;
sumPoints = 0;
return;
}
public void addGrade( char letter, int credits){
sumCredits = sumCredits + credits;
sumPoints = sumPoints + getPointsForGrade(letter) * credits;
}
public double calcGPA(){
double GPA;
if (sumCredits == 0){
return 0;
}
GPA = (double)sumPoints / sumCredits;
return GPA;
}
public int totalCredits(){
return sumCredits;
}
public int totalPoints(){
return sumPoints;
}
}
这是我的测试类,或者是gpa计算器
import java.util.*;
import java.util.Scanner;
public class ComputeGpa {
public static void main (String [] args){
Scanner scan = new Scanner(System.in);
Gpa gpa = new Gpa();
int courses;
int credits;
char letter;
while (true){
System.out.println(" Enter number of courses: ");
courses = scan.nextInt();
if (courses > 0){
break;
}
else {
System.out.println(" Invalid number of courses - must be greater than 0");
}
}
for (int i = 1; i<= courses; i++)
{
while (true){
System.out.println(" Enter Grade ( one character )");
String grade = scan.next();
letter = grade.toUpperCase().charAt(0);
if (letter == 'A' || letter == 'B' || letter == 'C' || letter =='D' || letter == 'F' ){
break;
}
else {
System.out.println(" must enter A,B,C,D,F (Upper or Lower case)");
}
if (letter != 'A' || letter != 'B' || letter != 'C' || letter != 'D' || letter != 'F'){
System.out.println("must be 1 letter only");
}
}
while (true){
System.out.println(" Enter Credits: ");
credits = scan.nextInt();
if (credits < 0 || credits > 9){
System.out.println("Invalid credits = must be between 0 and 9, inclusively");
}
else {
break;
}
}
gpa.addGrade(letter, credits);
System.out.println(" Points: " + gpa.getPointsForGrade(letter) );
System.out.println(" Sum Points: " + gpa.totalPoints() + " Sum Credits: " + gpa.totalCredits());
}
System.out.printf(" GPA: %.2f%n", gpa.calcGPA());
}
}
答案 0 :(得分:0)
您可以检查输入中是否有更多条目。我没有运行此代码,但这是一种简单的检查方法。
System.out.println(" Enter Grade ( one character )");
String grade = scan.next();
letter = grade.toUpperCase().charAt(0);
if(grade.length() > 1){ // check the size of the input
System.out.println("You entered too many letters");
}
else if (letter == 'A' || letter == 'B' || letter == 'C' || letter =='D' || letter == 'F' ){
break;
}
else {
System.out.println(" must enter A,B,C,D,F (Upper or Lower case)");
}
答案 1 :(得分:0)
您运行scan.next()
并将其存储到字符串中的位置,您可以检查字符串的长度。
if (grade.length() > 1) {
// show an error or throw an exception or smth
System.out.println("Error, too many chars"); // do this if you want to print an error
throw new Exception("TOO MANY CHARS"); // do this and don't catch it if you want to crash the program
}
编辑:对于OP的要求,如果错误则再次要求等级
String grade = "x"; // set the base case that will never occur
while (!grade.equals("x")) { // check if it is x - if it's not x, continue
if ((grade = scan.next()).length() == 1) {
// check the length - if it's 1 letter
// check the letter here. If it's not a valid letter,
// set grade back to x
} else {
// if you're here, it wasn't 1 letter.
// print an error msg
System.out.println("too many chars");
// Therefore, set grade back to x so that we ask again
grade = "x";
}
}