1,输入输出:
1) 从控制台读取输入
Scanner in = new Scanner(System.in);
附录:也可以去看看Scanner的其他构造函数:
构造方法摘要 | |
---|---|
Scanner(File source) 构造一个新的 Scanner ,它生成的值是从指定文件扫描的。 | |
Scanner(File source, String charsetName) 构造一个新的 Scanner ,它生成的值是从指定文件扫描的。 | |
Scanner(InputStream source) 构造一个新的 Scanner ,它生成的值是从指定的输入流扫描的。 | |
Scanner(InputStream source, String charsetName) 构造一个新的 Scanner ,它生成的值是从指定的输入流扫描的。 | |
Scanner(Readable source) 构造一个新的 Scanner ,它生成的值是从指定源扫描的。 | |
Scanner(ReadableByteChannel source) 构造一个新的 Scanner ,它生成的值是从指定信道扫描的。 | |
Scanner(ReadableByteChannel source, String charsetName) 构造一个新的 Scanner ,它生成的值是从指定信道扫描的。 | |
Scanner(String source) 构造一个新的 Scanner ,它生成的值是从指定字符串扫描的。 |
然后用Scanner类的各种操作来实现输入:
String name = in.next();
int age = in.nextInt();
double d = in.netDouble();
等等,,,,,
2)格式化输出
沿用了C的风格,不详述
2,大数值
BigInteger和BigDecimal类实现了任意精度的整数和浮点数运算,大整数运算提供了专门的方法,不能直接用+,*。
看个例子吧:
View Code
1
import
java.math.
*
;
2 import java.util. * ;
3
4
5 // BigInteger类的用法,计算组合数C(n,k),
6 public class BigIntegerTest
7 {
8 public static void main(String[] args)
9 {
10 Scanner in = new Scanner(System.in); // 标准输入
11
12 System.out.print( " How many numbers do you need to draw? " );
13 int k = in.nextInt(); // 读取标准输入
14
15 System.out.print( " What is the highest number you can draw? " );
16 int n = in.nextInt();
17
18 /*
19 compute binomial coefficient
20 n * (n - 1) * (n - 2) * . . . * (n - k + 1)
21 -------------------------------------------
22 1 * 2 * 3 * . . . * k
23 */
24
25 BigInteger lotteryOdds = BigInteger.valueOf( 1 ); // 大数加减乘除的用法
26
27 for ( int i = 1 ; i <= k; i ++ )
28 lotteryOdds = lotteryOdds
29 .multiply(BigInteger.valueOf(n - i + 1 ))
30 .divide(BigInteger.valueOf(i)); // 从n里面取k的概率
31
32 System.out.println( " Your odds are 1 in " + lotteryOdds + " . Good luck! " );
33 }
34 }
2 import java.util. * ;
3
4
5 // BigInteger类的用法,计算组合数C(n,k),
6 public class BigIntegerTest
7 {
8 public static void main(String[] args)
9 {
10 Scanner in = new Scanner(System.in); // 标准输入
11
12 System.out.print( " How many numbers do you need to draw? " );
13 int k = in.nextInt(); // 读取标准输入
14
15 System.out.print( " What is the highest number you can draw? " );
16 int n = in.nextInt();
17
18 /*
19 compute binomial coefficient
20 n * (n - 1) * (n - 2) * . . . * (n - k + 1)
21 -------------------------------------------
22 1 * 2 * 3 * . . . * k
23 */
24
25 BigInteger lotteryOdds = BigInteger.valueOf( 1 ); // 大数加减乘除的用法
26
27 for ( int i = 1 ; i <= k; i ++ )
28 lotteryOdds = lotteryOdds
29 .multiply(BigInteger.valueOf(n - i + 1 ))
30 .divide(BigInteger.valueOf(i)); // 从n里面取k的概率
31
32 System.out.println( " Your odds are 1 in " + lotteryOdds + " . Good luck! " );
33 }
34 }
3,数组
1)for each循环
for (int element : a) System.out.println(element);
a是一个数组。
2)多维数组,java不规则数组等
参考:《java2核心技术》 卷一 基础知识 第1~3章