我正在创建一个程序,我要求用户输入一个字母等级,然后打印出该等级的百分比范围的输出。例如,A +为95%至100%。我的程序编译但是当我运行它时,我获得了A +和A-的相同输出。此外,如果我输入A,它甚至不会工作。我不明白这个的原因。请帮忙。这是我的所有代码。它有3个课程。
public class gradesConverter {
public static void main (String []args) {
userI i = new userI();
i.inputOutput();
}
}
import java.util.Scanner;
class userI {
public void inputOutput() {
java.util.Scanner input=new java.util.Scanner(System.in);
String letterGrade = "A";
System.out.println("Enter your letter Grade");
letterGrade = input.nextLine();
char charGrade = letterGrade.charAt(0);
char charGrade2 = letterGrade.charAt(1);
converter conv = new converter();
conv.grades(charGrade, charGrade2);
}
}
public class converter {
public void grades(char charGrade, char charGrade2) {
switch (charGrade2) {
case '-':
switch (charGrade) {
case 'A': System.out.println ("Your percentage grade ranges from 80-86.");
break;
case 'B': System.out.println ("Your percentage grade ranges from 70-72.") ;
break;
case 'C': System.out.println ("Your percentage grade ranges from 60-62.") ;
break;
case 'D': System.out.println ("Your percentage grade ranges from 50-52.") ;
break;
}
}
switch (charGrade2) {
case '+':
switch (charGrade) {
case 'A': System.out.println ( "Your percentage grade ranges from 95-100.");
break;
case 'B': System.out.println ( "Your percentage grade ranges from 77-79.") ;
break;
case 'C': System.out.println ("Your percentage grade ranges from 67-69.") ;
break;
case 'D': System.out.println ( "Your percentage grade ranges from 57-59.") ;
break;
default: System.out.println ("Please enter a proper letter grade.");
}
}
switch (charGrade) {
case 'A': System.out.println ("Your percentage grade ranges from 87-94.");
break;
case 'B': System.out.println ("Your percentage grade ranges from 73-77.") ;
break;
case 'C': System.out.println ("Your percentage grade ranges from 63-66.") ;
break;
case 'D': System.out.println ("Your percentage grade ranges from 53-56.") ;
break;
case 'F': System.out.println ("Your percentage grade ranges from 0-49.") ;
break;
default: System.out.println ("Please enter a proper letter grade.");
}
}
}
答案 0 :(得分:1)
您需要修复一些错误。
chargrade2 = letterGrade.charAt(0)
更改为chargrade2 = letterGrade.charAt(1)
这是错误的原因是因为您正在检索用户为两个chargrade
变量输入的第一个字符。因此,当您输入“A”时,它不起作用,因为chargrade2
也等于'A'。
switch (chargrade2) { case '-': //Code goes here break; case '+': //Code goes here break; default: break; }
此外,请将代码直接粘贴到问题中。导航到这些图像链接有点痛苦。