偶是初学者,写一程序,其实要实现的东西很简单,要求输入三个一位整数a,b,c,程序验证c是不是a、b的差,但是下边的程序怎么也实现不了,跟一本书上的是一样的?怎么回事?请高手指点import java.io.*;public class ConsoleDemo 
{
public static void main(String[] args) 
{
System.out.println("Please type an integer(a digit):");
int r = -1;
int a = 0,b = 0,c = 0;

try
{
a = System.in.read();
}
catch(IOException e)
{
System.err.println("Exception!");
} try
{
r = System.in.read();
}
catch(IOException e)
{
System.err.println("Exception!");
}
if(r==13)
System.out.println("Please type another integer(a digit):"); try
{
b = System.in.read();
}
catch(IOException e)
{
System.err.println("Exception!");
} try
{
r = System.in.read();
}
catch(IOException e)
{
System.err.println("Exception!");
}
if(r==13)
System.out.println("The substraction of the two integers is(a digit):"); try
{
c = System.in.read();
}
catch(IOException e)
{
System.err.println("Exception!");
} try
{
r = System.in.read();
}
catch(IOException e)
{
System.err.println("Exception!");
}
if(r==13)
{
int i = a-b;
c = c-48;
if(c==i)
System.out.println("Very good");
else
System.out.println("Sorry ,Try again!");
} try
{
r = System.in.read();
}
catch(IOException e)
{
System.err.println("Exception!");
}
}
}

解决方案 »

  1.   

    我也是刚学JAVA这是人写的:也许代码很臭public class TestSub { public static void main(String[] args) 
    {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        String sA = null;
        String sB = null;
        String sC = null;
        try
    {
                 sA = br.readLine();
                 sB = br.readLine();
                 sC = br.readLine();
        
    }
        catch(IOException ex)
        {
         ex.printStackTrace();
        }
        
        if (Integer.parseInt(sA)- Integer.parseInt(sB) == Integer.parseInt(sC))
        {
         System.out.println("TRUE");
        }
        else
        {
         System.out.println("False"+String.valueOf(Integer.parseInt(sA)- Integer.parseInt(sB)));
        }
             
    }
        
    }
      

  2.   

    System.in.read();是读一个字节,而在windows下,一个回车是由两个字节组成,一个byte值为13,一个byte值为10,所以你在判断是否为回车时,少读了一次,应该每个都多加一个System.in.read();
      

  3.   

    import java.io.*;public class ConsoleDemo {
    public static void main(String[] args) {
    System.out.println("Please type an integer(a digit):");
    int r = -1,m=-1;
    int a = 0, b = 0, c = 0; try {
    a = System.in.read();
    } catch (IOException e) {
    System.err.println("Exception!");
    } try {
    r = System.in.read();
    System.in.read();
    } catch (IOException e) {
    System.err.println("Exception!");
    }
    if (r == 13)
    System.out.println("Please type another integer(a digit):"); try {
    b = System.in.read();
    } catch (IOException e) {
    System.err.println("Exception!");
    } try {
    m = System.in.read();
    System.in.read();
    } catch (IOException e) {
    System.err.println("Exception!");
    }
    if (m == 13)
    System.out.println("The substraction of the two integers is(a digit):"); try {
    c = System.in.read();
    } catch (IOException e) {
    System.err.println("Exception!");
    } try {
    r = System.in.read();
    System.in.read();
    } catch (IOException e) {
    System.err.println("Exception!");
    }
    if (r == 13) {
    int i = a - b;
    c = c - 48;
    if (c == i)
    System.out.println("Very good");
    else
    System.out.println("Sorry ,Try again!");
    } try {
    r = System.in.read();
    } catch (IOException e) {
    System.err.println("Exception!");
    }
    }
    }
      

  4.   

    以上是正确代码,但是你写的代码编译的类在linux环境下应该是运行成功的。
      

  5.   

    应该这样使用BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    将读入的字节流转换成字符流。