20220326 java基础代码题(二)

1)使用一维数组存储键盘字母 分别把键盘上每一排字母按键都保存成一个一维数组,利用数组长度分别输出键盘中3排字母键的个数。

public class Keys {
	public static void main(String[] args) {
		char[] firstRow = {'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'};
		char[] secondRow = {'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'};
		char[] thirdRow = {'Z', 'X', 'C', 'V', 'B', 'N', 'M'};
		System.out.println("键盘上第一排的字母键有" + firstRow.length + "个;");
		System.out.println("键盘上第二排的字母键有" + secondRow.length + "个;");
		System.out.println("键盘上第三排的字母键有" + thirdRow.length + "个。");
	}
}

2)寻找空储物箱 超市有20个储物箱,现第2、3、5、8、12、13、16、19、20号尚未使用,使用数组的长度分别输出尚未使用的储物箱个数以及已经使用的储物箱个数。

public class StorageBox {
	public static void main(String[] args) {
		int[] totalNum = new int[20];
		int[] emptyNum = {2, 3, 5, 8, 12, 13, 16, 19, 20};
		System.out.println("超市中有储物箱" + totalNum.length + "个。");
		System.out.println("超市中尚未被使用的储物箱有" + emptyNum.length + "个。");
		System.out.println("超市中已经被使用的储物箱有" + (totalNum.length - emptyNum.length) + "个。");
	}
}

3)模拟书柜放书 一个私人书柜有3层2列,分别向该书柜第1层第1列放入历史类读物,向该书柜第2层第1列放入经济类读物,向该书柜第2层第2列放入现代科学类读物。创建一个二维数组,并给该二维数组赋值。

public class BookCase {
	public static void main(String[] args) {
		String[][] bookshelf = new String[3][2];
		bookshelf[0][0] = "历史类读物";
		bookshelf[1][0] = "经济类读物";
		bookshelf[1][1] = "科学类读物";
		System.out.println("向该书柜第1层第1列放入" + bookshelf[0][0]);
		System.out.println("向该书柜第2层第1列放入" + bookshelf[1][0]);
		System.out.println("向该书柜第2层第2列放入" + bookshelf[1][1]);
	}
}

4)输出古诗 创建Poetry类,声明一个字符型二维数组,将古诗《春晓》的内容赋值于二维数组,然后分别用横版和竖版两种方式输出。

public class Poetry {
	public static void main(String[] args) {
		 arr[][] char = new char[4][]; // 创建一个4行的二维数组
		arr[0] = new char[] { '春', '眠', '不', '觉', '晓' }; // 为每一行赋值
		arr[1] = new char[] { '处', '处', '闻', '啼', '鸟' };
		arr[2] = new char[] { '夜', '来', '风', '语', '声' };
		arr[3] = new char[] { '花', '落', '知', '多', '少' };
		/* 横版输出 */
		System.out.println("-----横版-----");
		for (int i = 0; i < 4; i++) { // 循环4行
			for (int j = 0; j < 5; j++) { // 循环5列
				System.out.print(arr[i][j]); // 输出数组中的元素
			}
			if (i % 2 == 0) {
				System.out.println(","); // 如果是一、三句,输出逗号
			} else {
				System.out.println("。"); // 如果是二、四句,输出句号
			}
		}
		/* 竖版输出 */
		System.out.println("\n-----竖版-----");
		for (int j = 0; j < 5; j++) { // 列变行
			for (int i = 3; i >= 0; i--) { // 行变列,反序输出
				System.out.print(arr[i][j]); // 输出数组中的元素
			}
			System.out.println(); // 换行
		}
		System.out.println("。,。,"); // 输出最后的标点
	}
}

5)数独 将从1到9的数字放入一个3×3的数组中,判断数组每行每列以及每个对角线的值相加是否都相同

public class NineGrids {
	public static void main(String[] args) {
		int[][]  arr= { { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } };

		boolean result = true;
		int sum = arr[0][0] + arr[1][1] + arr[2][2];// 计算一条斜边和
		if (sum != arr[0][2] + arr[1][1] + arr[2][0]) {
			result = false;

		} else {
			for (int x = 0; x < 3; x++) {
				// 计算行、列的和
				if (sum != arr[x][0] + arr[x][1] + arr[x][2] || sum != arr[0][x] + arr[1][x] + arr[2][x]) {
					result = false;
					break;
				}
			}
		}

		if (result) {
			System.out.println("数组行、列、对角线的和相等");
		} else {
			System.out.println("数组行、列、对角线的和不相等");
		}
	}
}

6)矩阵转置 交换二维数组“int[][] array = {{ 91,25,8 }, { 56,14,2 }, { 47,3,67 }};”的行、列数据。

public class SwapRC {// 交换二维数组的行列数据
	public static void main(String[] args) {
		int i, j;// 定义两个变量,分别用来作为行和列的循环变量
		// 初始化一个静态的int型二维数组
		int[][] array = { { 8, 75, 23 }, { 21, 55, 34 }, { 15, 23, 20 } };
		System.out.println("—————原始数组—————");// 提示信息
		// 遍历原始的二维数组
		for (i = 0; i < 3; i++) {
			for (j = 0; j < 3; j++) {
				System.out.print(array[i][j] + "\t");// 输出原始数组中的元素
			}
			System.out.println();// 换行
		}
		int temp;// 临时变量
		// 通过循环调换元素的位置
		for (i = 0; i < 3; i++) {
			for (j = 0; j < i; j++) {
				temp = array[i][j];// 把数组元素赋给临时变量
				// 交换行列数据
				array[i][j] = array[j][i];
				array[j][i] = temp;
			}
		}
		System.out.println("——调换位置之后的数组——");// 提示信息
		// 遍历调换位置之后的二维数组
		for (i = 0; i < 3; i++) {
			for (j = 0; j < 3; j++) {
				System.out.print(array[i][j] + "\t");// 输出调换位置后的数组元素
			}
			System.out.println();// 换行
		}
	}
}

7) 统计学生成绩 输入学生的学号及语文、数学、英语成绩,输出学生各科成绩信息、平均成绩和总成绩。

import java.util.Scanner;

public class SchoolReport {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入本班学生总数:");
		int studentcout = sc.nextInt();
		int achivement[][] = new int[studentcout][4];
		for (int i = 0; i < studentcout; i++) {
			System.out.println("请输入第" + (i + 1) + "个学生的编号:");
			achivement[i][0] = sc.nextInt();
			System.out.println("请输入语文成绩:");
			achivement[i][1] = sc.nextInt();
			System.out.println("请输入数学成绩:");
			achivement[i][2] = sc.nextInt();
			System.out.println("请输入英语成绩:");
			achivement[i][3] = sc.nextInt();
		}
		System.out.println("学生成绩结果如下");
		System.out.println("---------------------------------------------");
		System.out.println("学生编号\t语文成绩\t数学成绩\t英语成绩\t平均成绩\t总成绩");

		for (int i = 0; i < achivement.length; i++) {
			double sum = 0;	//总成绩
			double ave = 0;	//平均成绩
			for (int j = 0; j < achivement[i].length; j++) {
				System.out.print(achivement[i][j] + "\t");
				if (j > 0) {
					sum += achivement[i][j];
				}
			}
			ave = sum / 3;
			System.out.print(String.format("%.2f", ave) + "\t" + (int) sum
					+ "\n");
		}
	}
}

 

 


 

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Royalreairman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值