java - 通过cmd行编译时出错

标签 java compiler-errors switch-statement final

对于入门级Java开发人员面试,他们要求我对此代码发表评论,并说明程序将打印eb的内容(不重写,因此我认为那不是我应该做的?)该工作始于一堆付费Java训练。我不相信他们期望我成为天才。我三天前才开始学习java。我已经在代码中添加了注释,但是当我尝试使用jdk 1.8 javac Foo.java进行编译时会出现一堆错误。

import java.util.ArrayList; //allows use of the class ArrayList within program
public class Foo { //declares new class

    public String FOONAME = "foobar";  //declares new string variable giving a value of foobar

    private final int fooAge = 23; //declares a private int giving it a final value of 23 

    private static final int fooWeight = 150; //declares a private int giving it a final value of 150

    private double fooHeight; //declares a private double variable, 

    private Foo() {}        // declares new constructor setting default initial values          

    public Foo(int age, int weight) {       //declares constructor and defines initial values
        fooAge = age; // initial value of age will be 23
        fooWeight = weight; // initial value of weight will be 150  
    }

    public void main(String args) { //call to main method, every program needs one, no cmd args, and no return necessary        

        Foo foo = new Foo();    //sets a value for the Foo class            

        System.out.println(Foo.FOONAME); //prints foobar in the cmd window

        int a = foo.getAge(); // declares int variable a giving it a value of foo.getAge() which = fooAge = 23      
        int w = foo.getWeight(); // declares int variable w giving it a value of foo.getWeight() which = fooWeight = 150            

        int young = 20; // delcares int variable called young, giving it a value of 20

        switch(a) { // begins the switch statement, relies on a being a constant value
        case young:         // when a = young (never because a is a constant and already set to 23)                 
            System.out.println("Young"); //print young on a new line in the cmd window is the case statement is True
            break;  // seperates the case statements of the switch statement
        case 30: // when a = 30 (Again this won't happen because a is set at a constant that = fooAge = 23)
            System.out.println("MiddleAged");   // print MiddleAged on a new line in the cmd window if the case statement is True
        default: // defines the result if a is anything other than the two cases defined above.
            System.out.println("Old"); // Old will be printed on a new line in the cmd window if the a meets the default requirements
        } // closes switch statement

        if (a > 70) //begins an if statement, if a is greater than 70 (which it won't be because a is again 23 the final fooAge
            System.out.println("Really Old");   // Really Old would print if a were over 70


        List<String> names = new ArrayList<>();     //creates a new resizable arrayList class
        names.add("Bob"); //adds a string value to the arraylist 
        names.add("George");//adds a string value to the arraylist 
        names.add("Linda");//adds a string value to the arraylist 
        names.add(young);   // adds the young variable to the array, but its not a string so that can't be right?                       



    } //closes the main method

    private int getAge() { //decalres the getage variable 
        return fooAge; //gives it the fooAge value that is final and previously set to 23
    } // closes getAge Variable declaration 

    public static int getWeight() { // declares getWeight variable 
        return fooWeight;  //gives it the fooWeight variable that is final and previosuly set to 150
    } // closes the getWeight Declration 

    public void setHeight(final double height) { //decalres setHeight variable,  
        fooHeight = height; // gives height a value of fooheight that hasn't been defined yet
        height = null;      // gives height a value of null, which may or may not be allowed for the double data type                       
    } // closes the setHeight variable

}    // closes the the class Foo 

/*
 if compiled it will print foobar on one line and Old on another 
 */

我的错误
C:\temp\MyWork> javac Foo.java                                                

  Foo.java:15: error: cannot assign a value to final variable fooAge                      

        fooAge = age; // initial value of age will be 23                             
   ^                                                           

   Foo.java:16: error: cannot assign a value to final variable fooWeight                      

     fooWeight = weight;     // initial value of weight will be 150                                                                                                  ^                                                            

   Foo.java:23: error: non-static variable FOONAME cannot be referenced from a static context      

             System.out.println(Foo.FOONAME); //prints foobar in the cmd window                                                                                                                    ^                                        

 Foo.java:31: error: constant expression required                               

                 case young:                     // when a = young (never because a is a constant and already set to 23)                                                              ^                                                     

     Foo.java:44: error: cannot find symbol                                 

                         List<String> names = new ArrayList<>();         //creates a new resizable arrayList class                                                                       ^                                                       

          symbol:   class List                                                  

          location: class Foo                                                   

        Foo.java:64: error: incompatible types: <null> cannot be converted to double        

            height = null;         // gives height a value of null, which may or may not be allowed for the double data type                                                                                                                                        ^                                                      6 errors                                                                        

任何帮助将不胜感激,我正在尝试展示我解决问题和访问可用资源(包括诸如堆栈交换之类的在线资源)的能力。这将是改变职位的方式,所以我将尽我所能!

谢谢

最佳答案

看一下Foo的构造函数

 public Foo(int age, int weight) {     
    fooAge = age; 
    fooWeight = weight; 
 }

但是这些fooAgefooWeightfinal
private final int fooAge = 23;
private static final int fooWeight = 150;

您不能更改final变量。 final它本身具有final变量的含义。

解决方案:您可以使用非final变量。

下期
 System.out.println(Foo.FOONAME);  //FOONAME non static you can't call by class
                                   //name non static variables 

解决方案:将FOONAME设为静态。

下期。
 switch(a) { 
        case young:  // cases must be constant.

在这里young不是常量。

解决方案:将young设置为final。
 final int young = 20;

下期。
List<String> names = new ArrayList<>(); // List not imported 
names.add(young); // adding int to String array?

解决方案:使用适当的进口。
import java.util.List;


names.add(String.valueOf(young));
下期。
public void setHeight(final double height) {
 fooHeight = height; 
 height = null; // you can't use null for primitive double
 // and also height is final can't change it.

解决方案:您不能将新值分配给height

关于java - 通过cmd行编译时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26210295/

相关文章:

Java - 为什么等待多个信号量不起作用?

ffmpeg - 拉取功能错误;打开 URL 时, '...' 的 keepalive 请求失败,使用新连接重试

vba - 如何在 VBA Excel 2016 中查看编译后的子例程的大小?

c# - C# switch 语句是否需要中断;

Java:切换默认覆盖 try-catch

c - "expression must have integral type"C中的开关问题

java - 多线程 Swing 事件调度程序线程

java - 如何覆盖java类中的单个方法

java - 如何为由 block 组成的塔创建布局

c++ - 无法在C++中包含头文件