class test
{
int[] str;
str=new int[2];
str[0]=25;
str[1]=50;
public static void main(String[] args)
{
}}
=====================
出错:
test.java:4: 需要 <标识符>
str=new int[2];
^
test.java:5: 需要 ']'
str[0]=25;
^
test.java:5: 需要 <标识符>
str[0]=25;
^
test.java:6: 需要 ']'
str[1]=50;
^
test.java:6: 需要 <标识符>
str[1]=50;
^
5 错误=========
也不知道为什么我都没有分给?CSDN没给我分啊。
{
int[] str;
str=new int[2];
str[0]=25;
str[1]=50;
public static void main(String[] args)
{
}}
=====================
出错:
test.java:4: 需要 <标识符>
str=new int[2];
^
test.java:5: 需要 ']'
str[0]=25;
^
test.java:5: 需要 <标识符>
str[0]=25;
^
test.java:6: 需要 ']'
str[1]=50;
^
test.java:6: 需要 <标识符>
str[1]=50;
^
5 错误=========
也不知道为什么我都没有分给?CSDN没给我分啊。
{
str=new int[2];
str[0]=25;
str[1]=50;
}
str[0]=25;
str[1]=50;
三句要写在方法里面要是写在main里面要加staticps:每天登录就有分了
int[] str;
str=new int[2];
str[0]=25;
str[1]=50;
关键是 你把声明和初始化的操作分开了,造成的结果是初始化的代码
str=new int[2];
str[0]=25;
str[1]=50;
没有作用域
你可以声明成静态的
static int[] str;
static {
str=new int[2];
str[0]=25;
str[1]=50;}
或者放在构造函数里int[] str;
public test() {
str=new int[2];
str[0]=25;
str[1]=50;}
str=new int[2];
str[0]=25;
str[1]=50;
上面3句只是执行初始化,不一定要放在方法里的
public test() {
str=new int[2];
str[0]=25;
str[1]=50;}
str[1]=50; 要放在方法里
public class Test {
int[] str=new int[]{25,50};
public static void main(String[] args){
}
}
如果要在main方法里使用需将str定义成static