-4

我正在尝试编写一个计算我的数学和英语 GPA 的程序。我无法让主识别我的两个花车,mathGpa并且englishGpa. 它告诉我让它们成为静态,但让它们成为静态意味着它们成为字符串,我需要它们保持双打。

import java.util.Scanner;
public class GPA {

    public static void main(String[] args)
    {
        double finalGpa=0;

        mathGpa();
        englishGpa();

        finalGpa= (mathGpa + englishGpa)/2;


    }

    public double mathGpa() {//Begin mathGpa
        int Math;
        double mathGpa = 0;
        System.out.println("Math = ");
        Scanner math = new Scanner(System.in);
        Math= math.nextInt();
        math.close();

        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 = 1.7;
            System.out.println(mathGpa);
        }

        return mathGpa;
    }//End mathGpa

    public double englishGpa() {//Begin englishGpa
        int English;
        double englishGpa = 0;
        System.out.println("English = ");
        Scanner english = new Scanner(System.in);
        English= english.nextInt();
        english.close();

        if (English >100){
            System.out.println("You have mistyped something");
        }
        if (English >= 94){
            englishGpa = 4.0;
        }
        if (English < 94 && English >=90){
            englishGpa = 3.7;
        }
        if (English < 90 && English >=87){
            englishGpa = 3.3;
        }
        if (English < 87 && English >=80){
            englishGpa = 3.0;
        }
        if (English < 80 && English >=77){
            englishGpa = 2.7;
        }
        if (English < 77 && English >=73){
            englishGpa = 2.3;
        }
        if (English < 73 && English >=70){
            englishGpa = 2.0;
        }
        if (English < 70 && English >=67){
            englishGpa = 1.7;
        }
        if (English < 67 && English >=67){
            englishGpa = 1.3;
        }
        if (English < 67 && English >=63){
            englishGpa = 1.0;
        }
        if (English < 63 && English >=60){
            englishGpa = 0.7;
        }
        if (English < 60){
            englishGpa = 1.7;
        }

        return englishGpa;
    }//End englishGpa

}//End Class
4

2 回答 2

0

使方法静态并不意味着它们成为字符串,它只是意味着可以调用该方法。当你在 mathGpa 和englishGpa 中声明'double' 时,你将返回类型设置为double,除非你用'String' 切换'double',否则它不会改变。此外,正如评论(@Greg Hewgill)中所说,您应该更改局部变量名称。

有关“静态”的更多信息,请单击此处

有关方法的更多信息,请单击此处

于 2015-06-30T01:21:41.463 回答
0

静态变量

静态变量的值对于类的所有对象(或实例)都是相同的。换句话说,您可以认为同一类的所有实例(对象)共享静态变量的单个副本。
示例 #1:

class StaticVariable
{
   static int counter=0;
   public void increment()
   {
       counter++;
   }
   public static void main(String args[])
   {
       StaticVariable objectA = new StaticVariable();
       StaticVariable objectB = new StaticVariable();
       objectA .increment();
       objectB .increment();
       System.out.println("objectA- counter is: "+ objectA.count);
       System.out.println("objectB- counter is: "+ objectA.count);
   }
}

输出:

objectA- counter is: 2
objectB- counter is: 2

类的两个对象共享静态变量计数器的相同副本;这就是为什么它们包含相同的计数器值。

而且我认为声明静态变量不会像您提到的那样改变您的变量类型 - 从 double 到 String。

示例#2:静态变量用于引用所有对象的公共属性(每个对象不是唯一的)。比如学院名、学生名等等……在加载类的那一刻,在类区只占用一次内存。使用静态变量的好处是使程序内存高效,是一种节省内存的方式。

 class Person{  
       int id; 
       String studentName;  
       // a static variable 
       static String collegeName ="MBA";  

       Person(int i,String n)
       {  
        id = i;  
        studentName = n;  
       }  

        void output ()
        {
         System.out.println(id +" "+ studentName +" "+ collegeName);
        }  

     public static void main(String args[])
       {  
        Person student1 = new Person(100,"Joe");  
        Person student2 = new Person(200,"Jef");  

        student1.output();
        student2.output(); 
       }  
    }  

Display:
     100 Joe MBA
     200 Jef MBA

请注意,为类的所有实例化对象分配了相同的静态值 MBA。

于 2015-06-30T02:06:27.800 回答