Java第5,6次作业

一:交换二维数组

 

package 百度;

public class ASD {

	public static void main(String[] args) {
        // TODO Auto-generated method stub
          int [][]array= {{91,25,8},{56,14,2},{47,3,67}}; //创建直接排序类的对象
          System.out.println("——原始数组——"); //输出原数组
          for(int i=0;i<3;i++) { //循环排序后i的数组
           for(int j=0;j<3;j++) { //循环排序后j的数组
            System.out.print(array[i][j]+"\t"); //输出数组中的每个元素
            while(j==2) {// while语句
             System.out.println();//输出结果 
break;//break跳出循环
            }
           }
          }
          System.out.println("——置换后的数组——");//输出置换后的数组
          for(int i=0;i<3;i++) {//循环排序后i的数组
           for(int j=0;j<3;j++) {//循环排序后j的数组
            System.out.print(array[j][i]+"\t"); //输出数组中的每个元素
            while(j==2) {// while语句
             System.out.println();//输出结果
break;//break跳出循环
            }
           }
           }
          }
    
 
    }

结果:

 二:查询成绩

System.out.println("您想调取第几位学生的答题结果(有效数字为1~8):");        
try (Scanner sc = new Scanner(System.in)) {
            int a=sc.nextInt();
            System.out.print("第"+a+"位同学的全部答案为:");
            int sum=0;
            char score[][]=new char[8][10];
char arr[]=new char[]{'B',' ','A',' ','D',' ','C',' ','C',' ','B',' ','C',' ','A',' ','D',' ','B'};
 score[0]=new char[]{'A',' ','A',' ','D',' ','B',' ','A',' ','B',' ','C',' ','A',' ','C',' ','B'};
score[1]=new char[]{'B',' ','A',' ','D',' ','B',' ','A',' ','B',' ','C',' ','A',' ','C',' ','B'};
score[2]=new char[]{'B',' ','A',' ','D',' ','C',' ','A',' ','B',' ','C',' ','A',' ','C',' ','B'};
score[3]=new char[]{'B',' ','A',' ','D',' ','B',' ','A',' ','B',' ','C',' ','A',' ','C',' ','B'};
score[4]=new char[]{'B',' ','A',' ','D',' ','B',' ','A',' ','B',' ','C',' ','A',' ','C',' ','B'};
score[5]=new char[]{'B',' ','A',' ','D',' ','B',' ','A',' ','B',' ','C',' ','A',' ','C',' ','B'};
score[6]=new char[]{'B',' ','B',' ','D',' ','B',' ','A',' ','B',' ','C',' ','A',' ','C',' ','B'};
score[7]=new char[]{'B',' ','A',' ','C',' ','B',' ','A',' ','B',' ','C',' ','A',' ','C',' ','B'};
             System.out.println();//自动换行
                System.out.print(score[a-1]);//输出数组即该同学的答案
                    
                for (int i = 0; i < 19; i++)
                {
                    if (arr[i] == score[a-1][i]){                        
                        sum++;               
                        
                        }       
                       
                    }
                 System.out.println();
                    System.out.println("第"+a+"位同学一共答对了"+(sum-9)+"道题");
        }
                }
    }

 结果:三:杨辉三角算法

 

package 百度;


public class ASD {
	public static void main(String[] args) {
        // TODO Auto-generated method stub
         int arr[][] = new int[11][10];
         for (int i = 0; i < arr.length; i++) {
          for (int j = 0; j < i; j++) {
           if (j == 0 || j == i) {
            arr[i][j] = 1;
           } else {
            arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
           }
           System.out.print(arr[i][j] + "\t");
          }
          System.out.println();
         }
 
    }
 
}

 结果

四:统计成绩

package 百度;

import java.util.Scanner;

public class ASD {
	public static void main(String[] args) {
        // TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
        int a=3;//定义int型
        int b=3;//定义int型
        int name[]=new int[] {1,2,3};//定义二维数组
        String course[]=new String[] {"语文成绩","数学成绩","英语成绩"};//储存课程
        int[][] score=new int[a][b];//存储各科成绩
        int[] sum=new int[a];//储存总分
        double[] avg=new double[a];//储存平均分
        String[] content=new String[a];//储存学生信息
        for(int i=0;i<a;i++) {
            System.out.println("请输入第"+(i+1)+"个学生的编号:");//输出语句
            Scanner sc=new Scanner(System.in);//创建扫描器
            name[i]=sc.nextInt();//在控制台输入一个数
            content[i]=name[i]+"\t\t";//拼接编号
            for (int j=0;j<b;j++) {
                System.out.println("请输入"+course[j]+"的:");
                score[i][j]=sc.nextInt();
                sum[i]+=score[i][j];//计算总分
                content[i]+=score[i][j]+"\t\t";
            }
            avg[i]=sum[i]/a;//计算平均分
            content[i]+=avg[i]+"\t\t"+sum[i]+"\t\t";
        }
        System.out.println("学生成绩结果如下:");
        System.out.println("--------------------------------------------------------------------------------------");
        System.out.print("学生编号\t\t");
        for(int i=0;i<b;i++) {
            System.out.print(course[i]+"\t\t");
        }
        System.out.println("平均成绩\t\t总成绩"); 
        for(int i=0;i<a;i++) {
            System.out.println(content[i]);
        }
        
}
}

 结果:

五: 客车售票

package 百度;

import java.util.Scanner;

public class ASD {
	public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("简单客车售票系统");//输出语句
        System.out.println("9排4车的大巴车开始售票");//输出语句
        char arr[][]=new char[9][4];//定义9行4列
        arr[0]=new char[] {'1','1','1','1'};//为第一行赋值
        arr[1]=new char[] {'1','1','1','1'};
        arr[2]=new char[] {'1','1','1','1'};
        arr[3]=new char[] {'1','1','1','1'};
        arr[4]=new char[] {'1','1','1','1'};
        arr[5]=new char[] {'1','1','1','1'};
        arr[6]=new char[] {'1','1','1','1'};
        arr[7]=new char[] {'1','1','1','1'};
        arr[8]=new char[] {'1','1','1','1'};
        for(int i=0;i<9;i++) {//循环行
            for(int j=0;j<4;j++) {//循环列
                System.out.print(arr[i][j]+"\t");//输出语句
            }
            System.out.println();//换行
        }
        System.out.println("请输入要预定的坐位行号:");//输出语句
        Scanner a=new Scanner(System.in);//创建扫描仪
        int b=a.nextInt();//控制台输出一个数字
        System.out.println("请输入要预定的坐位列号");//输出语句
        Scanner sc=new Scanner(System.in);//创建扫描仪
        int c=sc.nextInt();//控制台输出一个数字
        System.out.println("简单客车售票系统");//输出语句
        System.out.println("9排4列的大巴车开始售票");//输出语句
        char s[]=new char[] {'0'};//定义s为0
        arr[b-1][c-1]=s[0];//定义哪行和哪列为0
        for(int e=0;e<9;e++) {//循环行
            for(int r=0;r<4;r++) {//循环列
            System.out.print(arr[e][r]+"\t");//输出语句
            }
            System.out.println();//换行
        }
    }
 
    }

结果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值