分享

找工作 50道编程题Java实现(32-50)

 印度阿三17 2019-02-11

 

程序32 左移右移

题目:取一个整数a从右端开始的4~7位。
分析:比如取123456789从右端开始的4~7位即:3456

 

 1 package test50;
 2 /*
 3  * 题目:取一个整数a从右端开始的4~7位。
 4  * 分析:比如取123456789从右端开始的4~7位即:6543
 5  */
 6 public class test32 {
 7 
 8     public static void main(String[] args) {
 9         long num=123456789;
10         quzhi(num);
11     }
12     
13     public static void quzhi(long number){
14         String s =Long.toString(number);//将Long型转化成字符串
15         char[] arr=s.toCharArray();//将字符串转化成数组
16         if(arr.length<7){
17             System.out.print("输入整数不满足条件");
18         }
19         for(int i=arr.length-4;i>=arr.length-7;i--){
20             System.out.print(arr[i] " ");
21         }
22     }
23 }

 

程序33 杨辉三角

题目:打印出杨辉三角形(要求打印出10行如下图)

 1 package test50;
 2 /*
 3  * %4d代表在4个空格内打印数字。我们选择4,因为我们知道10行杨辉三角形的最大数字的最大位数是3位数。
 4  * System.out.format("$s: %s%n", "fefe","fwefrr");
 5    "$s: %s%n"的解释:
 6    $s:表示输出24个空格,s为替换的字符串
 7    %s:表示字符串,即替换的内容
 8    %n:表示换行
 9  */
10 public class test33 {
11 
12     public static void main(String[] args) {
13         int rows = 10;
14         for(int i =0;i<rows;i  ) {
15             int number = 1;
16             //打印空格字符串
17             System.out.format("%" (rows-i)*2 "s","");
18             for(int j=0;j<=i;j  ) {//打印每一行得数字
19                 System.out.format("M",number);//打印当前数字
20                 number = number * (i - j) / (j   1);//计算下一位数字
21                 }
22             System.out.println();//换行
23             }
24         }
25     }

 

 

程序34 三个数排序

程序35 最大最小交换

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
分析: 例如输入6 4 8 3 9 7
交换后输出9 4 8 7 6 3

 1 package test50;
 2 
 3 import java.util.Scanner;
 4 
 5 /*
 6  * 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
 7  * 分析: 例如输入6 4 8 3 9 7
 8  * 交换后输出9 4 8 7 6 3
 9  */
10 public class test35 {
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         Scanner input=new Scanner(System.in);
14         System.out.println("请输入数组元素个数:");
15         int num=input.nextInt();
16         System.out.print("请输入数组:");
17         int[] arr=new int[num];
18         for(int i =0;i<num;i  ) {
19              arr[i]=input.nextInt();
20         }
21         input.close();
22         arrchange(arr);
23     }
24     
25     public static void arrchange(int[] arr){
26         int temp=0;
27         int max=arr[0]; int min=arr[0];
28         int maxIndex=0; int minIndex=0;
29         //找出原数组arr的最大值、最小值位置
30         for(int i =0;i<arr.length;i  ){
31             if(max<arr[i]){//找到数组的最大值索引
32                 max=arr[i];
33                 maxIndex=i;
34             }
35             if(min>arr[i]){//找到数组的最小值索引
36                 min=arr[i];
37                 minIndex=i;
38             }
39         }
40         if(arr[0]!=max){//如果最大值不是第一个
41             arr[maxIndex]=arr[0];
42             arr[0]=max;
43         }
44         if(arr[arr.length-1]!=min){//如果最小值不是最后一个
45             arr[minIndex]=arr[arr.length-1];
46             arr[arr.length-1]=min;
47         }
48         for(int k=0;k<arr.length;k  ){
49             System.out.print(arr[k] " ");
50         }
51     }
52     
53 }

 

程序36 移动位置

题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
分析:比如有4个数n=4;
1 2 3 4
各个数向后移动2位m=2,变为
    1 2 3 4
