Hello World的程序就不记了,太EASY了。
试着写了个A + B problem的程序,就遇到了个问题。怎么读整数呢?System.in.read不行,难道要一个一个读字符再自己做转换?不会吧,那也太麻烦了。再 想想别的办法,翻了翻文档,挑了一个java.io.DataInputStream,用方法readInt试了一下,还是不行:
readInt
public final int readInt() throws IOException
b1, b2,b3 和 b4, 满足 0 <= b1, b2,b3,b4 <= 255, 那么结果等于:
(b1 << 24) | (b2 << 16) + (b3 <<
+b4 该方法将一直阻塞,直到此四个字节数据被读入, 或检测到了数据流尾或抛出异常。
int。 大 名鼎鼎的加瓦不会连读个数都这么不方便吧?继续找文档,在util里找到了一个叫Scanner的类,用它的nextInt方法,hoho,这会就对了 嘛。
public int nextInt()
radix is the default radix of this scanner.
InputMismatchException – if the next token does not match the Integer regular expression, or is out of range NoSuchElementException – if input is exhausted IllegalStateException – if this scanner is closed下面是完整的程序:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) {
int a, b;
try {
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
System.out.println(a + b);
}
catch (Exception e) {}
}
}
嘿嘿,我的第一个加瓦程序,志之^_^
至于加瓦其它的那么多特性, 以后慢慢体验吧!