综合练习5 五子棋游戏
编写一个建议五子棋游戏,在控制台上绘制棋盘,奇葩马上每个点都有对应的坐标,下棋者输入俩五子棋对应的坐标表示棋子的落点,其中,第一个数字表示横坐标,第二个数字表示纵坐标。
public static void main(String[] args) {
System.out.println("------------------------------------------");
String a[][] = new String[11][11];//创建一个11x11的数组
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i == 0&&j == 0) {//在数组的左上角填入空格
a[i][j] = " ";
}
else if (i == 0) {//将1-10填入第一行
a[i][j] = Integer.toString(j-1);
}
else if (j == 0) {//将1-10填入第一列
a[i][j] = Integer.toString(i-1);
} else if (i == 4 && j == 6||i == 6 && j == 5|| i == 7 && j == 7) {
a[i][j] = "O";//填入已有的棋子
}
else if (i == 5 && j == 5 ||i == 5 && j == 6||i == 6 && j == 6) {
a[i][j] = "*";//填入已有的棋子
}
else {
a[i][j] = "-";其余地方填入-
}
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
System.out.println("------------------------------------------");
System.out.print("请*输入棋子坐标:");
}
综合练习6 统计学生成绩
输入学生的语文数学英语成绩,输出学生各科成绩信息,平均分和总成绩。
public static void main(String[] args) {
int C , M , E , sum , num;//定义必要的变量
double ave ;
String a[][] = new String[10][10];
Scanner sc = new Scanner(System.in);//输入学号和成绩
System.out.println("请输入你要统计的学号:");
num= sc.nextInt();
System.out.println("请输入你要统计的语文成绩:");
C= sc.nextInt();
System.out.println("请输入你要统计的数学成绩:");
M= sc.nextInt();
System.out.println("请输入你要统计的英语成绩:");
E= sc.nextInt();
ave = (C+M+E)/3;
sum = C+E+M;
if(num < 1){判断学号的输入
System.out.println("您输入的学号有误,请重新输入。");
}
else {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {//存入信息
if (i == 0 && j == 0){
a[i][j] = "学生编号";
}
if (i == 0 && j == 1){
a[i][j] = "语文成绩";
}
if (i == 0 && j == 2){
a[i][j] = "数学成绩";
}
if (i == 0 && j == 3){
a[i][j] = "英语成绩";
}
if (i == 0 && j == 4){
a[i][j] = "平均成绩";
}
if (i == 0 && j == 5){
a[i][j] = "总成绩";
}
a[num][0] = Integer.toString(num);
if (j == 1){
a[num][j] = Integer.toString(C);
}
else if (j == 2){
a[num][j] = Integer.toString(M);
}
else if (j == 3){
a[num][j] = Integer.toString(E);
}
else if (j == 4){
a[num][j] = Double.toString(ave);
}
else if (j == 5){
a[num][j] = Integer.toString(sum);
}
}
}
}
for (int i = 0; i < 6; i++) {//如果数组的元素是空的那么输出空格
for (int j = 0; j < 6; j++) {
if (a[i][j] == null){
a[i][j] = " ";
}
}
}
for (int i = 0; i < 6; i++) {//输出数组
for (int j = 0; j < 6; j++) {
if (i>=1){
System.out.print(a[i][j] +"\t" + " ");
}
else {
System.out.print(a[i][j] +"\t" );
}
}
System.out.println();
}
}
综合练习7 模拟客车售票
一辆大巴有9排4列的座位,编写一个程序模拟大巴车的售票过程。
public static void main(String[] args) {
boolean a[][] = new boolean[9][4];//定义一个数组
System.out.println("简单客车售票系统");
System.out.println("九排四列的大巴车开始售票");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = true;
int temp = a[i][j] ? 1 : 0;//三元运算符
System.out.print(temp + "\t");
}
System.out.println();
}
Scanner sc = new Scanner(System.in);//输入信息
System.out.print("请输入你要预定的行号:");
int hang = sc.nextInt();
System.out.print("请输入你要预定的列号:");
int lie = sc.nextInt();
if (a[hang-1][lie-1]) {//如果该座位为1则购票成功将1改为0
a[hang-1][lie-1] = false;
System.out.println("九排四列的大巴车开始售票");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
int temp = a[i][j] ? 1 : 0;
System.out.print(temp + "\t");
}
System.out.println();
}
}
else {//购票失败
System.out.println("该座位已经售出,请重新选择。");
}
}
综合练习8 自动批卷程序
现有学号为1-8的八名学生和十道题目,将学生的答案存储在一个二维数组中,通过学号找到并输出该学生的答案和回答正确的题目总数。
public static void main(String[] args) {
String a[][] = new String[8][11];//创建一个存储数据的数组
String chance[] = new String[]{"A","B","C","D"};//将选项存入一个新的数组
Random x = new Random();//创建一个随机变量
int sum = 0;
for (int i = 0; i < a.length; i++) {//为每个学生循环输入数据
for (int j = 0; j < a[i].length; j++) {//第一列输入序号
if (j == 0) {
//定义的数组为String,将i的类型转变为String
a[i][j] = Integer.toString(i+1);
}
else {
a[i][j] = chance[x.nextInt(chance.length)];//随即输入选项
}
System.out.print(a[i][j] + " ");
}
System.out.println();
}
//定义答案数组
String aw[] = new String[]{"B","A","D","C","C","B","C","A","D","B"};
Scanner sc = new Scanner(System.in);//输入查询信息
System.out.print("您想调取第几位学生的答题结果:");
int n = sc.nextInt();
System.out.println("第"+n+"位同学的全部答案为:");
for (int i = 0; i < 10; i++) {//循环输出数组的信息
System.out.print(a[n-1][i+1]+"\t");
}
System.out.println();
for (int i = 0; i < 10; i++) {//判断正确的数量
if(aw[i] == a[n-1][i+1]){
sum = sum + 1;
}
}
System.out.println("第" + "n" + "位同学一共答对了" + sum +"道题。");
}