将最后m个数,即将最后2个数变成前面的2个数变为
3 4 1 2
 1 package test50;
 2 /*
 3  * 题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
 4  */
 5 import java.util.Scanner;
 6 public class test36 {
 7     public static void main(String[] args) {
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("请输入数组的长度:");//定义数组长度
10         int num = input.nextInt();
11         int [] arr = new int[num];
12         System.out.println("请输入数组元素:");//键入数组元素
13         for (int i = 0; i < num; i  ) {
14             arr[i] = input.nextInt();
15         }
16         System.out.println("您输入的数组是:");//打印数组
17         for (int j = 0; j < arr.length; j  ) {
18             System.out.print(arr[j]   " " );
19         }
20         System.out.println("请输入移动的位数:");//获取移动位数
21         int m = input.nextInt();
22         int [] arr2 = new int[num]; 
23         for (int k = 0; k < m; k  ) {//把向前移动的转移进新数组
24             arr2[k] = arr[num - m   k];
25         }
26         for (int k2 = 0; k2 < num - m; k2  ) {//把向后移的插入到新数组
27             arr2[m k2] = arr[k2];
28         }
29         System.out.println("移动后的数组为:");
30         for (int l = 0; l < arr2.length; l  ) {
31             System.out.println(arr2[l]);
32         }
33     }
34 }

 

 

程序37 报数*****

* 题目:有n个人围成一圈,顺序排号。
* 从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
* 分析:最后留下的是第n号那位

 1 package test50;
 2 /*
 3  * 题目:有n个人围成一圈,顺序排号。
 4  * 从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
 5  * 分析:最后留下的是第n号那位
 6  */
 7 
 8 import java.util.Scanner;
 9 public class test37 {
10     public static void main(String[] args) {
11         Scanner input = new Scanner(System.in);
12         System.out.println("请输入总人数:");//定义数组长度
13         int num = input.nextInt();
14         //定义数组,用其中的元素标记是否已经被淘汰,0表示为被淘汰
15         int [] arr = new int[num];
16         for (int i = 0; i < num; i  ) {//初始化数组元素都是1
17             arr[i] = 1;
18         }
19         for (int i = 0; i < arr.length; i  ) {
20             System.out.println(arr[i]);
21         }
22         int index = 0;
23         int sum = 0;
24         while(num > 1 )//用来控制剩余的人数
25         {
26             if (arr[index] == 1) {  //第index位置有人报数,如果arr[index]!= 1,表示index位置的人已经退出圈子
27                 sum  ;  //有人报数,sum就要加1
28                 if (sum == 3) {//如果是3,则重新记,从1开始
29                     sum = 0; 
30                     arr[index] = 0; 
31                     num-- ;//满足3个,总人数就减1
32                 }
33             }
34         index   ;//指向下一个位置
35         if (index == arr.length) {//如果索引是数组的长度,则从0开始,(实际上,索引数最大为arr.length-1)
36             index = 0 ;//实际上实现了循环遍历。如有人甲乙丙丁戊。当index=4时,下一次索引就指向0,让甲报数,而甲可能退出圈子,可能还在
37         }
38         }//while(num > 1 )结束
39         for (int i = 0; i < arr.length; i  ) {
40             System.out.println(arr[i]);
41         }
42         for (int i = 0; i < arr.length; i  ) {
43             if (arr[i] == 1) {
44                 System.out.println("第" (i 1) "留了下来");
45             }
46         }
47     }
48 }

 

 

 

程序38 求字符串长度

题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

 1 package test50;
 2 /*
 3  * 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
 4  */
 5 
 6 import java.util.Scanner;
 7 public class test38 {
 8     public static void main(String[] args) {
 9         Scanner input = new Scanner(System.in);
10         System.out.println("请输入一个字符串:");
11         String str = input.nextLine();
12         System.out.println("该字符串的长度是:" getArrLength(str));
13     }
14     public static int getArrLength(String str)
15     {
16         char[] charStr = str.toCharArray();
17         return charStr.length;
18     }
19 }

 

