怎么赋值?
我用 char=''不行 D:\javawork>javac CharInput.java
 CharInput.java:7: 空字符字面值
                char c='';
                       ^
 CharInput.java:7: 未结束的字符字面值
                char c='';
                        ^
 CharInput.java:8: 需要 ';'
              System.out.println("please input a char!");用char="" 也不行
 D:\javawork>javac CharInput.java
 CharInput.java:7: 不兼容的类型
 找到: java.lang.String
 需要: char
                char c="";
                       ^

解决方案 »

  1.   

    楼主看这个链接
    http://topic.csdn.net/t/20041111/15/3543812.html
      

  2.   

    谢谢你的链接哈~不过有点没看懂,我想用个空格赋值给char 型可以么?
      

  3.   

    恩。是可以的
    2中方法初始化为空格
    char c = '\u0020';
    char c = ' ';
      

  4.   


    import java.io.*;public class CharInput
    {
    public static void main(String []args)
    {
    char c='';
    System.out.println("please input a char!");
    try{
    c=(char)System.in.read();
    }
    catch(IOException e){}

    System.out.println("you entered is "+ c);
    }
    }
    这个是我用char c='';赋值的代码
    提示错误是:
    D:\javawork>javac CharInput.javaD:\javawork>javac CharInput.java
    CharInput.java:7: 空字符字面值
                    char c='';
                           ^
    CharInput.java:7: 未结束的字符字面值
                    char c='';
                            ^
    CharInput.java:8: 需要 ';'
                    System.out.println("please input a char!");
                          ^
    3 错误
      

  5.   


    import java.io.*;public class CharInput
    {
        public static void main(String []args)
        {
            char c=0; //或者用  char c=' ';
            
            System.out.println("please input a char!");
            try{
                c=(char)System.in.read();
                }
            catch(IOException e){}
            
            System.out.println("you entered is "+ c);
    }
      

  6.   

    import java.io.*; public class CharInput 

        public static void main(String []args) 
        { 
            char c=0; //ASCII 0,char_null 
            
            System.out.println("please input a char!"); 
            try{ 
                c=(char)System.in.read(); 
                } 
            catch(IOException e){System.out.println(e.getMessage());}         
            System.out.println("you entered is "+ c); 

      

  7.   

    我明白你的意思了 你是想像用空值初始化int一样初始化一个char c]
    这个问题我也是第一次遇到 我试了一下 用char c = 0; 可行 
    用 char c=' '; 会报错~ 
    变量或对象引用的默认值如下:int : 0
    byte : 0
    long : 0
    shor : 0
    float : 0.0
    double : 0.0
    boolean : false
    char : \u0000
    object reference : nullhttp://book.51cto.com/art/200708/54140.htm-------------
    我开始理解成如何给一个char 赋值一个 空格 了。
      

  8.   

    不过还真奇怪 我这么赋值 会报错 
    char c = \u0000; 
    System.out.println("the char is "+ c);用 char c = 0; 吧
      

  9.   

    这么写就行了~
    char c = '\u0000'; 
    System.out.println("the char is "+ c);
      

  10.   

    char c = ' '; 
      

  11.   

    传说用单引号引起来是字符
    双引号引起来是字符串
    char是字符~
      

  12.   

    只有引用类型的才能赋值为空
    char是基本数据类型
      

  13.   

    char c = '';
    这样写是错误地!单引号里必须有内容!
    char c = '\u0020 
    System.out.println("the char is "+ c); 
      

  14.   

    这样是不对的,实际上并没有给c赋值:
      char c = '';
    应该要给c赋是在的值,像这样:
      char c = 'a';
    即使你不用也要先给他赋一个再说
      

  15.   

    char c = '\u0000';
    或者
    char c = 0;
      

  16.   

    谢谢大家,经验证,char c =''是会报错的,就像我发出的错误提示一样,按照大家给的方案,char c=0 可以,char c='\u0000'是可以的,\u0020也可以,不过我还不知道这两个有什么区别。再简单的直接赋个 任意字符也可以。
    可以参考beguile的回复。yirentianran的回复也很有帮助:只有引用类型的才能赋值为空 char是基本数据类型
      

  17.   

    不好意思,今天上课我们也讲了个类似的列子,我回来实验了一下,用CHAR C=' '可以使用,楼上有正解。特此说明下