Topic: 各位大侠 :异常处理时,变量有什么特殊要求嘛 ?小弟在线等答案,谢了!

  Print this page

1.各位大侠 :异常处理时,变量有什么特殊要求嘛 ?小弟在线等答案,谢了! Copy to clipboard
Posted by: babyone
Posted on: 2004-09-10 20:11

//==============================================
// 程序名称:Zero.java
// 程序目的:判断一个浮点数是否等于0
//==============================================
import java.io.*;

public class Zero1{
  public static void main(String args[]){
    try
    {
      float x=Float.parseFloat(args[0]);
    }
    catch(NumberFormatException ne)
    {
      
      System.err.println("您必须输入一个浮点数");
    }
    catch(RuntimeException re)
    {
      System.err.println("RuntimeException:"+re);
    }
    finally
    {
    if(x==0f)
      System.out.println("浮点数x="+x+"等于0");
    else
      System.out.println("浮点数x="+x+"不等于0");
   }
  }
}
  
出现以下错误,请教时什么原因 ! 
  ---------------- JDK Debug Build ------------------
Compiling C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero1.java
Command line: "C:\j2sdk1.4.1\bin\javac.exe" -deprecation -g -classpath C:\DOCUME~1\user\MYDOCU~1\MODELW~1\MYPROJ~1 "C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero1.java"
The current directory is: C:\Documents and Settings\user\My Documents\Modelworks\MyProjects
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero1.java:25: cannot resolve symbol
symbol : variable x
location: class Zero1
    if(x==0f)
^
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero1.java:26: cannot resolve symbol
symbol : variable x
location: class Zero1
      System.out.println("浮点数x="+x+"等于0");
^
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero1.java:28: cannot resolve symbol
symbol : variable x
location: class Zero1
      System.out.println("浮点数x="+x+"不等于0");
^
3 errors
Finished
  怎么显示都是变量x的原因啊 ? 苦苦思索,没有答案 ,请教了 !

2.Re:各位大侠 :异常处理时,变量有什么特殊要求嘛 ?小弟在线等答案,谢了! [Re: babyone] Copy to clipboard
Posted by: why
Posted on: 2004-09-10 21:29

Please use your original post for this follow-up question. Thanks.

The declaration of x is in this try block, and so will be its scope

try
{
float x=Float.parseFloat(args[0]);
}
....

should be

float x;
try
{
x=Float.parseFloat(args[0]);
}

3.Re:各位大侠 :异常处理时,变量有什么特殊要求嘛 ?小弟在线等答案,谢了! [Re: babyone] Copy to clipboard
Posted by: babyone
Posted on: 2004-09-10 22:42

谢谢 ,在您的帮助下,终于克服了难关了 !


   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