程序39 分数累加

题目:编写一个函数,输入n为偶数时,调用函数求1/2 1/4 ... 1/n,当输入n为奇数时,调用函数1/1 1/3 ... 1/n

递归如何实现

 1 package test50;
 2 
 3 import java.util.Scanner;
 4 
 5 /*
 6  * 编写一个函数,输入 n 为偶数时,调用函数求 1/2 1/4 ... 1/n,当输入 n 为奇数时,调用函数 1/1 1/3 ... 1/n
 7  */
 8 public class test39 {
 9 
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         System.out.println("请输入一个数字:");
14         int num = input.nextInt();
15         double sum=0.0;
16         System.out.println(getsum(num,sum));
17         }
18     
19     public static double getsum(int number,double sum){
20         while(number>0){
21             sum =(double)1/number;
22             number=number-2;
23             getsum(number,sum);
24         }
25         return sum;
26    }
27         
28 
29 }

 

程序40

题目:根据字符串内字符的ASCII码值对字符串数组进行排序。

 1 package test50;
 2 /*
 3  * 题目:根据字符串内字符的ASCII码值对字符串数组进行排序。
 4 分析:字符串用ASCII码比较大小,规则是:
 5 1、比较首字母的ASCII码大小
 6 2、若是前面的字母相同,则比较之后的字母的ASCII码值
 7 3、若是一个字符串从首字母开始包含另一个字符串,则认为字符串长度较长的大;例 :ab > a
 8 备注:Java中String类有一个compareTo方法,该方法返回一个int类型的数据。
 9 其比较规则是:拿出字符串的第一个字符与参数的第一个字符进行比较,
10 如果两者不等,比较结束,返回两者的ascii差,即字符串的第一个字符减去参数的第一个字符的ascii码值.
11 如果相等,则比较第二个字符,以此类推。比较到最后还是相等的,方法返回值为0。
12 比如下面的代码:“abc”.compareTo(“cad”)==-2
13 这里有一点需要注意:如果两个字符串的长度不同,并且一个字符串与另一个字符串的前面N个字符相等,那么这个方法返回返回两个字符串长度之差。
14 比如下面的代码:“fa”.compareTo(“f”)==1
15  */
16 
17 public class test40 {
18 
19     public static void main(String[] args) {
20         String[] str = {"abc","cad","m","fa","f"};
21         for(int i=str.length-1;i>=1;i--){
22             for(int j=0;j<=i-1;j  ){
23                 //注意compareTo函数在这里的作用
24                 if(str[j].compareTo(str[j 1])<0){
25                     String temp = str[j];
26                     str[j] = str[j 1];
27                     str[j 1] = temp;
28                 }
29             }
30         }
31         //此处使用了增强for循环遍历数组元素
32         for(String subStr:str)//定义了一个String类型的变量接收遍历后的数组元素
33             System.out.print(subStr " ");//将遍历后的数组元素依次输出
34     }
35 }

 

 

 

 

题目41:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。
* 第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,
* 第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

采用递归

 1 public class Prog41 {
 2     public static void main(String[] args) {
 3         //第一只猴子分桃时,调用函数fun(1)求得桃数
 4         System.out.println("最初海滩上共有" fun(1) "个桃子");
 5     }
 6     //递归函数
 7     public static int fun(int i) {
 8         if(i==5) {
 9             return 6;//轮到第五只猴子分桃时,至少得6个桃
10         }else {
11             return fun(i 1)*5 1;
12         }
13     }
14 }
15 /*运行结果
16 最初海滩上共有3906个桃子
17 */

 

冒泡

 1 package test50;
 2 /*
 3  * 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。
 4  * 第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,
 5  * 第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
 6  * 分析:求最少的桃子数,则第五个猴子分桃时,手上一共有6个桃
 7  * 利用递归法可求得最初的桃数
 8  */
 9 public class test41 {
10 
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         int sum5=6;//求最少的桃子数,则第五个猴子分桃时,手上一共有6个桃
14         System.out.print("海滩上原来最少有多少个桃子:" getsum(sum5));
15 
16     }
17     public static int getsum(int sum){
18         for(int i=4;i>=1;i--){
19             sum=sum*5 1;
20         }
21         return sum;
22     }
23 
24 }
View Code

 

