一个JAVA小问题,马上结帖给分

oldfisher 2002-07-29 08:54:43
是一个两个数相加的小程序,可每次一输入A后,它就输出结果和为多少了
为什么?我还没输入B呢???
import java.util.*;
import java.io.*;
import java.lang.*;
public class add {

public static int dd(int a,int b){
return a+b;
}
public static void main(String[] args){
int a=0,b=0;
try {
System.out.print("请输入a:");
a = System.in.read();
System.out.print("请输入b:");
b = System.in.read();
System.out.println("和为:"+dd(a,b));
}catch(IOException e) {
System.out.println("wrong");
};



}
}
...全文
57 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
oldfisher 2002-07-30
  • 打赏
  • 举报
回复
结贴给分!
oldfisher 2002-07-30
  • 打赏
  • 举报
回复
感谢大家的帮忙,问题总算解决了。
谢谢各位!
代码整理如下:
import java.io.*;
import java.io.IOException;
public class add
{
public static void main(String[] args)
{
System.out.println("Hello World!");
try
{
int nFirst = 0;
int nSecond = 0;
String sFirst,sSecond;
BufferedReader in = new BufferedReader( new InputStreamReader(System.in) );
System.out.print("Please input the first number:");
sFirst = in.readLine();

try{
nFirst = Integer.parseInt( sFirst );
}
catch(NumberFormatException nfe )
{
System.out.println错误的参数:"+sFirst);
System.exit(-1);
}

System.out.print("Please input the second number:");
sSecond = in.readLine();
try{
nSecond = Integer.parseInt( sSecond );
}
catch(NumberFormatException nfe )
{
System.out.println错误的参数:"+sSecond);
System.exit(-1);
}
int nTotal = nFirst + nSecond;
System.out.println("The added value is:"+ nTotal);
}catch(IOException ie)
{
System.out.println( ie );
}
}
}
云智软件 2002-07-29
  • 打赏
  • 举报
回复
要用 bufferdReader
oldfisher 2002-07-29
  • 打赏
  • 举报
回复
俺再给帖子加50分!
oldfisher 2002-07-29
  • 打赏
  • 举报
回复
各位高人请指点一下吧
oldfisher 2002-07-29
  • 打赏
  • 举报
回复
try {
System.out.print("请输入a:");
byte[] aa = new byte[300];
a = System.in.read(aa);
//a = System.in.read();
System.out.print("请输入b:");
byte[] bb = new byte[300];
b = System.in.read(bb);
//b = System.in.read();
System.out.println("a="+a+" ,b="+b);
System.out.println("和为:"+dd(a,b));
}catch(IOException e) {
System.out.println("wrong");
};

是不是要这样写?
可运行不成功

请输入a:4
请输入b:6
a=2 ,b=2
和为:4
为什么?
woowindice 2002-07-29
  • 打赏
  • 举报
回复
byte[] aa = new byte[2000];
int num = System.in.read(aa);
num是读到的byte总数。只要你一行输入不超过2000个byte就能保存在aa里面
你可以再定义一个bb = new byte[num];就可以了。
xqiqi 2002-07-29
  • 打赏
  • 举报
回复
import java.io.DataInputStream;
qianlicao 2002-07-29
  • 打赏
  • 举报
回复
System.in.read()读入的是key 的编码
在你输入一个数后,按 enter key 时,己输入了b,b=enter 的编码
你在d=System.in.read(); 后 insert System.out.println(*a=*+a+*b=*);
观察结果
cat_hangzhou 2002-07-29
  • 打赏
  • 举报
回复
?查了一把api,没有readln的啊:(
那就只有自己判断了
用read(),一次读一个,然后凑起来
oldfisher 2002-07-29
  • 打赏
  • 举报
回复
回车被认为是B了。
DataInputStream dis=new DateInputStream(System.in);
不行啊
我编译了一下会出现错误
cannot resolve symbol
symbol : class DateInputStream

new DateInputStream(System.in);这样的写法有错吧

cat_hangzhou 2002-07-29
  • 打赏
  • 举报
回复
read和readln不同阿
read(byte[] b)
改成readln吧
yuanqigao 2002-07-29
  • 打赏
  • 举报
回复
b字符已经认为是回车了。

哈哈哈哈!
zmrljl 2002-07-29
  • 打赏
  • 举报
回复
DataInputStream dis=new DateInputStream(System.in);
a=dis.readline;
....
b=dis.readline;
....
需要缓冲。。。。
cctv5 2002-07-29
  • 打赏
  • 举报
回复
bufferdReader
是什么?怎么用?
yefaqi 2002-07-29
  • 打赏
  • 举报
回复
public static void main(String[] args)
{
try
{
int nFirst = 0;
int nSecond = 0;
String sFirst,sSecond;
BufferedReader in = new BufferedReader( new InputStreamReader(System.in) );
System.out.print("Please input the first number:");
sFirst = in.readLine();

try
{
nFirst = Integer.parseInt( sFirst );
}
catch(NumberFormatException nfe )
{
System.out.println("错误的参数:"+sFirst);
System.exit(-1);
}

System.out.print("Please input the second number:");
sSecond = in.readLine();
try
{
nSecond = Integer.parseInt( sSecond );
}
catch(NumberFormatException nfe )
{
System.out.println("错误的参数:"+sSecond);
System.exit(-1);
}
int nTotal = nFirst + nSecond;
System.out.println("The added value is:"+ nTotal);
}

catch(IOException ie)
{
System.out.println( ie );
}
}
zzzbx 2002-07-29
  • 打赏
  • 举报
回复
1要用 bufferdReader
2 要将类型转化 string 转化成 int

62,634

社区成员

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

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