(var)无法解析为某个类型

时间:2012-09-29 02:32:15

标签: java variables char

我一直在使用eclipse,每当我尝试更改存储在字符数组中的值时,它都会抛出错误。 代码:

    import java.util.Scanner;

public class test {
    public static void main(String args[]) {
        char monstername[] = {'e', 'v', 'i', 'l'};
        String monster = new String(monstername);
        System.out.println("Hello!");
        System.out.println("You are attacked by a " + monster);
        monstername[] = {'t', 'v', 'i', 'l'};
        System.out.println("You are attacked by a " + monster);
    }
}

我尝试更新库但是没有用。

5 个答案:

答案 0 :(得分:3)

monstername[] = {'t', 'v', 'i', 'l'};

不会有两个原因。

  1. 这是无效的语法,因此编译器不知道如何处理它。
  2. 您需要创建变量的新实例
  3. monstername = new char[] {'t', 'v', 'i', 'l'};
    

    因为monster已经被声明为char数组(char[]),所以你不需要在第二个语句中使用[]

答案 1 :(得分:2)

这一行

monstername[] = {'t', 'v', 'i', 'l'};

是有效(部分)声明,但它不是有效的作业。它应该是

monstername = new char[]{'t', 'v', 'i', 'l'};
monster = new String(monstername);

答案 2 :(得分:1)

[]不属于,并且要将数组创建为表达式,请使用new <type>[]

monstername = new char[] {'t', 'v', 'i', 'l'};

答案 3 :(得分:0)

你无法像那样更新它,因为monstername[]是一个指针。

尝试:monstername = new char[] {'s', 'm', 't', 'g'};

答案 4 :(得分:0)

更改行

monstername[] = {'t', 'v', 'i', 'l'};
System.out.println("You are attacked by a " + monster);

monstername = new char[]{'t', 'v', 'i', 'l'};
System.out.println("You are attacked by a " + new String(monstername));
相关问题