题目42:809*??=800*?? 9*?? 1其中??代表两位数,若有这样得数,求??代表的两位数

 1 package test50;
 2 /*
 3  * 809*??=800*?? 9*?? 1其中??代表两位数,若有这样得数,求??代表的两位数
 4  */
 5 public class test42 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         boolean  flag=false;
10         int n=0;
11         for(int i=10;i<=99;i  ){
12             if(809*i==800*i 9*i 1){
13                 flag=true;
14                 n=i;
15                 break;
16                 }
17             }
18         if(flag){
19             System.out.print("有这样得数:" n);
20         }else 
21             System.out.print("不存在这样得数");
22 
23     }
24 
25 }
结果:不存在

 

题目43:求0—7所能组成的奇数个数,奇数中不包含重复数字。

 1 package test50;
 2 /*
 3  * 题目:求0—7所能组成的奇数个数,奇数中不包含重复数字。
 4  */
 5 public class test43 {
 6     public static void main(String[] args) {
 7         //0-7能组成的所有不重复的数的最大值是76543210
 8         //0-76543210中所有的奇数个数为76543210/2
 9         String[] s=new String[76543210/2];//定义一个字符串数组存储0-7能组成的所有奇数
10         int n=0;//定义一个变量为满足条件的奇数计数
11         int count=0;
12         //将所有奇数转换成字符串存入字符串数组s中
13         for(int i=1;i<=76543210;i  ) {
14             if((i 1)%2==0) {
15                 s[n]=String.valueOf(i);
16                 n  ;
17             }
18         }
19         //将所有的字符串数组中的元素取出分别存入一个字符数组c中
20         for(int j=0;j<n;j  ) {
21             char[] c= s[j].toCharArray();
22             outer:for(int k=0;k<c.length;k  ) {
23                 for(int m=1;m<c.length;m  ) {
24                     if(c[k]==c[m]) {//将字符数组中的每个元素取出,一旦发现重复值退出循环
25                         break outer;//一个break语句只能跳出一个for循环,因此这里使用 break outer,跳到28行
26                     }
27                 }
28                 count  ;//若该字符数组中不存在重复的元素,则该奇数为满足条件的奇数
29             }
30         }
31         System.out.println("0-7所能组成的不包含重复数字的奇数个数共有:" count "个");
32     }
33 }
34 /*运行结果
35 0-7所能组成的不包含重复数字的奇数个数共有:18567220个
36 */
View Code

 

题目44:一个偶数总能表示为两个素数之和。

 1 package test50;
 2 
 3 import java.util.Scanner;
 4 
 5 /*
 6  * 题目:一个偶数总能表示为两个素数之和。
 7  * 分析:一个偶数可能会有不止一对两个素数之和的情况
 8  * 例如:20=3 17 20=7 13
 9 
10  */
11 public class test44 {
12 
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         Scanner input=new Scanner(System.in);
16         System.out.print("请输入一个偶数num:");
17         int num=input.nextInt();
18         if(num%2!=0){
19             System.out.print("输入的数不是偶数");
20             return;
21         }
22         for(int i=2;i<num;i  ){
23             if(isPrimeNumber(i) && isPrimeNumber(num-i)){
24                 System.out.println(num "=" i " " (num-i));
25             }
26         }
27 
28     }
29     
30     public static boolean isPrimeNumber(int n){
31         if(n < 2) return false;; 
32         for(int i=2;i<=n/2;i  ){
33             if(n%i==0){
34                 return false;//不是素数
35             }
36         }
37         return true;
38     }
39 
40 }

 

 

