Topic: 小虾求教:这个程序是不是某个地方出问错了 ?

  Print this page

1.小虾求教:这个程序是不是某个地方出问错了 ? Copy to clipboard
Posted by: babyone
Posted on: 2004-09-10 08:59

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

public class Zero {
public static void main(String[] args)
{
Float x;

System.out.println("请输入一个浮点数x=");
InputStreamReader i=new InputStreamReader(System.in);
BufferedReader b=new BufferedReader( i );


x=Float.parseFloat( b );

if (x==0)
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\Zero.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\Zero.java"
The current directory is: C:\Documents and Settings\user\My Documents\Modelworks\MyProjects
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero.java:21: ')' expected
System.out.println("浮点数x="+x"等于0");
^
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero.java:23: ')' expected
System.out.println("浮点数x="+x"不等于0");
^
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero.java:18: parseFloat(java.lang.String) in java.lang.Float cannot be applied to (java.io.BufferedReader)
x=Float.parseFloat(b);
^
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero.java:20: operator == cannot be applied to java.lang.Float,int
if (x==0)
^
4 errors
Finished
那位能给我在我原程序的基础上最少改动的建议 ! 我在此叩谢了 !

2.Re:小虾求教:这个程序是不是某个地方出问错了 ? [Re: babyone] Copy to clipboard
Posted by: why
Posted on: 2004-09-10 12:28

the error messages are clear enough! Sad

import java.io.*;

public class Zero {
public static void main(String[] args)
{
float x;

System.out.println("请输入一个浮点数x=");
InputStreamReader i=new InputStreamReader(System.in);
BufferedReader b=new BufferedReader( i );

x=Float.parseFloat( b );

if (x==0f)
System.out.println("浮点数x="+x + "等于0");
else
System.out.println("浮点数x="+x + "不等于0");
}
}


There could be other errors.
If you don't understand what the error messages mean, study English;
If you don't understand why they are errors, read more, work harder, or otherwise consider quitting Java.

3.Re:小虾求教:这个程序是不是某个地方出问错了 ? [Re: babyone] Copy to clipboard
Posted by: flywithmoon
Posted on: 2004-09-10 14:34

浮点数0必需加f,才能正确表示。

4.Re:小虾求教:这个程序是不是某个地方出问错了 ? [Re: babyone] Copy to clipboard
Posted by: babyone
Posted on: 2004-09-10 16:17

谢谢上面两位大侠 ,果然去掉了几个错误 ,但是还有个错误没解决掉 !现在变成这样了 !
---------------- JDK Debug Build ------------------
Compiling C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero.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\Zero.java"
The current directory is: C:\Documents and Settings\user\My Documents\Modelworks\MyProjects
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\Zero.java:18: parseFloat(java.lang.String) in java.lang.Float cannot be applied to (java.io.BufferedReader)
  x=Float.parseFloat(b);
^
1 error
Finished

我记得我这个方法没用错啊 ! 怎么会显示错误呢 ? 望几位大侠大侠能继续给我答案,小弟再次叩谢了 !

5.Re:小虾求教:这个程序是不是某个地方出问错了 ? [Re: babyone] Copy to clipboard
Posted by: why
Posted on: 2004-09-10 21:36

babyone wrote:
Zero.java:18: parseFloat(java.lang.String) in java.lang.Float cannot be applied to (java.io.BufferedReader)
  x=Float.parseFloatBeer;
^
1 error
Finished

我记得我这个方法没用错啊 ! 怎么会显示错误呢 ? 望几位大侠大侠能继续给我答案,小弟再次叩谢了 !

It is definitely incorrect,
Float.parseFloat is excepting a String!

BufferedReader b=new BufferedReader( i );
String sInput = b.readLine();
x=Float.parseFloat(sInput);

Actually, there are something else missing, one of which is Exception handling.

6.Re:小虾求教:这个程序是不是某个地方出问错了 ? [Re: babyone] Copy to clipboard
Posted by: babyone
Posted on: 2004-09-10 23:39

谢了 ! 能得到您的帮助,我想,我会很快就学好java的 !


   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