두 가지 질문. 3 개 이상의 'if'문을 사용할 때 switch 문을 사용하는 것이 가장 좋습니다. 그럴까요? 둘째 Java : 다중 표현식에 switch 문 사용?
는, 누군가가 다음 코드를 검토하고 변수 '등급은'항상while (!"Y".equalsIgnoreCase(exit))
{
i++;
System.out.print("Please enter name of assignment " + i + ": ");
assignment = user.nextLine().toUpperCase(); //consumes string + \n
System.out.print("Please enter points earned: ");
earned = user.nextDouble();//consumes double
user.nextLine();//consumes \n
System.out.print("Please enter total points possible: ");
total = user.nextDouble();
user.nextLine();
System.out.print("Assignment\t"); System.out.print("Score\t"); System.out.print("Total Points\t"); System.out.print("Percentage\t\n");
System.out.print(assignment + "\t\t"); System.out.print(earned + "\t"); System.out.print(total + "\t\t"); System.out.print(percent.format(earned/total) + "\n\t");
// Putting user data into an array for calculation:
double[] calc; calc = new double[4];
calc[i] = earned; // Array elements coincide with the current iteration loop
double[] totalPoints; totalPoints = new double[4];
totalPoints[i] = total;
for (double e : calc) // adds up all the stored data in the array
sum += e;
for (double f : totalPoints) // adds up all the stored data in the array
tot += f;
char grade = '0';
if (sum/tot >= 90)
{
grade = 'A';
}
else if (sum/tot >= 80 && sum/tot <= 89.99)
{
grade = 'B';
}
else if (sum/tot >= 70 && sum/tot <= 79.99)
{
grade = 'C';
}
else if (sum/tot >= 60 && sum/tot <= 69.99)
{
grade = 'D';
}
else if (sum/tot <= 59.99)
{
grade = 'F';
}
System.out.println("Your total is " + sum + " out of " + tot + ", or " + percent.format(sum/tot) + ", and your grade is an " + grade);
}
F.
결과 이유 다 표현되고 또는 B가 설명이기는하지만 switch 문으로 변환하는 방법 A에 나에게 아이디어를 줄 수
변수에 보유하고있는 소리가 들리면 모든 임계 값을 '100'으로 나누어야합니다. – Keppil
'sum'및 'tot'의 값을 모른 채 어떻게 의견을 말 할 수 있습니까? –
'switch'는 범위가 아닌 구체적인 값으로 작업 중입니다. – nikis