菜鸟小问题

skyboy0720 2003-10-15 08:23:44
import java.io.*;
public class IOTest
{
public static void main ( String [ ] args ) throws IOException
{
int b;
System.out.print ( "Input a integer:" );
int a = Integer.parseInt ( System.in.read ( ) );
System.out.println ( a + b );
}
}

这会出错,我就想从键盘获得一个整数,进行运算,很简单,怎么改,谢谢了

...全文
33 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyboy0720 2003-10-16
  • 打赏
  • 举报
回复
谢谢帮助,学到了东西!
kzjiang820218 2003-10-15
  • 打赏
  • 举报
回复
非常谢谢 DainelLee(现在开始学java)
我又学到了东西!
seaman0916 2003-10-15
  • 打赏
  • 举报
回复
不好意思,上面写错了!

import java.io.*;
public class IOTest
{

public static String keyin()
{
String s=" ";
try
{
BufferedReader one=new BufferedReader(new InputStreamReader(System.in));
s=one.readLine();
}
catch(IOException e) {System.out.println("There was wrong !");}

return s;
}
public static void main ( String [ ] args ) throws IOException
{
int a=0,b=5; //要初始化的!
System.out.print ( "Input a integer:" );
try
{
a= Integer.parseInt(keyin( )); //这个可能会有异常抛出
}
catch(NumberFormationException e)
{
System.out.println("请确信输入的是数字!");
}
System.out.println ( a + b );
}
}


这下试试吧!刚才实在是不好意思! 忘记了!
kzjiang820218 2003-10-15
  • 打赏
  • 举报
回复
看了一下,但是不能帮你解决!
seaman0916 2003-10-15
  • 打赏
  • 举报
回复
import java.io.*;
public class IOTest
{
public static void main ( String [ ] args ) throws IOException
{
int a=0,b=5; //要初始化的!
System.out.print ( "Input a integer:" );
try
{
a= Integer.parseInt( System.in.read( )); //这个可能会有异常抛出
}
catch(NumberFormationException e)
{
System.out.println("请确信输入的是数字!");
}
System.out.println ( a + b );
}
}

试试吧! 应该可以了
LuZhou 2003-10-15
  • 打赏
  • 举报
回复
import java.io.*;
public class IOTest
{
public static void main ( String [ ] args )
{
int b;
System.out.print ( "Input a integer:" );
int a = Integer.parseInt ( System.in.read ( ) );
System.out.println ( a + b );
}
}
jianlin 2003-10-15
  • 打赏
  • 举报
回复
System.in.read()的返回不是String型的
DainelLee 2003-10-15
  • 打赏
  • 举报
回复
import java.io.*;
public class IOTest
{
public static void main (String[] args )
{
int b = 4;
System.out.print ("Input a integer:");
int a;
char[] c = new char[1];
try {
a = System.in.read();
if ((a > 57) || (a< 48))
System.out.println("输入错误,请输入数字");
c[0] = (char)a;
System.out.println (" " + (Integer.parseInt(new String(c)) + b));
} catch (IOException e){
System.out.println("Error of IO");
}
}
}

我也才开始学java,不知哪位高手有更好的方法?
希偌 2003-10-15
  • 打赏
  • 举报
回复
b???

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