Topic: 请教 (char c 没初始化)

  Print this page

1.请教 (char c 没初始化) Copy to clipboard
Posted by: 雪原
Posted on: 2004-10-13 20:49

import java.io.*;
public class xiao
{ public static void main(String args[])
{char c;
System.out.println(" 请输入");
try{
c=(char)System.in.read();
} catch(IOException e){}
System.out.println(" 输入"+c);}
}
运行时 系统提示为 c 好象没初始化
为什么啊 请问如何改进啊


{ 请尽量用准确的文字描述作为标题 }

2.Re:请教 (char c 没初始化) [Re: 雪原] Copy to clipboard
Posted by: why
Posted on: 2004-10-13 21:02


import java.io.*;

public class Xiao {
public static void main(String args[]) {
char c; // c 没初始化
System.out.println(" 请输入");

try {
c=(char)System.in.read();
} catch(IOException e) { // if there's an exception, c 没初始化 }

System.out.println(" 输入"+c);
}
}

3.Re:请教 (char c 没初始化) [Re: 雪原] Copy to clipboard
Posted by: qingbo777
Posted on: 2004-10-13 21:09

版主还真是耐心啊Smile

4.Re:请教 [Re: 雪原] Copy to clipboard
Posted by: archonLing
Posted on: 2004-10-13 21:12

You need to initialize the "char c" for

System.out.println(" 输入"+c);}

to work. ex: char c = ' ';

5.Re:请教 (char c 没初始化) [Re: qingbo777] Copy to clipboard
Posted by: helloworld
Posted on: 2004-10-13 21:38

or :

try{

char c = (char) ......

6.Re:请教 (char c 没初始化) [Re: 雪原] Copy to clipboard
Posted by: zxhhero
Posted on: 2004-10-13 22:33

楼上的大哥能说一下那样改的理由吗?

7.Re:请教 (char c 没初始化) [Re: helloworld] Copy to clipboard
Posted by: archonLing
Posted on: 2004-10-13 23:09

helloworld wrote:
or :

try{

char c = (char) ......


This will not work. Variable declared inside the try block may not be visible to the Exception clause. char c has to be initialized outside.

8.Re:请教 (char c 没初始化) [Re: 雪原] Copy to clipboard
Posted by: lvqiang
Posted on: 2004-10-14 00:33

在try{}catch(){}外定义的c在其中是无用的。
只要改成这样就好了!
import java.io.*;
public class xiao{
public static void main(String args[]){
  
System.out.println(" 请输入");
try{
char c =(char)System.in.read();
System.out.println(" 输入"+c);
} catch(IOException e){}

}
}

9.Re:请教 (char c 没初始化) [Re: 雪原] Copy to clipboard
Posted by: HW升
Posted on: 2004-10-14 13:32

楼上的仁兄是对的!

10.Re:请教 (char c 没初始化) [Re: 雪原] Copy to clipboard
Posted by: 雪原
Posted on: 2004-10-15 18:53

谢谢 楼住 和各位 没办法 刚开始学啊 呵呵


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923