Java初学者-需要Java代码错误建议

时间:2018-09-16 02:50:15

标签: java

首先,我是Java的入门者。我终于要上大学的核心课程了。我在《计算机科学1》上,我正在将从书中获得的代码作为一种练习进行更正,以便对如何修复代码有更好的了解。这是一个简单的代码,但是,我每次都会遇到3个错误。我需要有关如何纠正它们的建议!我对所有这些都不熟悉,因此有时可能会令人沮丧。谢谢!

public class Mistakes
{
    public static void main(String[] args);
    {
    int y;
    int x = 12;
    y + x = y;
    System.out.println("This is what y + x equals: "+y);
    System.out.print("This is what t equals: "+t);
    }
}

我一直遇到3个错误:

java:3: error: missing method body, or declare abstract
public void main(String[] args);
            ^


java:7: error: unexpected type
y + x = y;                     
  ^
required: variable
found:    value

java:9: error: cannot find symbol
System.out.print("This is what t equals: "+t);
                                           ^ 
symbol:   variable t
location: class Mistakes

我可以将t更改为x吗? 我可以将public class更改为public abstract吗? 任何建议将不胜感激!

1 个答案:

答案 0 :(得分:2)

首先,您的const initialState = { flashType: "", }; export default function(state = initialState, action){ switch(action.type){ case USER_REGISTER: return { ...state, flashType: "registrationComplete", }; default: return state; } } 方法在声明后有一个main()。仅当您声明的方法为abstract并且没有“ body”时,才允许这样做。在这种情况下,应删除分号。看下面:

;

第二,您的分配操作错误。在Java中(以及一般而言,在编程中),您必须首先指定一个变量,然后指定它将要接收的值(从字面上或通过表达式来指定)。您应该按如下所示进行操作:

public static void main(String[] args) {
    //Your code here
} 

在这种情况下,如果您愿意,甚至可以使用快捷方式:

y = x + y; //the value of y will be equal to x+y

最后,由于未声明变量y += x; //this expression will have the same effect as the shown above ,因此您遇到了最后一个错误,因此方法t试图打印不存在的变量。在这种情况下,您应该删除符号System.out.print()或使用此名称声明一个变量,就像我在下面所做的那样:

t
相关问题