打开《Java 核心技术》第一卷第3章,都是些基本的语法要点,看起来没有什么难度,一路就下来了,也画了些笔记,不过都不值得一提了。 但是既然来了,总归要留下点什么吧,于是写了下面的几段代码。
Java数据类型的练习:
import java.util.*;
public class dataTypeTest {
/**
* Creates a new instance of <code>lifeTest</code>.
*/
public dataTypeTest() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello World!");
System.out.printf("新的生命诞生了 ");
System.out.printf("整数家庭诞生了四个小生命... ");
System.out.printf("老大是long ,%d字节,范围{%d~%d) ", Long.SIZE/8, Long.MIN_VALUE, Long.MAX_VALUE);
System.out.printf("老二是int ,%d字节,范围{%d~%d) ", Integer.SIZE/8, Integer.MIN_VALUE, Integer.MAX_VALUE);
System.out.printf("老三是short,%d字节,范围{%d~%d) ", Short.SIZE/8, Short.MIN_VALUE, Short.MAX_VALUE);
System.out.printf("老四是byte ,%d字节,范围{%d~%d) ", Byte.SIZE/8, Byte.MIN_VALUE, Byte.MAX_VALUE);
System.out.printf("Java中没有unsinged的概念,所以四个小家伙没有C++家里的四个孩子重量大 ");
System.out.printf("四个小家伙什么都吃,十进制1000=%d 八进制01000=%d 十六进制0x1000=%d ", 1000, 01000, 0x1000);
System.out.printf("默认的整数是int类型,long类型需要以L结尾 ");
System.out.printf("不过还是要小心别撑着,整数的越界是很麻烦的,特别是八进制和十六进制表示时 ");
System.out.println();
System.out.printf("浮点数家庭诞生了二个小生命... ");
System.out.printf("老大是double,%d字节,范围{%e~%e) ", Double.SIZE/8, Double.MIN_VALUE, Double.MAX_VALUE);
System.out.printf("老二是float ,%d字节,范围{%e~%e) ", Float.SIZE/8, Float.MIN_VALUE, Float.MAX_VALUE);
System.out.printf("默认的浮点数是double,float类型需要以F结尾 ");
System.out.println();
System.out.printf("还有两个未来的天才静悄悄的诞生了... ");
System.out.printf("一个是未来的文科天才char ,%d字节,范围{%d~%d) ",
Character.SIZE/8, (int)Character.MIN_VALUE, (int)Character.MAX_VALUE);
System.out.printf("一个是未来的理科天才boolean,%d字节,范围{%b~%b) ",
1, Boolean.FALSE, Boolean.TRUE);
System.out.println("Java的boolean类型禁止和其它类型相互转换,因为它是麻烦的源泉 ");
}
}
public class dataTypeTest {
/**
* Creates a new instance of <code>lifeTest</code>.
*/
public dataTypeTest() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello World!");
System.out.printf("新的生命诞生了 ");
System.out.printf("整数家庭诞生了四个小生命... ");
System.out.printf("老大是long ,%d字节,范围{%d~%d) ", Long.SIZE/8, Long.MIN_VALUE, Long.MAX_VALUE);
System.out.printf("老二是int ,%d字节,范围{%d~%d) ", Integer.SIZE/8, Integer.MIN_VALUE, Integer.MAX_VALUE);
System.out.printf("老三是short,%d字节,范围{%d~%d) ", Short.SIZE/8, Short.MIN_VALUE, Short.MAX_VALUE);
System.out.printf("老四是byte ,%d字节,范围{%d~%d) ", Byte.SIZE/8, Byte.MIN_VALUE, Byte.MAX_VALUE);
System.out.printf("Java中没有unsinged的概念,所以四个小家伙没有C++家里的四个孩子重量大 ");
System.out.printf("四个小家伙什么都吃,十进制1000=%d 八进制01000=%d 十六进制0x1000=%d ", 1000, 01000, 0x1000);
System.out.printf("默认的整数是int类型,long类型需要以L结尾 ");
System.out.printf("不过还是要小心别撑着,整数的越界是很麻烦的,特别是八进制和十六进制表示时 ");
System.out.println();
System.out.printf("浮点数家庭诞生了二个小生命... ");
System.out.printf("老大是double,%d字节,范围{%e~%e) ", Double.SIZE/8, Double.MIN_VALUE, Double.MAX_VALUE);
System.out.printf("老二是float ,%d字节,范围{%e~%e) ", Float.SIZE/8, Float.MIN_VALUE, Float.MAX_VALUE);
System.out.printf("默认的浮点数是double,float类型需要以F结尾 ");
System.out.println();
System.out.printf("还有两个未来的天才静悄悄的诞生了... ");
System.out.printf("一个是未来的文科天才char ,%d字节,范围{%d~%d) ",
Character.SIZE/8, (int)Character.MIN_VALUE, (int)Character.MAX_VALUE);
System.out.printf("一个是未来的理科天才boolean,%d字节,范围{%b~%b) ",
1, Boolean.FALSE, Boolean.TRUE);
System.out.println("Java的boolean类型禁止和其它类型相互转换,因为它是麻烦的源泉 ");
}
}
控制语句的练习:
import java.util.*;
public class ControlTest {
/**
* Creates a new instance of <code>ControlTest</code>.
*/
public ControlTest() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// 演示if语句
int age = 28;
System.out.printf("今年已经%d了... ", age);
if (age < 10)
System.out.println("还是小屁孩");
else if (age < 18)
System.out.println("还没成人呢");
else if (age < 23)
System.out.println("大学毕业了");
else if (age < 30)
System.out.println("努力工作中");
else if (age < 40)
System.out.println("工作好累啊");
else if (age < 50)
System.out.println("成功或失败");
else if (age < 60)
System.out.println("准备退休了");
else if (age < 70)
System.out.println("安度晚年了");
else if (age < 80)
System.out.println("估计入土了");
else if (age < 90)
System.out.println("寿星显灵了");
else if (age < 100)
System.out.println("我是老妖怪");
else
System.out.println("世界闻名了");
// 演示switch语句
System.out.println("我的老婆出场了...");
//Java的switch只接受一个int,不接受long
char level = 'C';
switch (level)
{
case 'A':
System.out.println("仙乐飘飘,我是不是在做梦?");
break;
case 'B':
System.out.println("得妇如此,夫复何求");
break;
case 'C':
System.out.println("平平淡淡总是真");
break;
case 'D':
System.out.println("生米做成熟饭了");
break;
default:
System.out.println("人世间最悲惨的事莫过如此");
break;
}
// 演示循环语句
System.out.println("准备攒钱买房了...");
int money = 0;
do
{
money += 50000;
}while(money < 500000);
System.out.println("居然还是买不起" + money);
while(money < 1000000)
{
money += 50000;
}
System.out.println("居然还是要贷款" + money);
for (int i = 0; i < 15; i++)
{
money += 30000;
}
System.out.println("买房总共花费了" + money);
System.out.println("命运是如此的奇妙...");
//演示带标签的break语句
int life = 300;
int limit = 0;
age = 0;
lifeover:
while(true)
{
//外在世界的影响
int out = (int)(Math.random() * life);
if (out % 2 == 0)
life -= 2;
else if (out % 3 == 0)
life -= 4;
else if (out % 5 == 0)
life += 1;
while(true)
{
//内心世界的影响
int in = (int)(Math.random() * life);
if (in % 2 == 0)
break;
else if (in % 3 == 0)
life -= 2;
else if (in % 5 == 0)
life += 1;
else
continue;
age++;
if (life < 0)
break lifeover;
}
}
System.out.printf("你共活了%d岁", age);
}
}
public class ControlTest {
/**
* Creates a new instance of <code>ControlTest</code>.
*/
public ControlTest() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// 演示if语句
int age = 28;
System.out.printf("今年已经%d了... ", age);
if (age < 10)
System.out.println("还是小屁孩");
else if (age < 18)
System.out.println("还没成人呢");
else if (age < 23)
System.out.println("大学毕业了");
else if (age < 30)
System.out.println("努力工作中");
else if (age < 40)
System.out.println("工作好累啊");
else if (age < 50)
System.out.println("成功或失败");
else if (age < 60)
System.out.println("准备退休了");
else if (age < 70)
System.out.println("安度晚年了");
else if (age < 80)
System.out.println("估计入土了");
else if (age < 90)
System.out.println("寿星显灵了");
else if (age < 100)
System.out.println("我是老妖怪");
else
System.out.println("世界闻名了");
// 演示switch语句
System.out.println("我的老婆出场了...");
//Java的switch只接受一个int,不接受long
char level = 'C';
switch (level)
{
case 'A':
System.out.println("仙乐飘飘,我是不是在做梦?");
break;
case 'B':
System.out.println("得妇如此,夫复何求");
break;
case 'C':
System.out.println("平平淡淡总是真");
break;
case 'D':
System.out.println("生米做成熟饭了");
break;
default:
System.out.println("人世间最悲惨的事莫过如此");
break;
}
// 演示循环语句
System.out.println("准备攒钱买房了...");
int money = 0;
do
{
money += 50000;
}while(money < 500000);
System.out.println("居然还是买不起" + money);
while(money < 1000000)
{
money += 50000;
}
System.out.println("居然还是要贷款" + money);
for (int i = 0; i < 15; i++)
{
money += 30000;
}
System.out.println("买房总共花费了" + money);
System.out.println("命运是如此的奇妙...");
//演示带标签的break语句
int life = 300;
int limit = 0;
age = 0;
lifeover:
while(true)
{
//外在世界的影响
int out = (int)(Math.random() * life);
if (out % 2 == 0)
life -= 2;
else if (out % 3 == 0)
life -= 4;
else if (out % 5 == 0)
life += 1;
while(true)
{
//内心世界的影响
int in = (int)(Math.random() * life);
if (in % 2 == 0)
break;
else if (in % 3 == 0)
life -= 2;
else if (in % 5 == 0)
life += 1;
else
continue;
age++;
if (life < 0)
break lifeover;
}
}
System.out.printf("你共活了%d岁", age);
}
}
Java数组的练习:
import java.util.*;
public class ArrayTest {
/**
* Creates a new instance of <code>ArrayTest</code>.
*/
public ArrayTest() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String[][] records = new String[5][];
System.out.println(" 500万 500万 500万 500万");
int year = 2005;
for (int i = 0; i < records.length; i++)
{
String[] record = new String[i % 4 + 1];
records[i] = record;
System.out.printf(year + "年 ");
for (int j = 0; j < record.length; j++)
{
String r = record[j];
r = draw();
System.out.printf("%-21s", r);
}
System.out.println();
year++;
}
}
public static String draw()
{
int n = 36;
int k = 7;
int[] numbers = new int[n];
for (int i = 0; i < n; i++)
{
numbers[i] = i + 1;
}
int[] result = new int[k];
for (int i = 0; i < result.length; i++)
{
//这里是从numbers中随机选择一个数
int r = (int)(Math.random() * n);
result[i] = numbers[r];
//这里是把选中的数从numbers中抽出来,算法很精致啊
numbers[r] = numbers[n-1];
n--;
}
//重新排序
Arrays.sort(result);
//打印出来
String ret = "";
for (int r : result)
{
ret += r + ",";
}
ret = ret.substring(0, ret.length()-1);
return ret;
}
}
public class ArrayTest {
/**
* Creates a new instance of <code>ArrayTest</code>.
*/
public ArrayTest() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String[][] records = new String[5][];
System.out.println(" 500万 500万 500万 500万");
int year = 2005;
for (int i = 0; i < records.length; i++)
{
String[] record = new String[i % 4 + 1];
records[i] = record;
System.out.printf(year + "年 ");
for (int j = 0; j < record.length; j++)
{
String r = record[j];
r = draw();
System.out.printf("%-21s", r);
}
System.out.println();
year++;
}
}
public static String draw()
{
int n = 36;
int k = 7;
int[] numbers = new int[n];
for (int i = 0; i < n; i++)
{
numbers[i] = i + 1;
}
int[] result = new int[k];
for (int i = 0; i < result.length; i++)
{
//这里是从numbers中随机选择一个数
int r = (int)(Math.random() * n);
result[i] = numbers[r];
//这里是把选中的数从numbers中抽出来,算法很精致啊
numbers[r] = numbers[n-1];
n--;
}
//重新排序
Arrays.sort(result);
//打印出来
String ret = "";
for (int r : result)
{
ret += r + ",";
}
ret = ret.substring(0, ret.length()-1);
return ret;
}
}
我常说嘛,学习确实是枯燥的,但是乐趣是自己找的。反复练习以上的练习,如果还对这几个语法点不熟悉的话,“来人,把这个家伙直接拉出去埋了”。
随着学习的深入,你可以把上述代码改成C#,Perl,Python,PHP,javascript,sql等等各种版本的,到那时你就清楚了,语言不过是工具,最终终归要解决问题的才是好的语言。