为什么这段代码不能进入if语句?
public class GradeCalculator {
public static void main(String[] args) {
/*
* A 900 - 1000(90% to 100%) B 800 - 899(80% to 89%)
* C 700 - 799(70% to 79%) D 600 - 699(60% to 69%)
* F 599 and below (Below 60%)
*/
String Name = JOptionPane.showInputDialog("Please enter your name: ");
String pointsEarned = JOptionPane.showInputDialog("Please enter the points earned: ");
String possiblePoints = JOptionPane.showInputDialog("Please enter the total points possible: ");
double pe = Double.parseDouble(pointsEarned);
double pp = Double.parseDouble(possiblePoints);
double grade = (pe/pp)*100;
char LetterGrade;
if(grade>=900){
LetterGrade = 'A';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you for an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
}
}
}
答案 0 :(得分:2)
您计算的百分比不会超过100.因此,只要将条件更改为 if(grade> = 90)
答案 1 :(得分:1)
你可以简单地调试并看看那里发生了什么。添加其他块并查看等级的值。
if(grade>=900){
LetterGrade = 'A';
// ....
}else{
System.out.print(grade);
}
答案 2 :(得分:0)
您的成绩变量是一个百分比,您将其比较为您计算百分比的分数。所以看起来你想要的只是使用pe
而不是grade
变量。
if(pe>=900){
然后仅显示
时的百分比double percentage = (pe/pp)*100
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + percentage + "%" + " and you for an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
在测试时将grade
变量视为百分比
if (grade>=90) //90%
同时强>
变量应始终以小写字母开头,因此LetterGrade
应为letterGrade
答案 3 :(得分:0)
import javax.swing.JOptionPane;
public class GradeCalculator
{
public static void main(String[] args)
{
boolean exitLoop = false;
while(exitLoop == false)
{
String Name = JOptionPane.showInputDialog("Please enter your name: ");
String pointsEarned = JOptionPane.showInputDialog("Please enter the points earned: ");
String possiblePoints = JOptionPane.showInputDialog("Please enter the total points possible: ");
double pe = Double.parseDouble(pointsEarned);
double pp = Double.parseDouble(possiblePoints);
double grade = (pe*100)/pp;
double Lgrade = pe;
char LetterGrade;
if(Lgrade >= 900)
{
LetterGrade = 'A';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
} else if (Lgrade <899 && Lgrade >=800)
{
LetterGrade = 'B';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
} else if (Lgrade <799 && Lgrade >=700)
{
LetterGrade = 'C';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
} else if (Lgrade <699 && Lgrade >=600)
{
LetterGrade = 'D';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
} else if(Lgrade <599)
{
LetterGrade = 'F';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
}
int selectedOption = JOptionPane.showConfirmDialog(null, "Do you want to run the program again?", "Yes or No", JOptionPane.YES_NO_OPTION);
if(selectedOption == JOptionPane.YES_OPTION)
{
exitLoop = false;
}
else
{
exitLoop = true;
}
}
}
}