几周前我几乎没有开始编码,并为我的高中计算机科学课程设计了一个项目,要求我制作一个GPA计算器。它不是现在最好的代码或计算GPA的最佳技术,但它是一个开始(如果有人可以帮助清理并简化它,我将非常感谢你。)
但是代码存在问题。每次从控制台需要用户输入时,他们必须按两次输入。我觉得解决方案非常简单,但我仍然坚持为什么会发生这种情况。
import java.util.Scanner;
public class gpaCalc {
public static double gpa;
public static String grade = "";
public static double gpaTotal = 0;
public static double points = 0;
public static double numberOfClasses;
public static double class1points;
public static double class2points;
public static double class3points;
public static double class4points;
public static double class5points;
public static double class6points;
public static double class7points;
public static double class8points;
static Scanner letterGrade = new Scanner(System.in);
static Scanner numClasses = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Adrian's GPA calculator.");
System.out.println("This program will calculate your weighted and unweighted GPA for this school year.");
classesNumber();
}
public static void classesNumber() {
System.out.println("Please enter the number of classes that you are enrolled in for the school year.");
numberOfClasses = numClasses.nextDouble(); //input is stored in variable numberOfClasses
if(numberOfClasses >= 9 || numberOfClasses < 2)
System.out.println("Are you sure you go to ECHS?");
//limit the number of classes from 2-8
if (numberOfClasses <= 8 && numberOfClasses >= 2)
System.out.println("Please enter the letter grade you have recieved in class 1 (Uppercase letters i.e. A,B,C,D,F).");
else
classesNumber(); //error occurs and doesn't reset when a letter is put and not a number
class1();
}
public static void class1() {
grade = letterGrade.next(); //unweighted GPA finder
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class1 ();
}
class1points = points;
class2();
}
public static void class2() {
System.out.println("Please enter the letter grade you have recieved in class 2 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class2();
}
class2points = points;
class3();
}
public static void class3() {
System.out.println("Please enter the letter grade you have recieved in class 3 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class3();
}
class3points = points;
class4();
}
public static void class4() {
System.out.println("Please enter the letter grade you have recieved in class 4 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class4();
}
class4points = points;
class5();
}
public static void class5() {
System.out.println("Please enter the letter grade you have recieved in class 5 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class5();
}
class5points = points;
class6();
}
public static void class6() {
System.out.println("Please enter the letter grade you have recieved in class 6 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class6();
}
class6points = points;
class7();
}
public static void class7() {
System.out.println("Please enter the letter grade you have recieved in class 7 (Uppercase letters i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class7();
}
class7points = points;
class8();
}
public static void class8() {
System.out.println("Please enter the letter grade you have recieved in class 8 (Uppercase letter i.e. A,B,C,D,F).");
grade = letterGrade.next();
if (grade.equals("A"))
points = 4.0;
else if (grade.equals("B"))
points = 3.0;
else if (grade.equals("C"))
points = 2.0;
else if (grade.equals("D"))
points = 1.0;
else if (grade.equals("F"))
points = 0;
else {
System.out.println("Invalid entry. Please enter a valid entry.");
class8();
}
class8points = points;
calculator();
}
public static void calculator() {
gpaTotal = (class1points + class2points + class3points + class4points + class5points + class6points + class7points + class8points ) / numberOfClasses;
System.out.println("Your unwweighted GPA is: " + gpaTotal);
}
}
答案 0 :(得分:0)
您的问题应该通过删除1个扫描程序和代码建议来解决:
使用header('Content-Type: text/html; charset=utf-8');
存储主题
String [] mySubjects = {&#34; sub_1&#34;,&#34; sub_2&#34;,&#34; sub_3&#34;,&#34; sub_4&#34;,..};
使用array
减少重复
loop
答案 1 :(得分:0)
创建了代码的简化版本,无需数组。 也许不是最好的,但如果你刚刚开始,它可以指导你避免重复代码。
我尝试按照您的方式组织一切,只是为了帮助您更清楚地看到代码。
也许你可以在classNumber()方法中为这段代码添加一个加号检查,以避免输入非整数值。 +将getClassPoints()方法拆分成某些部分以避免副作用。
public static char grade;
public static double gpaTotal = 0;
public static int numberOfClasses;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Adrian's GPA calculator.");
System.out.println("This program will calculate your weighted and unweighted GPA for this school year.");
classesNumber();
}
public static void classesNumber() {
System.out.println("Please enter the number of classes that you are enrolled in for the school year.");
numberOfClasses = scan.nextInt(); //input is stored in variable numberOfClasses
if(numberOfClasses >= 9 || numberOfClasses < 2)
System.out.println("Are you sure you go to ECHS?");
getClassPoints();
}
public static void getClassPoints () {
//You dont need the numberOfClasses as parameter because its static.
for (int i = 0; i < numberOfClasses; i++){
while (true){
System.out.println("Please enter the letter grade you have recieved in class " + (i + 1) +"(Uppercase letter i.e. A,B,C,D,F).");
grade = scan.next().charAt(0); //unweighted GPA finder
grade = Character.toLowerCase(grade);
if (grade == 'a'){
gpaTotal += 4;
break;
}
else if (grade == 'b'){
gpaTotal += 3;
break;
}
else if (grade == 'c'){
gpaTotal += 2;
break;
}
else if (grade == 'd'){
gpaTotal += 1;
break;
}
else if (grade == 'f'){
break;
}
System.out.println("Invalid entry. Please enter a valid entry.");;
}
}
if (gpaTotal > 0){
System.out.println("Your unwweighted GPA is: " + (gpaTotal / numberOfClasses));
}
else {
System.out.println("Too many F grade!");
}
}
}