我无法让我的程序识别eClassGpa和sClassGpa,即使我在他们的方法中返回它们。我试图将主要的GPA添加到单独的方法中。当我这样做时,我返回的两件事会产生错误。
import java.util.Scanner;
public class GPA3 {
public static void main(String[] args)
{
System.out.println("Hello! Welcome to the GPA Calculator.");
int Math;
double mathGpa = 0;
System.out.println("Math = ");
Scanner input = new Scanner(System.in);
Math= input.nextInt();
if (Math >100){
System.out.println("You have mistyped something");
}
if (Math >= 94){
System.out.println("You have an A");
mathGpa = 4.0;
System.out.println(mathGpa);
}
if (Math < 94 && Math >=90){
System.out.println("You have an A-");
mathGpa = 3.7;
System.out.println(mathGpa);
}
if (Math < 90 && Math >=87){
System.out.println("You have a B+");
mathGpa = 3.3;
System.out.println(mathGpa);
}
if (Math < 87 && Math >=80){
System.out.println("You have a B");
mathGpa = 3.0;
System.out.println(mathGpa);
}
if (Math < 80 && Math >=77){
System.out.println("You have a B-");
mathGpa = 2.7;
System.out.println(mathGpa);
}
if (Math < 77 && Math >=73){
System.out.println("You have a C+");
mathGpa = 2.3;
System.out.println(mathGpa);
}
if (Math < 73 && Math >=70){
System.out.println("You have a C");
mathGpa = 2.0;
System.out.println(mathGpa);
}
if (Math < 70 && Math >=67){
System.out.println("You have a C-");
mathGpa = 1.7;
System.out.println(mathGpa);
}
if (Math < 67 && Math >=67){
System.out.println("You have a D+");
mathGpa = 1.3;
System.out.println(mathGpa);
}
if (Math < 67 && Math >=63){
System.out.println("You have a D");
mathGpa = 1.0;
System.out.println(mathGpa);
}
if (Math < 63 && Math >=60){
System.out.println("You have a D-");
mathGpa = 0.7;
System.out.println(mathGpa);
}
if (Math < 60){
System.out.println("You have a F");
mathGpa = 0.0;
System.out.println(mathGpa);
}//End of Math
int English;
double englishGpa = 0;
System.out.println("English = ");
English= input.nextInt();
if (English >100){
System.out.println("You have mistyped something");
}
if (English >= 94 && English <101){
System.out.println("You have an A");
englishGpa = 4.0;
System.out.println(englishGpa);
}
if (English < 94 && English >=90){
System.out.println("You have an A-");
englishGpa = 3.7;
System.out.println(englishGpa);
}
if (English < 90 && English >=87){
System.out.println("You have a B+");
englishGpa = 3.3;
System.out.println(englishGpa);
}
if (English < 87 && English >=80){
System.out.println("You have a B");
englishGpa = 3.0;
System.out.println(englishGpa);
}
if (English < 80 && English >=77){
System.out.println("You have a B-");
englishGpa = 2.7;
System.out.println(englishGpa);
}
if (English < 77 && English >=73){
System.out.println("You have a C+");
englishGpa = 2.3;
System.out.println(englishGpa);
}
if (English < 73 && English >=70){
System.out.println("You have a C");
englishGpa = 2.0;
System.out.println(englishGpa);
}
if (English < 70 && English >=67){
System.out.println("You have a C-");
englishGpa = 1.7;
System.out.println(englishGpa);
}
if (English < 67 && English >=67){
System.out.println("You have a D+");
englishGpa = 1.3;
System.out.println(englishGpa);
}
if (English < 67 && English >=63){
System.out.println("You have a D");
englishGpa = 1.0;
System.out.println(englishGpa);
}
if (English < 63 && English >=60){
System.out.println("You have a D-");
englishGpa = 0.7;
System.out.println(englishGpa);
}
if (English < 60){
System.out.println("You have a F");
englishGpa = 0.0;
System.out.println(englishGpa);
}//End of English
int Science;
double SciGpa = 0;
System.out.println("Science = ");
Science= input.nextInt();
if (Science >100){
System.out.println("You have mistyped something");
}
if (Science >= 94 && Science <101){
System.out.println("You have an A");
SciGpa = 4.0;
System.out.println(SciGpa);
}
if (Science < 94 && Science >=90){
System.out.println("You have an A-");
SciGpa = 3.7;
System.out.println(SciGpa);
}
if (Science < 90 && Science >=87){
System.out.println("You have a B+");
SciGpa = 3.3;
System.out.println(SciGpa);
}
if (Science < 87 && Science >=80){
System.out.println("You have a B");
SciGpa = 3.0;
System.out.println(SciGpa);
}
if (Science < 80 && Science >=77){
System.out.println("You have a B-");
SciGpa = 2.7;
System.out.println(SciGpa);
}
if (Science < 77 && Science >=73){
System.out.println("You have a C+");
SciGpa = 2.3;
System.out.println(SciGpa);
}
if (Science < 73 && Science >=70){
System.out.println("You have a C");
SciGpa = 2.0;
System.out.println(SciGpa);
}
if (Science < 70 && Science >=67){
System.out.println("You have a C-");
SciGpa = 1.7;
System.out.println(SciGpa);
}
if (Science < 67 && Science >=67){
System.out.println("You have a D+");
SciGpa = 1.3;
System.out.println(SciGpa);
}
if (Science < 67 && Science >=63){
System.out.println("You have a D");
SciGpa = 1.0;
System.out.println(SciGpa);
}
if (Science < 63 && Science >=60){
System.out.println("You have a D-");
SciGpa = 0.7;
System.out.println(SciGpa);
}
if (Science < 60){
System.out.println("You have a F");
SciGpa = 0.0;
System.out.println(SciGpa);
}//End of Science
int Spanish;
double SpanGpa = 0;
System.out.println("Spanish = ");
Spanish= input.nextInt();
if (Spanish >100){
System.out.println("You have mistyped something");
}
if (Spanish >= 94 && Spanish <101){
System.out.println("You have an A");
SpanGpa = 4.0;
System.out.println(SpanGpa);
}
if (Spanish < 94 && Spanish >=90){
System.out.println("You have an A-");
SpanGpa = 3.7;
System.out.println(SpanGpa);
}
if (Spanish < 90 && Spanish >=87){
System.out.println("You have a B+");
SpanGpa = 3.3;
System.out.println(SpanGpa);
}
if (Spanish < 87 && Spanish >=80){
System.out.println("You have a B");
SpanGpa = 3.0;
System.out.println(SpanGpa);
}
if (Spanish < 80 && Spanish >=77){
System.out.println("You have a B-");
SpanGpa = 2.7;
System.out.println(SpanGpa);
}
if (Spanish < 77 && Spanish >=73){
System.out.println("You have a C+");
SpanGpa = 2.3;
System.out.println(SpanGpa);
}
if (Spanish < 73 && Spanish >=70){
System.out.println("You have a C");
SpanGpa = 2.0;
System.out.println(SpanGpa);
}
if (Spanish < 70 && Spanish >=67){
System.out.println("You have a C-");
SpanGpa = 1.7;
System.out.println(SpanGpa);
}
if (Spanish < 67 && Spanish >=67){
System.out.println("You have a D+");
SpanGpa = 1.3;
System.out.println(SpanGpa);
}
if (Spanish < 67 && Spanish >=63){
System.out.println("You have a D");
SpanGpa = 1.0;
System.out.println(SpanGpa);
}
if (Spanish < 63 && Spanish >=60){
System.out.println("You have a D-");
SpanGpa = 0.7;
System.out.println(SpanGpa);
}
if (Spanish < 60){
System.out.println("You have a F");
SpanGpa = 0.0;
System.out.println(SpanGpa);
}//End of Spanish
int Religion;
double RGpa = 0;
System.out.println("Religion = ");
Religion= input.nextInt();
if (Religion >100){
System.out.println("You have mistyped something");
}
if (Religion >= 94 && Religion < 101){
System.out.println("You have an A");
RGpa = 4.0;
System.out.println(RGpa);
}
if (Religion < 94 && Religion >=90){
System.out.println("You have an A-");
RGpa = 3.7;
System.out.println(RGpa);
}
if (Religion < 90 && Religion >=87){
System.out.println("You have a B+");
RGpa = 3.3;
System.out.println(RGpa);
}
if (Religion < 87 && Religion >=80){
System.out.println("You have a B");
RGpa = 3.0;
System.out.println(RGpa);
}
if (Religion < 80 && Religion >=77){
System.out.println("You have a B-");
RGpa = 2.7;
System.out.println(RGpa);
}
if (Religion < 77 && Religion >=73){
System.out.println("You have a C+");
RGpa = 2.3;
System.out.println(RGpa);
}
if (Religion < 73 && Religion >=70){
System.out.println("You have a C");
RGpa = 2.0;
System.out.println(RGpa);
}
if (Religion < 70 && Religion >=67){
System.out.println("You have a C-");
RGpa = 1.7;
System.out.println(RGpa);
}
if (Religion < 67 && Religion >=67){
System.out.println("You have a D+");
RGpa = 1.3;
System.out.println(RGpa);
}
if (Religion < 67 && Religion >=63){
System.out.println("You have a D");
RGpa = 1.0;
System.out.println(RGpa);
}
if (Religion < 63 && Religion >=60){
System.out.println("You have a D-");
RGpa = 0.7;
System.out.println(RGpa);
}
if (Religion < 60){
System.out.println("You have a F");
RGpa = 0.0;
System.out.println(RGpa);
}//End of Religion
answer(input);
elective(input);
double finalGrade;
finalGrade= mathGpa + englishGpa+ SciGpa+ sClassGpa + eClassGpa;
}//End of Main
public static double answer(Scanner input)
{//Begin answer
double sClassGpa=0;
System.out.println("Do you have gym this semester?");
input.nextLine();
String yes = "yes";
String no = "no";
String answer = input.nextLine();
if (answer.equals(yes)){
int Gym;
System.out.println("Gym = ");
Gym= input.nextInt();
if (Gym >100){
System.out.println("You have mistyped something");
}
if (Gym >= 94 && Gym < 101){
System.out.println("You have an A");
sClassGpa = 4.0;
System.out.println(sClassGpa);
}
if (Gym < 94 && Gym >=90){
System.out.println("You have an A-");
sClassGpa = 3.7;
System.out.println(sClassGpa);
}
if (Gym < 90 && Gym >=87){
System.out.println("You have a B+");
sClassGpa = 3.3;
System.out.println(sClassGpa);
}
if (Gym < 87 && Gym >=80){
System.out.println("You have a B");
sClassGpa = 3.0;
System.out.println(sClassGpa);
}
if (Gym < 80 && Gym >=77){
System.out.println("You have a B-");
sClassGpa = 2.7;
System.out.println(sClassGpa);
}
if (Gym < 77 && Gym >=73){
System.out.println("You have a C+");
sClassGpa = 2.3;
System.out.println(sClassGpa);
}
if (Gym < 73 && Gym >=70){
System.out.println("You have a C");
sClassGpa = 2.0;
System.out.println(sClassGpa);
}
if (Gym < 70 && Gym >=67){
System.out.println("You have a C-");
sClassGpa = 1.7;
System.out.println(sClassGpa);
}
if (Gym < 67 && Gym >=67){
System.out.println("You have a D+");
sClassGpa = 1.3;
System.out.println(sClassGpa);
}
if (Gym < 67 && Gym >=63){
System.out.println("You have a D");
sClassGpa = 1.0;
System.out.println(sClassGpa);
}
if (Gym < 63 && Gym >=60){
System.out.println("You have a D-");
sClassGpa = 0.7;
System.out.println(sClassGpa);
}
if (Gym < 60){
System.out.println("You have a F");
sClassGpa = 0.0;
System.out.println(sClassGpa);
}
}//End Gym
if (answer.equals(no)){
int History;
System.out.println("History = ");
History= input.nextInt();
if (History >100){
System.out.println("You have mistyped something");
}
if (History >= 94 && History <101){
System.out.println("You have an A");
sClassGpa = 4.0;
System.out.println(sClassGpa);
}
if (History < 94 && History >=90){
System.out.println("You have an A-");
sClassGpa = 3.7;
System.out.println(sClassGpa);
}
if (History < 90 && History >=87){
System.out.println("You have a B+");
sClassGpa = 3.3;
System.out.println(sClassGpa);
}
if (History < 87 && History >=80){
System.out.println("You have a B");
sClassGpa = 3.0;
System.out.println(sClassGpa);
}
if (History < 80 && History >=77){
System.out.println("You have a B-");
sClassGpa = 2.7;
System.out.println(sClassGpa);
}
if (History < 77 && History >=73){
System.out.println("You have a C+");
sClassGpa = 2.3;
System.out.println(sClassGpa);
}
if (History < 73 && History >=70){
System.out.println("You have a C");
sClassGpa = 2.0;
System.out.println(sClassGpa);
}
if (History < 70 && History >=67){
System.out.println("You have a C-");
sClassGpa = 1.7;
System.out.println(sClassGpa);
}
if (History < 67 && History >=67){
System.out.println("You have a D+");
sClassGpa = 1.3;
System.out.println(sClassGpa);
}
if (History < 67 && History >=63){
System.out.println("You have a D");
sClassGpa = 1.0;
System.out.println(sClassGpa);
}
if (History < 63 && History >=60){
System.out.println("You have a D-");
sClassGpa = 0.7;
System.out.println(sClassGpa);
}
if (History < 60){
System.out.println("You have a F");
sClassGpa = 0.0;
System.out.println(sClassGpa);
}//End of History
}
return (sClassGpa);
}//End answer
public static double elective(Scanner input)
{//Begin answer
double eClassGpa=0;
System.out.println("Do you have one or two electives this year?");
System.out.println("Hint: write answers in word form");
input.nextLine();
String one = "one";
String two = "two";
String elective = input.nextLine();
if (elective.equals(one)){
int electiveOne;
System.out.println("Elective 1 = ");
electiveOne= input.nextInt();
if (electiveOne >100){
System.out.println("You have mistyped something");
}
if (electiveOne >= 94 && electiveOne < 101){
System.out.println("You have an A");
eClassGpa = 4.0;
System.out.println(eClassGpa);
}
if (electiveOne < 94 && electiveOne >=90){
System.out.println("You have an A-");
eClassGpa = 3.7;
System.out.println(eClassGpa);
}
if (electiveOne < 90 && electiveOne >=87){
System.out.println("You have a B+");
eClassGpa = 3.3;
System.out.println(eClassGpa);
}
if (electiveOne < 87 && electiveOne >=80){
System.out.println("You have a B");
eClassGpa = 3.0;
System.out.println(eClassGpa);
}
if (electiveOne < 80 && electiveOne >=77){
System.out.println("You have a B-");
eClassGpa = 2.7;
System.out.println(eClassGpa);
}
if (electiveOne < 77 && electiveOne >=73){
System.out.println("You have a C+");
eClassGpa = 2.3;
System.out.println(eClassGpa);
}
if (electiveOne < 73 && electiveOne >=70){
System.out.println("You have a C");
eClassGpa = 2.0;
System.out.println(eClassGpa);
}
if (electiveOne < 70 && electiveOne >=67){
System.out.println("You have a C-");
eClassGpa = 1.7;
System.out.println(eClassGpa);
}
if (electiveOne < 67 && electiveOne >=67){
System.out.println("You have a D+");
eClassGpa = 1.3;
System.out.println(eClassGpa);
}
if (electiveOne < 67 && electiveOne >=63){
System.out.println("You have a D");
eClassGpa = 1.0;
System.out.println(eClassGpa);
}
if (electiveOne < 63 && electiveOne >=60){
System.out.println("You have a D-");
eClassGpa = 0.7;
System.out.println(eClassGpa);
}
if (electiveOne < 60){
System.out.println("You have a F");
eClassGpa = 0.0;
System.out.println(eClassGpa);
}
}//End Elective 1
if(elective.equals(two)){
double e1ClassGpa=0;
double e2ClassGpa=0;
if (elective.equals(two)){
int electiveOne;
System.out.println("Elective 1 = ");
electiveOne= input.nextInt();
if (electiveOne >100){
System.out.println("You have mistyped something");
}
if (electiveOne >= 94 && electiveOne < 101){
System.out.println("You have an A");
e1ClassGpa = 4.0;
System.out.println(e1ClassGpa);
}
if (electiveOne < 94 && electiveOne >=90){
System.out.println("You have an A-");
e1ClassGpa = 3.7;
System.out.println(e1ClassGpa);
}
if (electiveOne < 90 && electiveOne >=87){
System.out.println("You have a B+");
e1ClassGpa = 3.3;
System.out.println(e1ClassGpa);
}
if (electiveOne < 87 && electiveOne >=80){
System.out.println("You have a B");
e1ClassGpa = 3.0;
System.out.println(e1ClassGpa);
}
if (electiveOne < 80 && electiveOne >=77){
System.out.println("You have a B-");
e1ClassGpa = 2.7;
System.out.println(e1ClassGpa);
}
if (electiveOne < 77 && electiveOne >=73){
System.out.println("You have a C+");
e1ClassGpa = 2.3;
System.out.println(e1ClassGpa);
}
if (electiveOne < 73 && electiveOne >=70){
System.out.println("You have a C");
e1ClassGpa = 2.0;
System.out.println(e1ClassGpa);
}
if (electiveOne < 70 && electiveOne >=67){
System.out.println("You have a C-");
e1ClassGpa = 1.7;
System.out.println(e1ClassGpa);
}
if (electiveOne < 67 && electiveOne >=67){
System.out.println("You have a D+");
e1ClassGpa = 1.3;
System.out.println(e1ClassGpa);
}
if (electiveOne < 67 && electiveOne >=63){
System.out.println("You have a D");
e1ClassGpa = 1.0;
System.out.println(e1ClassGpa);
}
if (electiveOne < 63 && electiveOne >=60){
System.out.println("You have a D-");
e1ClassGpa = 0.7;
System.out.println(e1ClassGpa);
}
if (electiveOne < 60){
System.out.println("You have a F");
e1ClassGpa = 0.0;
System.out.println(e1ClassGpa);
}
}//End Elective 1
int elective2;
System.out.println("Elective 2 = ");
elective2= input.nextInt();
if (elective2 >100){
System.out.println("You have mistyped something");
}
if (elective2 >= 94 && elective2 <101){
System.out.println("You have an A");
e2ClassGpa = 4.0;
System.out.println(e2ClassGpa);
}
if (elective2 < 94 && elective2 >=90){
System.out.println("You have an A-");
e2ClassGpa = 3.7;
System.out.println(e2ClassGpa);
}
if (elective2 < 90 && elective2 >=87){
System.out.println("You have a B+");
e2ClassGpa = 3.3;
System.out.println(e2ClassGpa);
}
if (elective2 < 87 && elective2 >=80){
System.out.println("You have a B");
e2ClassGpa = 3.0;
System.out.println(e2ClassGpa);
}
if (elective2 < 80 && elective2 >=77){
System.out.println("You have a B-");
e2ClassGpa = 2.7;
System.out.println(e2ClassGpa);
}
if (elective2 < 77 && elective2 >=73){
System.out.println("You have a C+");
e2ClassGpa = 2.3;
System.out.println(e2ClassGpa);
}
if (elective2 < 73 && elective2 >=70){
System.out.println("You have a C");
e2ClassGpa = 2.0;
System.out.println(e2ClassGpa);
}
if (elective2 < 70 && elective2 >=67){
System.out.println("You have a C-");
e2ClassGpa = 1.7;
System.out.println(e2ClassGpa);
}
if (elective2 < 67 && elective2 >=67){
System.out.println("You have a D+");
e2ClassGpa = 1.3;
System.out.println(e2ClassGpa);
}
if (elective2 < 67 && elective2 >=63){
System.out.println("You have a D");
e2ClassGpa = 1.0;
System.out.println(e2ClassGpa);
}
if (elective2 < 63 && elective2 >=60){
System.out.println("You have a D-");
e2ClassGpa = 0.7;
System.out.println(e2ClassGpa);
}
if (elective2 < 60){
System.out.println("You have a F");
e2ClassGpa = 0.0;
System.out.println(e2ClassGpa);
}//End of Elective 2
eClassGpa= e1ClassGpa + e2ClassGpa;
}//End of ElectiveTwo
input.close();
return (eClassGpa);
}//End of Elective
}//End of Class
答案 0 :(得分:0)
您调用方法elective
和answer
而不将结果存储到变量中。
您应该使用变量来存储结果:
sClassGpa= answer(input);
eClassGpa = elective(input);
您应该意识到您在函数内创建的局部变量虽然它们具有相同的名称,但与您在main函数中声明的变量不同。这就是为什么你必须将函数的结果存储在一个变量中的原因。
修改强>
要明确:您应该将这些行放在主要方法的末尾
}//End of Religion
answer(input); //Edit this line...
elective(input); //And this line
double finalGrade;
finalGrade= mathGpa + englishGpa+ SciGpa+ sClassGpa + eClassGpa;
}//End of Main
答案 1 :(得分:0)
将@ MiltoxBeyond的答案放到上下文中:
public static void main(String[] args){
// your main method
// End of Religion
double sClassGpa = answer(input);
double eClassGpa = elective(input);
double finalGrade = mathGpa + englishGpa+ SciGpa+ sClassGpa + eClassGpa;
}