第二章 数据类型
例2-1 在控制台输出商品价目表。
public class Example2_1 {
public static void main(String[] args) {
System.out.println("\t商品价目表");
System.out.println("序号\t商品名称\t商品单价");
System.out.println("1\t美的电饭煲\t300");
System.out.println("2\t小米充电器\t79");
}
}
例2-2 使用变量存储以下手机信息,并按以下样式输出。
public class Example2_2 {
public static void main(String[] args) {
//变量生声明并初始化
String brand = "华为 P10 Plus"; //品牌型号
float screenSize = 5.5f; //主屏尺寸
int ram = 6; //内存大小
int price = 3499; //价格
System.out.println("\t手机");
System.out.print("名称:\t");
//变量使用
System.out.print(brand + "\n");
System.out.print("主屏尺寸:\t");
System.out.print(screenSize + "\n");
System.out.print("内存:\t");
System.out.print(ram + "\n");
System.out.print("价格:\t");
System.out.print(price + "\n");
}
}
例2-3 数据输入。
import java.util.Scanner;
public class Example2_3 {
public static void main(String[] args) {
int x;
float f;
String str;
Scanner sc = new Scanner(System.in); //初始化Scanner对象
System.out.println("请输入一个正整数:"); //输出提示语
x = sc.nextInt(); //将用户从控制台输入的整型数据赋值给变量x
System.out.println("请输入一个浮点型数:"); //输出提示语
f = sc.nextFloat(); //将用户从控制台输入的浮点型数据赋值给变量f
System.out.println("请输入一个字符串:"); //输出提示语
str = sc.next(); //将用户从控制台输入的字符串赋值给变量str
System.out.print("您输入的整型数为");
System.out.println(x);
System.out.print("您输入的浮点型数为");
System.out.println(f);
System.out.print("您输入的字符串为");
System.out.println(str);
}
}
第三章 运算符
例3-1 算术运算符的用法。
public class Example3_1 {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.print("a + b = ");
System.out.println(a + b);
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("b / a = " + (b / a));
System.out.println("b % a = " + (b % a));
System.out.println("a++ = " + (a++));
System.out.println("--b = " + (--b));
}
}
例3-2 赋值运算符的用法。
public class Example3_2 {
public static void main(String[] args) {
int num1 = 5;
int num2 = 2;
int a = num1 % num2;
int b = num1 / num2;
System.out.println(num1 + " % " + num2 + " = " + a);
System.out.println(num1 + " / " + num2 + " = " + b);
}
}
例3-3 已知圆的半径radius=1.5,求其面积。
public class Example3_3 {
public static void main(String[] args) {
final double PI = 3.14159;// 圆周率常量
double radius = 1.5;// 半径
double area = PI * radius * radius;// 计算圆面积
System.out.println("半径为" + radius + "的圆面积是:" + area);
}
}
例3-4 复合赋值运算符的用法。
public class Example3_4 {
public static void main(String[] args) {
int a = 5;
int b = 10;
a += b /= a;
b += a++;
System.out.println("a=" + a);
System.out.println("b=" + b);
}
}
例3-5 强制类型转换案例。
public class Example3_5 {
public static void main(String[] args) {
double d = 100.34;// 定义double型变量d, 并赋初值
int i = (int) d;// 使用强制类型转换, 将d的类型转换为int型
char ch = (char) (i + 3);// 将运算的结果强制转换为char型
System.out.println("字符的值为:" + ch);
}
}
例3-6 逻辑运算符”&“和”&&“的区别。
public class Example3_6 {
public static void main(String[] args) {
int x = 0, y = 0, z = 0;// 定义三个整型变量x,y,z, 初始值都为0
boolean a, b;// 声明两个布尔变量a, b
a = x > 0 & y++ > 1;// 逻辑运算符 & 对表达式进行运算
System.out.println("a = " + a);// 输出a的值
System.out.println("y = " + y);// 输出y的值
b = x > 0 && z++ > 1;
System.out.println("b = " + b);// 输出b的值
System.out.println("z = " + z);// 输出z的值
}
}
第四章 控制语句
例4-1 某银行存款一年的年利率为1.75%, 计算存满一年后的本息。
public class Example4_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入本金:");
double principal = input.nextDouble(); //本金
double total = 0; //本息总额
double rately = 1.75 / 100; //存期1年利率
double interest;
System.out.println("本金为:" + principal);
/* 存满一年的利息和本息 */
interest = principal * rately;
total = principal + interest;
System.out.println("存满一年后的本息是:" + total);
}
}
例4-2 求三个数中的最大数。
public class Example4_2 {
public static void main(String[] args) {
int a, b, c;
Scanner scan = new Scanner(System.in);
System.out.println("请输入三个数:");
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
if (a < b) {
a = b;
}
if (a < c) {
a = c;
}
System.out.println("输入的三个数中最大的数为:" + a);
}
}
例4-3 判断一个正整数是奇数还是偶数。
public class Example4_3 {
public static void main(String[] args) {
int x = 3;
if (x % 2 == 0) {
System.out.println(x + "是偶数!");
}
else {
System.out.println(x + "是奇数!");
}
}
}
例4-4 判断分数对应的等级。
public class Example4_4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入分数:");
int score = scanner.nextInt();// 定义整型变量score, 表示分数
if (score == 5) {
System.out.println("A");
} else if (score == 4) {
System.out.println("B");
} else if (score == 3) {
System.out.println("C");
} else if (score == 2) {
System.out.println("D");
} else if (score == 1 || score == 0) {
System.out.println("E");
} else {
System.out.println("非法成绩!");
}
}
}
例4-5 判断某一年是否为闰年。
public class Example4_5 {
public static void main(String[] args) {
int year = 2008;// 定义年份
boolean leap = false;// 定义布尔变量leap表示是否是闰年, true代表是闰年, false代表是平年
if (year % 4 == 0) {
if (year % 100 != 0) {
leap = true;
} else if (year % 400 == 0) {
leap = true;
}
} else {
leap = false;
}
if (leap) {
System.out.println(year + "年是闰年");
} else {
System.out.println(year + "年是平年");
}
}
}
例4-6 根据输入年和月确定该月的天数。
public class Example4_6 {
public static void main(String[] args) {
// 声明变量year用于存储年份数据
int year;
// 声明变量month用于存储月份数据
int month;
// 初始化变量days用于存储天数
int days = 0;
System.out.println("请输入年月");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
month = sc.nextInt();
switch (month) {
case 2:
// 如果输入月份是2月, 则先判断该年是平年还是闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
// 闰年的2月是29天
days = 29;
} else {
days = 28;
}
break;
// 一年中4/6/9/11月有30天
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
// 1/3/5/7/8/10/12月有31天
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
// 如果输入是月份和上面case子句的值没有匹配上, 则代表月份输入错误
default:
System.out.println("输入月份数据不正确!");
}
System.out.println(year + "年" + month + "月有" + days + "天");
}
}
例4-7 使用hasNextInt()、hasNextFloat()方法进行输入验证。
public class Example4_7 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("输入整数:");
if (scan.hasNextInt()) {
// 判断输入的是否是整数
int i = scan.nextInt();// 接收整数
System.out.println("整数数据:" + i);
} else {
// 输入错误的信息
System.out.println("输入的不是整数!");
scan.nextLine();
}
System.out.print("输入单精度浮点数:");
// 判断输入的是否是单精度浮点数
if (scan.hasNextFloat()) {
float f = scan.nextFloat();
System.out.println("单精度浮点数据:" + f);
} else {
// 输入错误的信息
System.out.println("输入的不是浮点数!");
}
}
}
例4-8 编程求1+2+3+…+100的值。
public class Example4_8 {
public static void main(String[] args) {
int i, sum;// i作为循环因子, sum作为求和结果
sum = 0;// sum要进行初始化, 因为求和的初值为0
for (i = 1; i < 101; i++) {// for循环中起点为1, 终点为100, 步长为1
sum += i;// 相当于sum = sum + 1;
}
System.out.println("1+2+3+...+100=" + sum);
}
}
例4-9 下面的程序的错误在哪里。
public class Example4_9 {
public static void main(String[] args) {
int i, sum;// i作为循环因子, sum作为求和结果
for (i = 1; i < 101; i++) {// for循环中起点为1, 终点为100, 步长为1
sum = 0;// sum赋初值0
sum += i;// 相当于sum=sum+i;
}
System.out.println("1+2+3+...+100=" + sum);
}
}
例4-10 计算1-100中所有的偶数的和。
方法一:步长为1。
public class Example4_10 {
public static void main(String[] args) {
int sum = 0;// sum记录所有偶数的和, 初值设置为0
for (int i = 1; i <= 100; i++) {//起点为1, 步长为1, 终点为100
if (i % 2 == 0) {
// 判断是否为偶数
sum += i;// 求所有偶数的和, 相当于sum = sum + i;
}
}
System.out.println("1-100之间偶数的和是" + sum);
}
}
方法二:步长为2。
public class Example4_10 {
public static void main(String[] args) {
int sum = 0;
for (int i = 2; i <= 100; i = i + 2) {
sum += i;
}
System.out.println("1-100之间偶数的和是" + sum);
}
}
例4-11 求5的阶乘。
public class Example4_11 {
public static void main(String[] args) {
int teg = 1;// 记录阶乘的结果, 初值为1
int i = 5;// 阶乘的初值从5开始
while (i >= 1) {// 控制循环次数
teg *= i;
i--;
}
System.out.println("5的阶乘的结果是" + teg);
}
}
例4-12 求A-Z所有的大写字母的Unicode值的和。
public class Example4_12 {
public static void main(String[] args) {
char c = 'A';
int sum = 0;
do {
sum += c;
c++;
} while (c <= 'Z');
System.out.println("A-Z的值的和是" + sum);
}
}
例4-13 顺序输出1-5的数字。
public class Example4_13 {
public static void main(String[] args) {
int i = 1;
System.out.println("观察i的值:");
while (i < 5) {
System.out.println(i);
i++;
}
}
}
例4-14 从控制台输入5个正整数,并求出这5个数的和。一旦输入了负数,则停止输入,并计算之前输入的正数和。
public class Example4_14 {
public static void main(String[] args) {
int sum = 0;
int num;
int i;
Scanner sc = new Scanner(System.in);
for (i = 1; i < 5; i++) {
System.out.println("请输入第" + i + "个正整数");
num = sc.nextInt();
if (num <= 0) {
System.out.println("你输入的" + num + "是负数!");
break;
}
sum += num;
}
System.out.println((i - 1) + "个正数之和为:" + sum);
}
}
例4-15 系统随机生成一个1-100内的数据,然后记录,有用户猜这个数据,当猜数据大于系统生成的数据时,就提示用户大了,如果用户猜的数据小于系统生成的数据,那么提示小了。只有当用户猜对的时候,才提示用户猜对了,停止运行。
public class Example4_15 {
public static void main(String[] args) {
int ran = (int) (Math.random() * 100 + 1);// 生成随机数, 为1-100内
Scanner sc = new Scanner(System.in);
// 创建一个屏幕取数据的对象 Scanner为屏幕扫描类
System.out.println("请输入一个1-100之间的数据, 计算机会告诉你是不是输入正确的");
for (;;) {
int res = sc.nextInt();
if (res != ran) {
if (res > ran) {
System.out.println("输入的结果大了");
} else {
System.out.println("输入的结果小了");
}
System.out.println("请重新猜一个数字");
} else {
System.out.println("您猜得很对, 太牛了!!!");
break;
}
}
}
}
例4-16 求1-10的不能被3整除的数
public class Example4_16 {
public static void main(String[] args) {
int i = 0;
System.out.println("1-10之间所有不能被3整除的数是:");
while (i < 10) {
i++;
if (i % 3 == 0)
continue;// 如果能被3整除, 则结束本次循环继续下一次
System.out.print(i+"\t");
}
}
}
例4-17 打印一个矩形图。
public class Example4_17 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
例4-18 打印一个直角三角形。
public class Example4_18 {
public static void main(String[] args) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
例4-19 使用多重循环实现九九乘法表
public class Example4_19 {
public static void main(String[] args) {
int i, j;// 定义两个变量 作为九九乘法表的内外循环变量
for (i = 1; i <= 9; i++) {
for (j = 1; j <= 9; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
if (i == j) {// i=j的时候就换行, 终止本次循环
break;// 跳转到外层循环
}
}
System.out.println();
}
}
}
例4-20 改写九九乘法表
public class Example4_20 {
public static void main(String[] args) {
int i, j;// 定义两个变量 作为九九乘法表的内外循环变量
out:// 使用out标记外层循环
for (i = 1; i <= 9; i++) {
for (j = 1; j <= 9; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
if (i == j) {// i = j的时候, 就会换行, 终止本次循环
System.out.println();// 终止循环前要换行
continue out;// 跳转到外层循环
}
}
}
}
}
第五章 数组
例5-1 软件专业1班共有学生32人,现需要将学生的学号进行存储,学号为1001,1002,…,1032,使用数组对这些数据进行存储。
public class Example5_1 {
public static void main(String[] args) {
int[] arr = new int[32];// 定义一个整型数组, 含有32个元素
for (int i = 0; i < arr.length; i++) {// 使用for循环进行赋值
arr[i] = 1001 + i;// 给元素赋值
}
for (int i = 0; i < arr.length; i++) {// 遍历数组
System.out.println("第" + (i + 1) + "位同学的学号是" + arr[i]);
}
}
}
例5-2 请使用冒泡排序法将6,9,7,4,3这5个数按从小到大的顺序排序。
public class Example5_2 {
public static void main(String[] args) {
// 定义数组
int[] arr = {6, 9, 7, 4, 3};
// 输出排序前序列
System.out.println("原序列为:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
// 冒泡排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
// 输出排序结果
System.out.println("\n排序后序列为:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
例5-3 有一组数据,共计10个,分别是10,8,41,22,30,55,2,18,15,33,这一组数据的大小是无序的,请使用数组的sort方法对这些数据进行排序。
public class Example5_3 {
public static void main(String[] args) {
int[] arr = {10, 8, 41, 22, 30, 55, 2, 18, 15, 33};// 直接给数组赋值
Arrays.sort(arr);
System.out.print("排序后的数组:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
例5-4 逆序输出字符串“you are the best"。
public class Example5_4 {
public static void main(String[] args) {
String str = "you are the best";// 创建一个数组并赋值
char[] arr = str.toCharArray();// 将字符串转换为数组
char[] brr = new char[arr.length];// 定义一个与转化数组等长度的字符数组
for (int i = 0; i < arr.length; i++) {
brr[i] = arr[arr.length - 1 - i];
}
String strTarget = new String(brr);// 将字符数组转化为字符串
System.out.println(strTarget);
}
}
例5-5 给定一个矩阵,求该矩阵的转置矩阵。
public class Example5_5 {
public static void main(String[] args) {
int[][] a = {{2, 5, 0, -1}, {1, 0, -2, 3}};
int b[][] = new int[4][2];
// 原矩阵
System.out.println("原矩阵为:");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
// 转置
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
b[j][i] = a[i][j];
}
}
// 输出转置矩阵
System.out.println("转置矩阵为:");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
System.out.print(b[i][j]+"\t");
}
System.out.println();
}
}
}