Day022

1题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。

[html]  view plain  copy
  1. import java.util.Scanner;  
  2. public class Day22{  
  3.    public static void main(String[] args){  
  4.        System.out.print("请输入7个整数(1-50):");  
  5.        Scanner scan = new Scanner(System.in);  
  6.        int n1 = scan.nextInt();  
  7.        int n2 = scan.nextInt();  
  8.        int n3 = scan.nextInt();  
  9.        int n4 = scan.nextInt();  
  10.        int n5 = scan.nextInt();  
  11.        int n6 = scan.nextInt();  
  12.        int n7 = scan.nextInt();  
  13.        scan.close();  
  14.        printStar(n1);  
  15.        printStar(n2);  
  16.        printStar(n3);  
  17.        printStar(n4);  
  18.        printStar(n5);  
  19.        printStar(n6);  
  20.        printStar(n7);  
  21.     }  
  22.    static void printStar(int m){  
  23.        System.out.println(m);  
  24.        for(int i=0;i<m;i++)  
  25.          System.out.print("*");  
  26.        System.out.println();  
  27.     }  
  28. }   

2.题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

[html]  view plain  copy
  1. public class Prog48{  
  2.    public static void main(String[] args){  
  3.       int n = 1234;  
  4.        int[] a = new int[4];  
  5.        for(int i=3;i>=0;i--){  
  6.          a[i] = n%10;  
  7.          n /= 10;  
  8.        }  
  9.        for(int i=0;i<4;i++)  
  10.          System.out.print(a[i]);  
  11.        System.out.println();  
  12.        for(int i=0;i<a.length;i++){  
  13.          a[i] += 5;  
  14.          a[i] %= 10;  
  15.       }  
  16.        int temp1 = a[0];  
  17.        a[0] = a[3];  
  18.        a[3] = temp1;  
  19.        int temp2 = a[1];  
  20.        a[1] = a[2];  
  21.        a[2] = temp2;  
  22.        for(int i=0;i<a.length;i++)  
  23.          System.out.print(a[i]);  
  24.     }  
  25. }  

3.题目:计算字符串中子串出现的次数

[html]  view plain  copy
  1. public class Prog49{  
  2.    public static void main(String[] args){  
  3.        String str = "I come from County DingYuan Province AnHui.";  
  4.        char[] ch = str.toCharArray();  
  5.        int count = 0;  
  6.       for(int i=0;i<ch.length;i++){  
  7.            if(ch[i]==' ')  
  8.               count++;  
  9.        }  
  10.        count++;  
  11.        System.out.println("共有"+count+"个字串");  
  12.     }  
  13. }  

4.题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。

[html]  view plain  copy
  1. import java.io.*;  
  2. public class Prog50{  
  3.    //定义学生模型  
  4.    String[] number = new String[5];  
  5.    String[] name = new String[5];  
  6.    float[][] grade = new float[5][3];  
  7.    float[] sum = new float[5];  
  8.    public static void main(String[] args) throws Exception{  
  9.        Prog50 stud = new Prog50();  
  10.        stud.input();  
  11.        stud.output();  
  12.     }  
  13.   //输入学号、姓名、成绩  
  14.    void input() throws IOException{  
  15.       BufferedReader br = new BufferedReader(newInputStreamReader(System.in));  
  16.        //录入状态标识  
  17.        boolean isRecord = true;  
  18.        while(isRecord){  
  19.            try{  
  20.               for(int i=0;i<5;i++){  
  21.                   System.out.print("请输入学号:");  
  22.                   number[i] = br.readLine();  
  23.                   System.out.print("请输入姓名:");  
  24.                   name[i] = br.readLine();  
  25.                   for(int j=0;j<3;j++){  
  26.                       System.out.print("请输入第"+(j+1)+"门课成绩:");  
  27.                       grade[i][j] =Integer.parseInt(br.readLine());  
  28.                   }  
  29.                   System.out.println();  
  30.                  sum[i] =grade[i][0]+grade[i][1]+grade[i][2];  
  31.               }  
  32.                 isRecord = false;  
  33.            }catch(NumberFormatException e){  
  34.                  System.out.println("请输入一个数字!");  
  35.          }  
  36.        }  
  37.     }  
  38.    //输出文件  
  39.   void output() throws IOException{  
  40.        FileWriter fw = new FileWriter("E://java50//stud.txt");  
  41.        BufferedWriter bw = new BufferedWriter(fw);  
  42.        bw.write("No. "+"Name "+"grade1 "+"grade2 "+"grade3 "+"average");  
  43.        bw.newLine();  
  44.        for(int i=0;i<5;i++){  
  45.          bw.write(number[i]);  
  46.          bw.write("  "+name[i]);  
  47.          for(int j=0;j<3;j++)  
  48.            bw.write(" "+grade[i][j]);  
  49.          bw.write(" "+(sum[i]/5));  
  50.          bw.newLine();  
  51.        }  
  52.       bw.close();  
  53.     }  
  54. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值