1题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
- import java.util.Scanner;
- public class Day22{
- public static void main(String[] args){
- System.out.print("请输入7个整数(1-50):");
- Scanner scan = new Scanner(System.in);
- int n1 = scan.nextInt();
- int n2 = scan.nextInt();
- int n3 = scan.nextInt();
- int n4 = scan.nextInt();
- int n5 = scan.nextInt();
- int n6 = scan.nextInt();
- int n7 = scan.nextInt();
- scan.close();
- printStar(n1);
- printStar(n2);
- printStar(n3);
- printStar(n4);
- printStar(n5);
- printStar(n6);
- printStar(n7);
- }
- static void printStar(int m){
- System.out.println(m);
- for(int i=0;i<m;i++)
- System.out.print("*");
- System.out.println();
- }
- }
2.题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
- public class Prog48{
- public static void main(String[] args){
- int n = 1234;
- int[] a = new int[4];
- for(int i=3;i>=0;i--){
- a[i] = n%10;
- n /= 10;
- }
- for(int i=0;i<4;i++)
- System.out.print(a[i]);
- System.out.println();
- for(int i=0;i<a.length;i++){
- a[i] += 5;
- a[i] %= 10;
- }
- int temp1 = a[0];
- a[0] = a[3];
- a[3] = temp1;
- int temp2 = a[1];
- a[1] = a[2];
- a[2] = temp2;
- for(int i=0;i<a.length;i++)
- System.out.print(a[i]);
- }
- }
3.题目:计算字符串中子串出现的次数
- public class Prog49{
- public static void main(String[] args){
- String str = "I come from County DingYuan Province AnHui.";
- char[] ch = str.toCharArray();
- int count = 0;
- for(int i=0;i<ch.length;i++){
- if(ch[i]==' ')
- count++;
- }
- count++;
- System.out.println("共有"+count+"个字串");
- }
- }
4.题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
- import java.io.*;
- public class Prog50{
- //定义学生模型
- String[] number = new String[5];
- String[] name = new String[5];
- float[][] grade = new float[5][3];
- float[] sum = new float[5];
- public static void main(String[] args) throws Exception{
- Prog50 stud = new Prog50();
- stud.input();
- stud.output();
- }
- //输入学号、姓名、成绩
- void input() throws IOException{
- BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
- //录入状态标识
- boolean isRecord = true;
- while(isRecord){
- try{
- for(int i=0;i<5;i++){
- System.out.print("请输入学号:");
- number[i] = br.readLine();
- System.out.print("请输入姓名:");
- name[i] = br.readLine();
- for(int j=0;j<3;j++){
- System.out.print("请输入第"+(j+1)+"门课成绩:");
- grade[i][j] =Integer.parseInt(br.readLine());
- }
- System.out.println();
- sum[i] =grade[i][0]+grade[i][1]+grade[i][2];
- }
- isRecord = false;
- }catch(NumberFormatException e){
- System.out.println("请输入一个数字!");
- }
- }
- }
- //输出文件
- void output() throws IOException{
- FileWriter fw = new FileWriter("E://java50//stud.txt");
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write("No. "+"Name "+"grade1 "+"grade2 "+"grade3 "+"average");
- bw.newLine();
- for(int i=0;i<5;i++){
- bw.write(number[i]);
- bw.write(" "+name[i]);
- for(int j=0;j<3;j++)
- bw.write(" "+grade[i][j]);
- bw.write(" "+(sum[i]/5));
- bw.newLine();
- }
- bw.close();
- }
- }