当然不对了read()方法读出来的不是值,而是读到的字节长度。
得进行转换一下

解决方案 »

  1.   

    用InputStream的readLine()方法读取输入
      

  2.   

    import javax.swing.*;
    public class GetTwoNumbers
    {
    public static void main(String[] args) 
    {
    int a=0;int b=0;int c=0;
    String xyy = JOptionPane.showInputDialog("输入第一个数");
      a = Integer.parseInt(xyy);  xyy = JOptionPane.showInputDialog("输入第二个数");
       b = Integer.parseInt(xyy);

    System.out.println(a);
    System.out.println(b);
    c=a*b;
    System.out.println("a*b="+c);
    }
    }
      

  3.   

    TRY:import java.io.*;
    public class GetTwoNumbers
    {
            public static void main(String[] args)
            {
                    int a=0;int b=0;int c=0;                System.out.print("Please input two numbers: ");
                    try{
                            a=System.in.read();
                            b=System.in.read();
                    }
                    catch(IOException e){}                a = encoder(a);
                    b = encoder(b);                System.out.println(a);
                    System.out.println(b);
                    c=a*b;
                    System.out.println("a*b="+c);
            }        private static int encoder(int code){
              int result = 0 ;
              char ch[] = {(char)code};          if (Character.isDigit(ch[0])) {
                result = Integer.parseInt(new String(ch));
              }else {
                result = 0;
              }
              return result;
            }
    }
      

  4.   

    当然不对了read()方法读出来的不是值,而是读到的字节长度。
    得进行转换一下回复:好象读出来的不是字节长度,是字符的ACSII码值。
      

  5.   

    import java.io.*;
    public class GetTwoNumbers
    {
    public static void main(String[] args)
    {
    int a=0;int b=0;int c=0;
    System.out.print("Please input two numbers: ");
    try{
    a=System.in.read();
    b=System.in.read();
    a-=48;                   //改的部分
    b-=48;                   //
    }
    catch(IOException e){}
    System.out.println(a);
    System.out.println(b);
    c=a*b;
    System.out.println("a*b="+c);
    }
    }
    读出的是ASCII的值0=48,1=49,......9=57.减一下就行了,上面已经通过;
      

  6.   

    private static int encoder(int code){
              int result = 0 ;
              char ch[] = {(char)code};          if (Character.isDigit(ch[0])) {
                result = Integer.parseInt(new String(ch));
              }else {
                result = 0;
              }
              return result;
            }这个函数就是作转换工作的,并且兼容读入的为任意字符形式。
      

  7.   

    import java.io.*;
    public class GetTwoNumbers
    {
    public static void main(String[] args)
    {
    int a=0;int b=0;int c=0;
    System.out.print("Please input two numbers: ");
    try{
    a=System.in.read();
    b=System.in.read();
    a-=48;                   //改的部分
    b-=48;                   //
    }
    catch(IOException e){}
    System.out.println(a);
    System.out.println(b);
    c=a*b;
    System.out.println("a*b="+c);
    }
    }
    读出的是ASCII的值0=48,1=49,......9=57.减一下就行了,上面已经通过;
    回复:好象你的那个程序还有问题:我只能输入0-9之间的数,如果想输入>9的数好象就不行了,而且2个数字之间不能有空格,这样程序执行时可读性差!!!!
      

  8.   

    JAVA中的read和C++中的不一样,要用以下的方法读入数据。
    很奇怪没有看到书上提到过,兄弟琢磨了很久才发现。import java.io.*;
    public class multiply{
      public static void main(String[] args) throws IOException {
         BufferedReader in =
            new BufferedReader(new InputStreamReader(System.in));    System.out.print("please enter the a: ");
        int a = Integer.parseInt(in.readLine());
        System.out.print("please enter the b: ");
        int b= Integer.parseInt(in.readLine());
        int c = a * b;
        System.out.println("a * b = " + c);
      }
    }//时间关系, 程序未调试
      

  9.   

    import java.io.*;
    public class multiply{
      public static void main(String[] args) throws IOException {
         BufferedReader in =
            new BufferedReader(new InputStreamReader(System.in));    System.out.print("please enter the a: ");
        int a = Integer.parseInt(in.readLine());
        System.out.print("please enter the b: ");
        int b= Integer.parseInt(in.readLine());
        int c = a * b;
        System.out.println("a * b = " + c);
      }
    }
    //调试楼上通过
      

  10.   

    to  imagex(境): 谢谢
      

  11.   

    楼上的方法的确不错,但是如果我想输入的数没有类型的规定,也就是说,可以输入任何类型的(包括int,float,double等),又该怎么办呢?是不是可以将
    Integer.parseInt(in.readLine());
    修改为Double.parseDouble(in.readLine())就可以了呢?
    然后int,float都看作double来处理,在需要的时候再将它们转化过来
    我想这么是不是太复杂了?
      

  12.   

    那用try{}catch(){}吧,捕捉造型转换错误。
      

  13.   

    import java.io.*;
    public class multiply{
      public static void main(String[] args) throws IOException {
         BufferedReader in =
            new BufferedReader(new InputStreamReader(System.in));    System.out.print("please enter the a: ");
        int a = Integer.parseInt(in.readLine());
        System.out.print("please enter the b: ");
        int b= Integer.parseInt(in.readLine());
        int c = a * b;
        System.out.println("a * b = " + c);
      }
    }
    这个对