控制流程条件语句if (condition)
statement1
else
statement2
可以没有else部分。
循环while (condition)
statement
- import java.util.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
-
- System.out.print("How much money do you need to retire? ");
- double goal = in.nextDouble();
-
- System.out.print("How much money will you contribute every year? ");
- double payment = in.nextDouble();
-
- System.out.print("Interest rate in %: ");
- double interestRate = in.nextDouble();
-
- double balance = 0;
- int years = 0;
-
- while (balance < goal)
- {
- balance += payment;
- double interest = balance * interestRate / 100;
- balance += interest;
- years++;
- }
-
- System.out.println("You can retire in " + years + " years.");
- }
- }
复制代码 do
statement
while (condition);
- import java.util.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- System.out.print("How much money will you contribute every year? ");
- double payment = in.nextDouble();
- System.out.print("Interest rate in %: ");
- double interestRate = in.nextDouble();
- double balance = 0;
- int year = 0;
- String input;
- do
- {
- balance += payment;
- double interest = balance * interestRate / 100;
- balance += interest;
- year++;
- System.out.printf("After year %d, your balance is %,.2f\n", year, balance);
- System.out.print("Ready to retire? (Y/N) ");
- input = in.next();
- } while (input.equals("N"));
- }
- }
复制代码 for语句的第一部分通常用于对计数器初始化;
第二部分给出每次新一轮循环执行前要检测的循环条件;
第三部分指示如何更新计数器。
- import java.util.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
-
- System.out.print("How many numbers do you need to draw? ");
- int k = in.nextInt();
-
- System.out.print("What is the highest number you can draw? ");
- int n = in.nextInt();
-
- int lotterOdds = 1;
- for (int i = 1; i <= k; i++)
- lotterOdds = lotterOdds * (n - i + 1) / i;
-
- System.out.println("Your odds are 1 in " + lotterOdds + ". Good luck!");
- }
- }
复制代码 多重选择switch语句将从与选项值相匹配的case标签处开始执行直到遇到break语句,或者执行到switch语句的结束处为止。
如果没有相匹配的case标签,而有default子句,就执行这个子句。
中断控制流程语句break语句用于跳出语句块。
continue语句将控制转移到循环首部。
大数值BigInteger类和BigDecimal类用于处理包含任意长度数字序列的数值。
使用valueOf方法将普通数值转换为大数值。使用add和multiply方法进行加法和乘法运算。
- import java.util.*;
- import java.math.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
-
- System.out.print("How many numbers do you need to draw? ");
- int k = in.nextInt();
-
- System.out.print("What is the highest number you can draw? ");
- int n = in.nextInt();
-
- BigInteger lotteryOdds = BigInteger.valueOf(1);
-
- for (int i = 1; i <= k; i++)
- lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1).divide(BigInteger.valueOf(i)));
-
- System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
- }
- }
复制代码 数组
数组一旦创建,就不能再改变大小。如果需要在运行时扩展数组,应该使用ArrayList。
for each循环用来处理元素集合中的每个元素,格式为:for (variable : collection) statement
声明并初始化数组可以有两种形式:int[] a = new int[] { 0, 1, 2 }或int[] a = { 0, 1, 2 }。
打印数组可以用toString方法,拷贝数组可以用copyOf方法。
对数值型数组进行排序,可以使用sort方法。
- import java.util.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
-
- System.out.print("How many numbers do you need to draw? ");
- int k = in.nextInt();
-
- System.out.print("What is the highest number you can draw? ");
- int n = in.nextInt();
-
- int[] numbers = new int[n];
- for (int i = 0; i < numbers.length; i++)
- numbers[i] = i + 1;
-
- int[] result = new int[k];
- for (int i = 0; i < result.length; i++)
- {
- int r = (int) (Math.random() * n);
-
- result[i] = numbers[r];
-
- numbers[r] = numbers[n - 1];
- n--;
- }
-
- Arrays.sort(result);
- System.out.println("Bet the following combination. It'll make you rich!");
- for (int r : result)
- System.out.println(r);
- }
- }
复制代码 可以把二维数组看成矩阵,以行列的角度索引二维数组中的元素。
- public class HelloJava
- {
- public static void main(String[] args)
- {
- final double STARTRATE = 10;
- final int NRATES = 6;
- final int NYEARS = 10;
-
- double[] interestRate = new double[NRATES];
- for (int j = 0; j < interestRate.length; j++)
- interestRate[j] = (STARTRATE + j) / 100.0;
-
- double[][] balances = new double[NYEARS][NRATES];
-
- for (int j = 0; j < balances[0].length; j++)
- balances[0][j] = 10000;
-
- for (int i = 1; i < balances.length; i++)
- {
- for (int j = 0; j < balances[i].length; j++)
- {
- double oldBalance = balances[i - 1][j];
-
- double interest = oldBalance * interestRate[j];
-
- balances[i][j] = oldBalance + interest;
- }
- }
-
- for (int j = 0; j < interestRate.length; j++)
- System.out.printf("%9.0f%%", 100 * interestRate[j]);
-
- System.out.println();
-
- for (double[] row : balances)
- {
- for (double b : row)
- System.out.printf("%10.2f", b);
-
- System.out.println();
- }
- }
- }
复制代码 二维数组每一行的长度可以不同。
- public class HelloJava
- {
- public static void main(String[] args)
- {
- final int NMAX = 10;
-
- int[][] odds = new int[NMAX + 1][];
- for (int n = 0; n <= NMAX; n++)
- odds[n] = new int[n + 1];
-
- for (int n = 0; n < odds.length; n++)
- for (int k = 0; k < odds[n].length; k++)
- {
- int lotteryOdds = 1;
- for (int i = 1; i <= k; i++)
- lotteryOdds = lotteryOdds * (n - i + 1) / i;
-
- odds[n][k] = lotteryOdds;
- }
-
- for (int[] row : odds)
- {
- for (int odd : row)
- System.out.printf("%4d", odd);
- System.out.println();
- }
- }
- }
复制代码
|
|