题目45:判断一个素数能被几个9整除
分析:素数只能被1和其本身整除,不能被9整除,所以返回false

 1 import java.util.Scanner;
 2 public class Prog45{
 3     public static void main(String[] args){
 4         System.out.print("请输入一个素数:");
 5         Scanner scan = new Scanner(System.in);
 6         int n=scan.nextInt();
 7         if(!isPrime(n)) {
 8             System.out.println("你输入的不是素数,请重新输入");
 9             n=scan.nextInt();
10         }
11         scan.close();
12         System.out.println("素数" n "能被9整除吗?" zhengchu(n));
13     }
14     //判断素数是否能被9整除
15     private static boolean zhengchu(int n) {
16         return n%9==0;
17     }
18     //判断输入的数是否是素数
19     private static boolean isPrime(int n){
20         boolean flag = true;
21         for(int i=2;i<Math.sqrt(n) 1;i  ){
22             if(n%i==0){
23                 flag = false;
24                 break;
25             }
26         }
27         return flag;
28     }
29 }
30 /*运行结果
31 请输入一个素数:11
32 素数11能被9整除吗?false
33 */
View Code

 

程序46 字符串连接*

题目:编写一个两个字符串连接的程序

 1 package test50;
 2 
 3 import java.util.Scanner;
 4 
 5 /*
 6  * 题目:编写一个两个字符串连接的程序
 7  */
 8 public class test46 {
 9      public static void main(String[] args){
10          Scanner input=new Scanner(System.in);
11          System.out.print("输入2个字符串:");
12          String s1=input.nextLine();
13          String s2=input.nextLine();
14          input.close();
15          String s3=s1 s2;
16          System.out.print("连接后的字符串:" s3);
17         
18      }

 

程序47 输入数字打印星号

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

 

 1 package test50;
 2 
 3 import java.util.Scanner;
 4 
 5 /*
 6  * 题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
 7  */
 8 public class test47 {
 9     public static void main(String[] args){
10          System.out.print("请输入7个整数(1-50):");
11          Scanner scan = new Scanner(System.in);
12          int n1 = scan.nextInt();
13          int n2 = scan.nextInt();
14          int n3 = scan.nextInt();
15          int n4 = scan.nextInt();
16          int n5 = scan.nextInt();
17          int n6 = scan.nextInt();
18          int n7 = scan.nextInt();
19          scan.close();
20          printStar(n1);
21          printStar(n2);
22          printStar(n3);
23          printStar(n4);
24          printStar(n5);
25          printStar(n6);
26          printStar(n7);
27     }
28     static void printStar(int m){
29         System.out.println(m);
30         for(int i=0;i<m;i  )
31             System.out.print("*");
32         System.out.println();
33     }
34 
35 
36 }
View Code
 1 package test50;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * @author VellBibi
 7  *【程序47】 TestPrint.java
 8  *题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
 9  */
10 public class TestPrint {
11 
12     public static void main(String[] args) {
13         Scanner s = new Scanner(System.in);
14         int[] a = new int[7];
15         for(int i=0; i<7; i  ){
16             System.out.print("输入第" (i 1) "个整数:");
17             a[i] = s.nextInt();
18         }
19         
20         for(int i=0; i<7; i  ){
21             for(int j=0; j<a[i]; j  ){
22                 System.out.print("*");
23             }
24             System.out.println();
25         }
26     }
27 
28 }
View Code

 

程序48 数字加密

题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,
加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
分析:例如原始数据是: 6 7 8 9(原始数据)
每个数加上5后为:11 12 13 14
除以10后为: 1 2 3 4
交换位置之后 4 3 2 1(加密后的数据)

 1 package test50;
 2 
 3 import java.util.Scanner;
 4 
 5 /*
 6  * 题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,
 7  * 加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
 8  * 分析:例如原始数据是: 6 7 8 9(原始数据)
 9  * 每个数加上5后为:11 12 13 14
10  * 除以10后为: 1 2 3 4
11  * 交换位置之后 4 3 2 1(加密后的数据)
12  */
13 public class test48 {
14 
15     public static void main(String[] args) {
16         // TODO Auto-generated method stub
17         int temp=0;
18          System.out.println("请输入0到9间的4个整数:");
19          Scanner scan = new Scanner(System.in);
20          int[] arr=new int[4];
21          for(int i=0;i<=3;i  ){
22              arr[i]=scan.nextInt();
23          }
24          scan.close();
25          for(int i=0;i<=3;i  ){
26              arr[i]=(arr[i] 5);
27          }
28          //将第一位和第四位交换
29          temp=arr[0];
30          arr[0]=arr[3];
31          arr[3]=temp;
32          //第二位和第三位交换
33          temp=arr[2];
34          arr[2]=arr[1];
35          arr[1]=temp;
36          
37          for(int i=0;i<=3;i  ){
38              System.out.print(arr[i] " ");
39          }
40     }
41 
42 }
View Code

 

 

 

程序49 子串出现的个数

题目:计算首末不含空格各个子串之间只含一个空格的字符串中子串出现的次数
分析:例如输入的字符串为"I come from County DingYuan Province AnHui."
空格隔断的即为字符子串,所以上述字符串的子串个数有7个

 

 1 package test50;
 2 /*
 3  * 题目:计算首末不含空格各个子串之间只含一个空格的字符串中子串出现的次数
 4  * 分析:例如输入的字符串为"I come from County DingYuan Province AnHui."
 5  * 空格隔断的即为字符子串,所以上述字符串的子串个数有7个
 6  */
 7 public class test49{
 8     public static void main(String[] args){
 9         String str="I come from County DingYuan Province AnHui.";
10         int count=0;
11         char[] ch=str.toCharArray();//将字符串转换成字符数组
12         for(int i=0;i<ch.length;i  ) {
13             if(ch[i]==' ')//计算字符串中的空格个数
14                 count  ;
15         }
16         count  ;//字符串中子串的个数=空格数 1
17         System.out.println("共有" count "个字符子串");
18     }
19 }
20 /*运行结果
21 共有7个字符子串
22 */

思路:转换成数组,利用

 if(ch[i]==' ')

计算空格数

 

程序50 文件IO

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

 1 package test50;
 2 
 3 import java.io.*;
 4 public class test50{
 5     //定义学生模型
 6     String[] number = new String[5];
 7     String[] name = new String[5];
 8     float[][] grade = new float[5][3];
 9     float[] sum = new float[5];
10     public static void main(String[] args) throws Exception{
11         test50 stud = new test50();
12         stud.input();
13         stud.output();
14     }
15     //输入学号、姓名、成绩
16     void input() throws IOException{
17         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18         //录入状态标识
19         boolean isRecord = true;
20         while(isRecord){
21             try{
22                 for(int i=0;i<5;i  ){
23                     System.out.print("请输入学号:");
24                     number[i] = br.readLine();
25                     System.out.print("请输入姓名:");
26                     name[i] = br.readLine();
27                     for(int j=0;j<3;j  ){
28                         System.out.print("请输入第" (j 1) "门课成绩:");
29                         grade[i][j] = Integer.parseInt(br.readLine());
30                     }
31                     System.out.println();
32                     sum[i] = grade[i][0] grade[i][1] grade[i][2];
33                 }
34                 isRecord = false;
35             }catch(NumberFormatException e){
36                 System.out.println("请输入一个数字!");
37             }
38         }
39     }
40     //输出文件
41     void output() throws IOException{
42         FileWriter fw = new FileWriter("D:\\JavaEE\\Code\\Java基础50道经典练习题\\src//stud.txt");
43         BufferedWriter bw = new BufferedWriter(fw);
44         bw.write("No.  " "Name  " "grade1  " "grade2  " "grade3  " "average");
45         bw.newLine();
46         for(int i=0;i<5;i  ){
47             bw.write(number[i]);
48             bw.write("  " name[i]);
49             for(int j=0;j<3;j  )
50                 bw.write("  " grade[i][j]);
51             bw.write("  " (sum[i]/5));
52             bw.newLine();
53         }
54         bw.close();
55     }
56 }

 

来源:http://www./content-1-112551.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多