簡體   English   中英

為什么我的主要人不知道我退回了什么?

[英]Why won't my main recognize what I returned?

即使我在它們的方法中返回了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

你調用的方法electiveanswer沒有結果存儲到一個變量。

您應該使用一個變量來存儲結果:

sClassGpa= answer(input);
eClassGpa = elective(input);

您應該認識到在函數內部創建的局部變量,盡管它們的名稱與在主函數中聲明的名稱不同。 這就是為什么要使用函數的結果必須存儲在變量中的原因。

編輯

要清楚:您應該將這些行放在主方法的末尾

        }//End of Religion 
        answer(input);   //Edit this line...
        elective(input); //And this line

        double finalGrade;
        finalGrade= mathGpa + englishGpa+ SciGpa+ sClassGpa + eClassGpa;   
}//End of Main

將@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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM