注册 登录
编程论坛 J2EE论坛

/为什么输不出正确的int型?求答复!谢谢!

daemon1 发布于 2011-10-12 19:54, 1069 次点击
import *;

public class Hello
{
    public static void main(String [] args)
    {
        DataInputStream d
        = new DataInputStream(new BufferedInputStream(System.in));
        int s;
        try{
            s = d.readInt();
            System.out.println(s);
            int b = d.readInt();
            System.out.println(b);
            int sum = s + b;
            System.out.println(sum);
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}
7 回复
#2
2011-10-13 17:17
你的是读取后转换为data类型了,所以没有正确结果
运行下我的吧
import *;

public class Hello
{
    public static void main(String [] args)
    {
        
        Hello hello = new Hello();
        hello.doRead();
        /*DataInputStream d = new DataInputStream(new BufferedInputStream(System.in));
        int s;
        try{
            s = d.readInt();
            System.out.println(s);
            int b = d.readInt();
            System.out.println(b);
            int sum = s + b;
            System.out.println(sum);
        }catch(IOException e)
        {
            e.printStackTrace();
        }*/
    }
   
    public void doRead(){
        System.out.println("请输入第一个整型数字,回车");
        int numA,numB;
        BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in));
        try {           
            numA = Integer.parseInt(brKey.readLine());
            System.out.println("请再输入一个整型数字,回车");
            numB = Integer.parseInt(brKey.readLine());
            System.out.println("两数之和:"+(numA+numB));
            brKey.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("读取错误,程序已经退出运行");
            System.exit(0);
        }catch(NumberFormatException e){
            System.out.println("请输入整型数字,程序已经退出运行");
            System.exit(0);
        }
    }
}

#3
晓夜圆舞曲2011-10-13 18:45
你那个没有在输出结果时进行类型的转换,在输出结果进行类型转换
#4
daemon12011-10-14 18:16
回复 2楼 付政委
BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in));
这个是可以!
但我想要的是datainputstream对象输入,这样就不用integer了!
#5
daemon12011-10-14 18:16
回复 3楼 晓夜圆舞曲
如何转换?求解!谢谢!
#6
daemon12011-10-17 23:16
无人啊!
#7
husiwen2011-10-19 12:55
你测试下小于 255的数字,看是不是能接受到。。。我记得 readInt好像只取最后8位
#8
daemon12011-10-19 16:04
回复 7楼 husiwen
1,2都不行啊!
1