6、50道JAVA基础编程练习题跟答案

   1 50道JAVA基础编程练习题
   2 【程序1】
   3 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
   4 程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
   5 public class Prog1{
   6 public static void main(String[] args){
   7   int n = 10;
   8   System.out.println("第"+n+"个月兔子总数为"+fun(n));
   9 }
  10 private static int fun(int n){
  11   if(n==1 || n==2)
  12      return 1;
  13   else
  14      return fun(n-1)+fun(n-2);
  15 }
  16 }
  17 【程序2】
  18 题目:判断101-200之间有多少个素数,并输出所有素数。
  19 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
  20 public class Prog2{
  21 public static void main(String[] args){
  22   int m = 1;
  23   int n = 1000;
  24   int count = 0;
  25   //统计素数个数
  26   for(int i=m;i<n;i++){
  27    if(isPrime(i)){
  28     count++;
  29     System.out.print(i+" ");
  30     if(count%10==0){
  31      System.out.println();
  32     }
  33    }
  34   }
  35   System.out.println();
  36   System.out.println("在"+m+"和"+n+"之间共有"+count+"个素数");
  37 }
  38 //判断素数
  39 private static boolean isPrime(int n){
  40   boolean flag = true;
  41   if(n==1)
  42     flag = false;
  43   else{
  44    for(int i=2;i<=Math.sqrt(n);i++){
  45    if((n%i)==0 || n==1){
  46     flag = false;
  47     break;
  48    }
  49     else
  50       flag = true;
  51     }
  52   }
  53   return flag;
  54 }
  55 }
  56 【程序3】
  57 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
  58 程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
  59 public class Prog3{
  60 public static void main(String[] args){
  61   for(int i=100;i<1000;i++){
  62    if(isLotus(i))
  63       System.out.print(i+" ");
  64   }
  65   System.out.println();
  66 }
  67 //判断水仙花数
  68 private static boolean isLotus(int lotus){
  69   int m = 0;
  70   int n = lotus;
  71   int sum = 0;
  72   m = n/100;
  73   n  -= m*100;
  74   sum = m*m*m;
  75   m = n/10;
  76   n -= m*10;
  77   sum += m*m*m + n*n*n;
  78   if(sum==lotus)
  79    return true;
  80   else
  81    return false;
  82   }
  83 }
  84 【程序4】
  85 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5  86 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
  87 (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
  88 (2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n,重复执行第一步。
  89 (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
  90 public class Prog4{
  91 public static void main(String[] args){
  92   int n = 13;
  93   decompose(n);
  94 }
  95 private static void decompose(int n){
  96   System.out.print(n+"=");
  97   for(int i=2;i<n+1;i++){
  98    while(n%i==0 && n!=i){
  99     n/=i;
 100     System.out.print(i+"*");
 101    }
 102    if(n==i){
 103     System.out.println(i);
 104     break;
 105    }
 106   }
 107 }
 108 }
 109 【程序5】
 110 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
 111 程序分析:(a>b)?a:b这是条件运算符的基本例子。
 112 public class Prog5{
 113 public static void main(String[] args){
 114   int n = -1;
 115   try{
 116    n = Integer.parseInt(args[0]);
 117   }catch(ArrayIndexOutOfBoundsException e){
 118    System.out.println("请输入成绩");
 119    return;
 120   }
 121   grade(n);
 122 }
 123 //成绩等级计算
 124 private static void grade(int n){
 125   if(n>100 || n<0)
 126     System.out.println("输入无效");
 127   else{
 128     String str = (n>=90)?"分,属于A等":((n>60)?"分,属于B等":"分,属于C等");
 129     System.out.println(n+str);
 130   }
 131 }
 132 }
 133 【程序6】
 134 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
 135 程序分析:利用辗除法。
 136 public class Prog6{
 137 public static void main(String[] args){
 138   int m,n;
 139   try{
 140    m = Integer.parseInt(args[0]);
 141    n = Integer.parseInt(args[1]);
 142   }catch(ArrayIndexOutOfBoundsException e){
 143    System.out.println("输入有误");
 144    return;
 145   }
 146   max_min(m,n);
 147 }
 148 //求最大公约数和最小公倍数
 149 private static void max_min(int m, int n){
 150   int temp = 1;
 151   int yshu = 1;
 152   int bshu = m*n;
 153   if(n<m){
 154    temp = n;
 155    n = m;
 156    m = temp;
 157   }
 158   while(m!=0){
 159    temp = n%m;
 160    n = m;
 161    m = temp;
 162   }
 163   yshu = n;
 164   bshu /= n;
 165   System.out.println(m+"和"+n+"的最大公约数为"+yshu);
 166   System.out.println(m+"和"+n+"的最小公倍数为"+bshu);
 167 }
 168 }
 169 【程序7】
 170 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
 171 程序分析:利用while语句,条件为输入的字符不为'\n'.
 172 import java.util.Scanner;
 173 public class Prog7_1{
 174 public static void main(String[] args){
 175   System.out.print("请输入一串字符:");
 176   Scanner scan = new Scanner(System.in);
 177   String str = scan.nextLine();//将一行字符转化为字符串
 178   scan.close();
 179   count(str);
 180 }
 181 //统计输入的字符数
 182 private static void count(String str){
 183   String E1 = "[\u4e00-\u9fa5]";//汉字
 184   String E2 = "[a-zA-Z]";
 185   String E3 = "[0-9]";
 186   String E4 = "\\s";//空格
 187   int countChinese = 0;
 188   int countLetter = 0;
 189   int countNumber = 0;
 190   int countSpace = 0;
 191   int countOther = 0;
 192   char[] array_Char = str.toCharArray();//将字符串转化为字符数组
 193   String[] array_String = new String[array_Char.length];//汉字只能作为字符串处理
 194   for(int i=0;i<array_Char.length;i++)
 195     array_String[i] = String.valueOf(array_Char[i]);
 196   //遍历字符串数组中的元素
 197   for(String s:array_String){
 198    if(s.matches(E1))
 199      countChinese++;
 200    else if(s.matches(E2))
 201      countLetter++;
 202    else if(s.matches(E3))
 203      countNumber++;
 204    else if(s.matches(E4))
 205      countSpace++;
 206    else
 207      countOther++;
 208   }
 209   System.out.println("输入的汉字个数:"+countChinese);
 210   System.out.println("输入的字母个数:"+countLetter);
 211   System.out.println("输入的数字个数:"+countNumber);
 212   System.out.println("输入的空格个数:"+countSpace);
 213   System.out.println("输入的其它字符个数:"+countSpace);
 214 }
 215 }
 216 import java.util.*;
 217 public class Prog7_2{
 218 public static void main(String[] args){
 219    System.out.println("请输入一行字符:");
 220    Scanner scan = new Scanner(System.in);
 221    String str = scan.nextLine();
 222    scan.close();
 223    count(str);
 224 }
 225 //统计输入的字符
 226 private static void count(String str){
 227   List<String> list = new ArrayList<String>();
 228   char[] array_Char = str.toCharArray();
 229   for(char c:array_Char)
 230     list.add(String.valueOf(c));//将字符作为字符串添加到list表中
 231   Collections.sort(list);//排序
 232   for(String s:list){
 233    int begin = list.indexOf(s);
 234    int end = list.lastIndexOf(s);
 235    //索引结束统计字符数
 236    if(list.get(end)==s)
 237      System.out.println("字符‘"+s+"’有"+(end-begin+1)+"个");
 238   }
 239 }
 240 }
 241 【程序8】
 242 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
 243 程序分析:关键是计算出每一项的值。
 244 import java.util.Scanner;
 245 
 246 public class Prog8{
 247 public static void main(String[] args){
 248   System.out.print("求s=a+aa+aaa+aaaa+...的值,请输入a的值:");
 249   Scanner scan = new Scanner(System.in).useDelimiter("\\s*");//以空格作为分隔符
 250   int a = scan.nextInt();
 251   int n = scan.nextInt();
 252   scan.close();//关闭扫描器
 253   System.out.println(expressed(2,5)+add(2,5));
 254 }
 255 //求和表达式
 256 private static String expressed(int a,int n){
 257   StringBuffer sb = new StringBuffer();
 258   StringBuffer subSB = new StringBuffer();
 259   for(int i=1;i<n+1;i++){
 260     subSB = subSB.append(a);
 261     sb = sb.append(subSB);
 262     if(i<n)
 263       sb = sb.append("+");
 264   }
 265   sb.append("=");
 266   return sb.toString();
 267 }
 268 //求和
 269 private static long add(int a,int n){
 270   long sum = 0;
 271   long subSUM = 0;
 272   for(int i=1;i<n+1;i++){
 273    subSUM = subSUM*10+a;
 274    sum = sum+subSUM;
 275   }
 276   return sum;
 277 }
 278 }
 279 【程序9】
 280 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
 281 public class Prog9{
 282 public static void main(String[] args){
 283   int n = 10000;
 284   compNumber(n);
 285 }
 286 //求完数
 287 private static void compNumber(int n){
 288   int count = 0;
 289   System.out.println(n+"以内的完数:");
 290   for(int i=1;i<n+1;i++){
 291    int sum = 0;
 292    for(int j=1;j<i/2+1;j++){
 293     if((i%j)==0){
 294      sum += j;
 295      if(sum==i){
 296       System.out.print(i+" ");
 297       if((count++)%5==0)
 298         System.out.println();
 299        }
 300     }
 301    }
 302   }
 303 }
 304 }
 305 【程序10】
 306 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
 307 import java.util.Scanner;
 308 public class Prog10{
 309 public static void main(String[] args){
 310   System.out.print("请输入小球落地时的高度和求解的次数:");
 311   Scanner scan = new Scanner(System.in).useDelimiter("\\s");
 312   int h = scan.nextInt();
 313   int n = scan.nextInt();
 314   scan.close();
 315   distance(h,n);
 316 }
 317 //小球从h高度落下,经n次反弹后经过的距离和反弹的高度
 318 private static void distance(int h,int n){
 319   double length = 0;
 320   for(int i=0;i<n;i++){
 321    length += h;
 322    h /=2.0 ;
 323   }
 324   System.out.println("经过第"+n+"次反弹后,小球共经过"+length+"米,"+"第"+n+"次反弹高度为"+h+"米");
 325 }
 326 }
 327 【程序11】
 328 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
 329 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
 330 public class Prog11{
 331 public static void main(String[] args){
 332   int count = 0;
 333   int n = 0;
 334   for(int i=1;i<5;i++){
 335    for(int j=1;j<5;j++){
 336     if(j==i)
 337       continue;
 338     for(int k=1;k<5;k++){
 339      if(k!=i && k!=j){
 340       n = i*100+j*10+k;
 341        System.out.print(n+" ");
 342        if((++count)%5==0)
 343        System.out.println();
 344      }
 345     }
 346    }
 347   }
 348   System.out.println();
 349   System.out.println("符合条件的数共:"+count+"个");
 350 }
 351 }
 352 【程序12】
 353 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
 354 程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
 355 import java.io.*;
 356 public class Prog12{
 357 public static void main(String[] args){
 358   System.out.print("请输入当前利润:");
 359   long profit = Long.parseLong(key_Input());
 360   System.out.println("应发奖金:"+bonus(profit));
 361 }
 362 //接受从键盘输入的内容
 363 private static String key_Input(){
 364   String str = null;
 365   BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));
 366   try{
 367    str = bufIn.readLine();
 368   }catch(IOException e){
 369    e.printStackTrace();
 370   }finally{
 371    try{
 372     bufIn.close();
 373    }catch(IOException e){
 374     e.printStackTrace();
 375    }
 376   }
 377   return str;
 378 }
 379 //计算奖金
 380 private static long bonus(long profit){
 381   long prize = 0;
 382   long profit_sub = profit;
 383   if(profit>1000000){
 384    profit = profit_sub-1000000;
 385    profit_sub = 1000000;
 386    prize += profit*0.01;
 387   }
 388   if(profit>600000){
 389    profit = profit_sub-600000;
 390    profit_sub = 600000;
 391    prize += profit*0.015;
 392   }
 393   if(profit>400000){
 394    profit = profit_sub-400000;
 395    profit_sub = 400000;
 396    prize += profit*0.03;
 397   }
 398   if(profit>200000){
 399    profit = profit_sub-200000;
 400    profit_sub = 200000;
 401    prize += prize*0.05;
 402   }
 403   if(profit>100000){
 404    profit = profit_sub-100000;
 405    profit_sub = 100000;
 406    prize += profit*0.075;
 407   }
 408   prize += profit_sub*0.1;
 409   return prize;
 410 }
 411 }
 412 
 413 【程序13】
 414 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
 415 程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
 416 public class Prog13{
 417 public static void main(String[] args){
 418   int n=0;
 419   for(int i=0;i<100001;i++){
 420    if(isCompSqrt(i+100) && isCompSqrt(i+268)){
 421     n = i;
 422     break;
 423    }
 424   }
 425   System.out.println("所求的数是:"+n);
 426 }
 427 //判断完全平方数
 428 private static boolean isCompSqrt(int n){
 429   boolean isComp = false;
 430   for(int i=1;i<Math.sqrt(n)+1;i++){
 431    if(n==Math.pow(i,2)){
 432     isComp = true;
 433     break;
 434    }
 435   }
 436   return isComp;
 437 }
 438 }
 439 【程序14】
 440 题目:输入某年某月某日,判断这一天是这一年的第几天?
 441 程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
 442 import java.util.Scanner;
 443 public class Prog14{
 444 public static void main(String[] args){
 445   Scanner scan = new Scanner(System.in).useDelimiter("\\D");//匹配非数字
 446   System.out.print("请输入当前日期(年-月-日):");
 447   int year = scan.nextInt();
 448   int month = scan.nextInt();
 449   int date = scan.nextInt();
 450   scan.close();
 451   System.out.println("今天是"+year+"年的第"+analysis(year,month,date)+"天");
 452 }
 453 //判断天数
 454 private static int analysis(int year, int month, int date){
 455   int n = 0;
 456   int[] month_date = new int[] {0,31,28,31,30,31,30,31,31,30,31,30};
 457   if((year%400)==0 || ((year%4)==0)&&((year%100)!=0))
 458     month_date[2] = 29;
 459   for(int i=0;i<month;i++)
 460     n += month_date[i];
 461   return n+date;
 462 }
 463 }
 464 【程序15】
 465 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
 466 程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
 467 import java.util.Scanner;
 468 public class Prog15{
 469 public static void main(String[] args){
 470   Scanner scan = new Scanner(System.in).useDelimiter("\\D");
 471   System.out.print("请输入三个数:");
 472   int x = scan.nextInt();
 473   int y = scan.nextInt();
 474   int z = scan.nextInt();
 475   scan.close();
 476   System.out.println("排序结果:"+sort(x,y,z));
 477 }
 478 //比较两个数的大小
 479 private static String sort(int x,int y,int z){
 480   String s = null;
 481   if(x>y){
 482    int t = x;
 483    x = y;
 484    y = t;
 485   }
 486   if(x>z){
 487    int t = x;
 488    x = z;
 489    z = t;
 490   }
 491   if(y>z){
 492    int t = z;
 493    z = y;
 494    y = t;
 495   }
 496   s = x+" "+y+" "+z;
 497   return s;
 498 }
 499 }
 500 【程序16】
 501 题目:输出9*9口诀。
 502 程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
 503 public class Prog16{
 504 public static void main(String[] args){
 505   for(int i=1;i<10;i++){
 506    for(int j=1;j<i+1;j++)
 507     System.out.print(j+"*"+i+"="+(j*i)+" ");
 508    System.out.println();
 509   }
 510 }
 511 }
 512 【程序17】
 513 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
 514 程序分析:采取逆向思维的方法,从后往前推断。
 515 public class Prog17{
 516 public static void main(String[] args){
 517   int m = 1;
 518    for(int i=10;i>0;i--)
 519      m = 2*m + 2;
 520    System.out.println("小猴子共摘了"+m+"桃子");
 521 }
 522 }
 523 【程序18】
 524 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
 525 import java.util.ArrayList;
 526 public class Prog18{
 527 String a,b,c;//甲队成员
 528 public static void main(String[] args){
 529   String[] racer = {"x","y","z"};//乙队成员
 530   ArrayList<Prog18> arrayList = new ArrayList<Prog18>();
 531   for(int i=0;i<3;i++)
 532     for(int j=0;j<3;j++)
 533       for(int k=0;k<3;k++){
 534        Prog18 prog18 = new Prog18(racer[i],racer[j],racer[k]);
 535        if(!prog18.a.equals(prog18.b) && !prog18.a.equals(prog18.c) && !prog18.b.equals(prog18.c) &&
 536           !prog18.a.equals("x") && !prog18.c.equals("x") && !prog18.c.equals("z"))
 537           arrayList.add(prog18);
 538       }
 539     for(Object obj:arrayList)
 540       System.out.println(obj);
 541 }
 542 //构造方法
 543 private Prog18(String a,String b,String c){
 544   this.a = a;
 545   this.b = b ;
 546   this.c = c;
 547 }
 548 public String toString(){
 549   return "a的对手是"+a+"  "+"b的对手是"+b+"  "+"c的对手是"+c;
 550 }
 551 }
 552 【程序19】
 553 题目:打印出如下图案(菱形)
 554     *
 555    ***
 556 ******
 557 ********
 558 ******
 559   ***
 560    *
 561 程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。
 562 public class Prog19{
 563 public static void main(String[] args){
 564   int n = 5;
 565   printStar(n);
 566 }
 567 //打印星星
 568 private static void printStar(int n){
 569   //打印上半部分
 570   for(int i=0;i<n;i++){
 571    for(int j=0;j<2*n;j++){
 572      if(j<n-i)
 573        System.out.print(" ");
 574      if(j>=n-i && j<=n+i)
 575        System.out.print("*");
 576     }
 577     System.out.println();
 578   }
 579   //打印下半部分
 580   for(int i=1;i<n;i++){
 581    System.out.print(" ");
 582    for(int j=0;j<2*n-i;j++){
 583     if(j<i)
 584        System.out.print(" ");
 585      if(j>=i && j<2*n-i-1)
 586        System.out.print("*");
 587    }
 588    System.out.println();
 589   }
 590 }
 591 }
 592 【程序20】
 593 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
 594 程序分析:请抓住分子与分母的变化规律。
 595 public class Prog20{
 596 public static void main(String[] args){
 597   double n1 = 1;
 598   double n2 = 1;
 599   double fraction = n1/n2;
 600   double Sn = 0;
 601   for(int i=0;i<20;i++){
 602     double t1 = n1;
 603     double t2 = n2;
 604     n1 = t1+t2;
 605     n2 = t1;
 606     fraction = n1/n2;
 607     Sn += fraction;
 608   }
 609   System.out.print(Sn);
 610 }
 611 }
 612 【程序21】
 613 题目:求1+2!+3!+...+20!的和
 614 程序分析:此程序只是把累加变成了累乘。
 615 public class Prog21{
 616 public static void main(String[] args){
 617   long sum = 0;
 618   for(int i=0;i<20;i++)
 619     sum += factorial(i+1);
 620   System.out.println(sum);
 621 }
 622 //阶乘
 623 private static long factorial(int n){
 624   int mult = 1;
 625   for(int i=1;i<n+1;i++)
 626     mult *= i;
 627   return mult;
 628 }
 629 }
 630 【程序22】
 631 题目:利用递归方法求5! 632 程序分析:递归公式:fn=fn_1*4!
 633 public class Prog22{
 634 public static void main(String[] args){
 635   System.out.println(fact(10));
 636 }
 637 //递归求阶乘
 638 private static long fact(int n){
 639   if(n==1)
 640     return 1;
 641   else
 642     return fact(n-1)*n;
 643 }
 644 }
 645 【程序23】
 646 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
 647 程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。
 648 public class Prog23{
 649 public static void main(String[] args){
 650   System.out.println(getAge(5,2));
 651 }
 652 //求第m位同志的年龄
 653 private static int getAge(int m,int n){
 654   if(m==1)
 655     return 10;
 656   else
 657     return getAge(m-1,n)+n; 
 658 }
 659 }
 660 【程序24】
 661 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
 662 public class Prog24{
 663 public static void main(String[] args){
 664   int n = Integer.parseInt(args[0]);
 665   int i = 0;
 666   int[] a = new int[5];
 667   do{
 668    a[i] = n%10;
 669     n /= 10;
 670     ++i;
 671   }while(n!=0);
 672   System.out.print("这是一个"+i+"位数,从个位起,各位数字依次为:");
 673   for(int j=0;j<i;j++)
 674     System.out.print(a[j]+" ");
 675 }
 676 }
 677 【程序25】
 678 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
 679 import java.io.*;
 680 public class Prog25{
 681 public static void main(String[] args){
 682   int n = 0;
 683   System.out.print("请输入一个5位数:");
 684   BufferedReader bufin = new BufferedReader(new InputStreamReader(System.in));
 685   try{
 686     n = Integer.parseInt(bufin.readLine());
 687   }catch(IOException e){
 688    e.printStackTrace();
 689   }finally{
 690    try{
 691      bufin.close();
 692    }catch(IOException e){
 693     e.printStackTrace();
 694    }
 695   }
 696   palin(n);
 697 }
 698 private static void palin(int n){
 699   int m = n;
 700   int[] a = new int[5];
 701   if(n<10000 || n>99999){
 702    System.out.println("输入的不是5位数!");
 703    return;
 704   }else{
 705     for(int i=0;i<5;i++){
 706      a[i] = n%10;
 707      n /= 10;
 708     }
 709     if(a[0]==a[4] && a[1]==a[3])
 710       System.out.println(m+"是一个回文数");
 711     else
 712       System.out.println(m+"不是回文数");
 713      }
 714    }
 715 }
 716 【程序26】
 717 题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
 718 程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
 719 import java.io.*;
 720 public class Prog26{
 721 public static void main(String[] args){
 722   String str = new String();
 723    BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));
 724    System.out.print("请输入星期的英文单词前两至四个字母):");
 725    try{
 726     str = bufIn.readLine();
 727    }catch(IOException e){
 728     e.printStackTrace();
 729    }finally{
 730     try{
 731      bufIn.close();
 732     }catch(IOException e){
 733      e.printStackTrace();
 734     }
 735    }
 736    week(str);
 737 }
 738 private static void week(String str){
 739   int n = -1;
 740   if(str.trim().equalsIgnoreCase("Mo") || str.trim().equalsIgnoreCase("Mon") || str.trim().equalsIgnoreCase("Mond"))
 741     n = 1;
 742   if(str.trim().equalsIgnoreCase("Tu") || str.trim().equalsIgnoreCase("Tue") || str.trim().equalsIgnoreCase("Tues"))
 743     n = 2;
 744   if(str.trim().equalsIgnoreCase("We") || str.trim().equalsIgnoreCase("Wed") || str.trim().equalsIgnoreCase("Wedn"))
 745     n = 3;
 746   if(str.trim().equalsIgnoreCase("Th") || str.trim().equalsIgnoreCase("Thu") || str.trim().equalsIgnoreCase("Thur"))
 747     n = 4;
 748   if(str.trim().equalsIgnoreCase("Fr") || str.trim().equalsIgnoreCase("Fri") || str.trim().equalsIgnoreCase("Frid"))
 749     n = 5;
 750   if(str.trim().equalsIgnoreCase("Sa") || str.trim().equalsIgnoreCase("Sat") || str.trim().equalsIgnoreCase("Satu"))
 751     n = 2;
 752   if(str.trim().equalsIgnoreCase("Su") || str.trim().equalsIgnoreCase("Sun") || str.trim().equalsIgnoreCase("Sund"))
 753     n = 0;
 754   switch(n){
 755    case 1:
 756      System.out.println("星期一");
 757      break;
 758    case 2:
 759      System.out.println("星期二");
 760      break;
 761    case 3:
 762      System.out.println("星期三");
 763      break;
 764    case 4:
 765      System.out.println("星期四");
 766      break;
 767    case 5:
 768      System.out.println("星期五");
 769      break;
 770    case 6:
 771      System.out.println("星期六");
 772      break;
 773    case 0:
 774      System.out.println("星期日");
 775      break;
 776    default:
 777      System.out.println("输入有误!");
 778      break;
 779   }
 780 }
 781 }
 782 【程序27】
 783 题目:求100之内的素数
 784 public class Prog27{
 785 public static void main(String[] args){
 786   int n = 100;
 787   System.out.print(n+"以内的素数:");
 788   for(int i=2;i<n+1;i++){
 789    if(isPrime(i))
 790      System.out.print(i+" ");
 791   }
 792 }
 793 //求素数
 794 private static boolean isPrime(int n){
 795   boolean flag = true;
 796   for(int i=2;i<Math.sqrt(n)+1;i++)
 797    if(n%i==0){
 798      flag = false;
 799      break;
 800    }
 801   return flag;
 802 }
 803 }
 804 【程序28】
 805 题目:对10个数进行排序
 806 程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第二个元素与后8个进行比较,并进行交换。
 807 public class Prog28{
 808 public static void main(String[] args){
 809   int[] a = new int[]{31,42,21,50,12,60,81,74,101,93};
 810   for(int i=0;i<10;i++)
 811    for(int j=0;j<a.length-i-1;j++)
 812     if(a[j]>a[j+1]){
 813      int temp = a[j];
 814      a[j] = a[j+1];
 815      a[j+1] = temp;
 816     }
 817   for(int i=0;i<a.length;i++)
 818     System.out.print(a[i]+" ");
 819 }
 820 }
 821 【程序29】
 822 题目:求一个3*3矩阵对角线元素之和
 823 程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
 824 public class Prog29{
 825 public static void main(String[] args){
 826   int[][] a = new int[][] {{100,2,3,},{4,5,6},{17,8,9}};
 827   matrSum(a);
 828 }
 829 private static void matrSum(int[][] a){
 830   int sum1 = 0;
 831   int sum2 = 0;
 832   for(int i=0;i<a.length;i++)
 833     for(int j=0;j<a[i].length;j++){
 834      if(i==j) sum1 += a[i][j];
 835      if(j==a.length-i-1) sum2 += a[i][j];
 836     }
 837   System.out.println("矩阵对角线之和分别是:"+sum1+"和"+sum2);
 838 }
 839 }
 840 【程序30】
 841 题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
 842 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
 843 import java.util.Scanner;
 844 public class Prog30{
 845 public static void main(String[] args){
 846   int[] A = new int[]{0,8,7,5,9,1,2,4,3,12};
 847   int[] B = sort(A);
 848   print(B);
 849   System.out.println();
 850         System.out.print("请输入10个数的数组:");
 851   Scanner scan = new Scanner(System.in); 
 852 int a = scan.nextInt();
 853 scan.close();
 854   int[] C = insert(a,B);
 855   print(C);
 856 }
 857 //选择排序
 858 private static int[] sort(int[] A){
 859   int[] B = new int[A.length];
 860   for(int i=0;i<A.length-1;i++){
 861    int min = A[i];
 862    for(int j=i+1;j<A.length;j++){
 863     if(min>A[j]){
 864      int temp = min;
 865      min = A[j];
 866      A[j] = temp;
 867     }
 868     B[i] = min;
 869    }
 870   }
 871   B[A.length-1] = A[A.length-1];
 872   return B;
 873 }
 874 //打印
 875 private static void print(int[] A){
 876   for(int i=0;i<A.length;i++)
 877     System.out.print(A[i]+" ");
 878 }
 879 //插入数字
 880 private static int[] insert(int a,int[] A){
 881   int[] B = new int[A.length+1];
 882   for(int i=A.length-1;i>0;i--)
 883     if(a>A[i]){
 884      B[i+1] = a;
 885       for(int j=0;j<=i;j++)
 886         B[j] = A[j];
 887         for(int k=i+2;k<B.length;k++)
 888           B[k] = A[k-1];
 889         break;
 890     }
 891   return B;
 892 }
 893 }
 894 【程序31】
 895 题目:将一个数组逆序输出。
 896 程序分析:用第一个与最后一个交换。
 897 public class Prog31{
 898 public static void main(String[] args){
 899   int[] A = new int[]{1,2,3,4,5,6,7,8,9,};
 900   print(A);
 901   System.out.println();
 902   int[] B = reverse(A);
 903   print(B);
 904 }
 905 private static int[] reverse(int[] A){
 906   for(int i=0;i<A.length/2;i++){
 907    int temp = A[A.length-i-1];
 908    A[A.length-i-1] = A[i];
 909    A[i] = temp;
 910   }
 911   return A;
 912 }
 913 private static void print(int[] A){
 914   for(int i=0;i<A.length;i++)
 915     System.out.print(A[i]+" ");
 916 }
 917 }
 918 【程序32】
 919 题目:取一个整数a从右端开始的4~7位。
 920 程序分析:可以这样考虑:
 921 (1)先使a右移4位。
 922 (2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
 923 (3)将上面二者进行&运算。
 924 import java.util.Scanner;
 925 public class Prog32{
 926 public static void main(String[] msg){
 927   //输入一个长整数
 928   Scanner scan = new Scanner(System.in);
 929   long l = scan.nextLong();
 930   scan.close();
 931   //以下截取字符
 932   String str = Long.toString(l);
 933   char[] ch = str.toCharArray();
 934   int n = ch.length;
 935   if(n<7)
 936     System.out.println("输入的数小于7位!");
 937   else
 938     System.out.println("截取的4~7位数字:"+ch[n-7]+ch[n-6]+ch[n-5]+ch[n-4]);
 939   }  
 940 }
 941 【程序33】
 942 题目:打印出杨辉三角形(要求打印出10行如下图)
 943 程序分析:
 944         1
 945        1 1
 946      1 2 1
 947 1 3 3 1
 948 1 4 6 4 1
 949 1 5 10 10 5 1
 950 public class Prog33{
 951 public static void main(String[] args){
 952   int[][] n = new int[10][21];
 953   n[0][10] = 1;
 954   for(int i=1;i<10;i++)
 955     for(int j=10-i;j<10+i+1;j++)
 956       n[i][j] = n[i-1][j-1]+n[i-1][j+1];
 957   for(int i=0;i<10;i++){
 958    for(int j=0;j<21;j++){
 959     if(n[i][j]==0)
 960       System.out.print("   ");
 961     else{
 962        if(n[i][j]<10)
 963          System.out.print("  "+n[i][j]);//空格为了美观需要
 964        else if(n[i][j]<100)
 965          System.out.print(" "+n[i][j]);
 966          else
 967            System.out.print(n[i][j]);
 968      }
 969    }
 970    System.out.println();
 971   }
 972 }
 973 }
 974 【程序34】
 975 题目:输入3个数a,b,c,按大小顺序输出。
 976 程序分析:利用指针方法。
 977 import java.util.Scanner;
 978 public class Prog34{
 979 public static void main(String[] args){
 980   System.out.print("请输入3个数:");
 981   Scanner scan = new Scanner(System.in).useDelimiter("\\s");
 982   int a = scan.nextInt();
 983   int b = scan.nextInt();
 984   int c = scan.nextInt();
 985   scan.close();
 986   if(a<b){
 987    int t = a;
 988    a = b;
 989    b = t;
 990   }
 991   if(a<c){
 992    int t = a;
 993    a = c;
 994    c = t;
 995   }
 996   if(b<c){
 997    int t = b;
 998    b = c;
 999    c = t;
1000   }
1001   System.out.println(a+" "+b+" "+c);
1002 }
1003 }
1004 【程序35】
1005 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
1006 import java.util.Scanner;
1007 public class Prog35{
1008 public static void main(String[] args){
1009   System.out.print("请输入一组数:");
1010   Scanner scan = new Scanner(System.in).useDelimiter("\\s");
1011   int[] a = new int[50];
1012   int m = 0;
1013   while(scan.hasNextInt()){
1014    a[m++] = scan.nextInt();
1015   }
1016   scan.close();
1017 int[] b = new int[m];
1018   for(int i=0;i<m;i++)
1019     b[i] = a[i];
1020   for(int i=0;i<b.length;i++)
1021    for(int j=0;j<b.length-i-1;j++)
1022     if(b[j]<b[j+1]){
1023      int temp = b[j];
1024      b[j] = b[j+1];
1025      b[j+1] = temp;
1026     }
1027   for(int i=0;i<b.length;i++)
1028     System.out.print(b[i]+" ");
1029 
1030 }
1031 }
1032 【程序36】
1033 题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
1034 import java.util.Scanner;
1035 public class Prog36{
1036 public static void main(String[] args){
1037   final int N = 10;
1038   System.out.print("请输入10个数的数组:");
1039   Scanner scan = new Scanner(System.in);
1040   int[] a = new int[N];
1041   for(int i=0;i<a.length;i++)
1042     a[i] = scan.nextInt();
1043   System.out.print("请输入一个小于10的数:");
1044   int m = scan.nextInt();
1045   scan.close();
1046   int[] b = new int[m];
1047   int[] c = new int[N-m];
1048   for(int i=0;i<m;i++)
1049     b[i] = a[i];
1050   for(int i=m,j=0;i<N;i++,j++)
1051     c[j] = a[i];
1052   for(int i=0;i<N-m;i++)
1053     a[i] = c[i];
1054   for(int i=N-m,j=0;i<N;i++,j++)
1055     a[i] = b[j];
1056   for(int i=0;i<a.length;i++)
1057     System.out.print(a[i]+" ");
1058 }
1059 }
1060 【程序37】
1061 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
1062 import java.util.Scanner;
1063 public class Prog37{
1064 public static void main(String[] args){
1065   System.out.print("请输入一个整数:");
1066   Scanner scan = new Scanner(System.in);
1067   int n = scan.nextInt();
1068   scan.close();
1069   //定义数组变量标识某人是否还在圈内
1070   boolean[] isIn = new boolean[n];
1071   for(int i=0;i<isIn.length;i++)
1072     isIn[i] = true;
1073   //定义圈内人数、报数、索引
1074   int inCount = n;
1075   int countNum = 0;
1076   int index = 0;
1077   while(inCount>1){
1078    if(isIn[index]){
1079     countNum++;
1080     if(countNum==3){
1081      countNum = 0;
1082      isIn[index] = false;
1083      inCount--;
1084     }
1085    }
1086    index++;
1087    if(index==n)
1088      index = 0;
1089   }
1090   for(int i=0;i<n;i++)
1091     if(isIn[i])
1092       System.out.println("留下的是:"+(i+1));
1093 }
1094 }
1095 【程序38】
1096 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
1097 import java.util.Scanner;
1098 public class Prog38{
1099 public static void main(String[] args){
1100   System.out.print("请输入一串字符:");
1101   Scanner scan = new Scanner(System.in).useDelimiter("\\n");
1102   String strIn = scan.next();
1103   scan.close();
1104   char[] ch = strIn.toCharArray();
1105   System.out.println(strIn+"共"+(ch.length-1)+"个字符");
1106 }
1107 }
1108 【程序39】
1109 题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)
1110 import java.util.Scanner;
1111 public class Prog39{
1112 public static void main(String[] args){
1113   System.out.print("请输入一个整数:");
1114   Scanner scan = new Scanner(System.in);
1115   int n = scan.nextInt();
1116   scan.close();
1117   if(n%2==0)
1118     System.out.println("结果:"+even(n));
1119   else
1120     System.out.println("结果:"+odd(n));
1121 }
1122 //奇数
1123 static double odd(int n){
1124   double sum = 0;
1125   for(int i=1;i<n+1;i+=2){
1126    sum += 1.0/i;
1127   }
1128   return sum;
1129 }
1130 //偶数
1131 static double even(int n){
1132   double sum = 0;
1133   for(int i=2;i<n+1;i+=2){
1134    sum += 1.0/i;
1135   }
1136   return sum;
1137 }
1138 }
1139 【程序40】
1140 题目:字符串排序。
1141 public class Prog40{
1142 public static void main(String[] args){
1143   String[] str = {"abc","cad","m","fa","f"};
1144   for(int i=str.length-1;i>=1;i--){
1145    for(int j=0;j<=i-1;j++){
1146     if(str[j].compareTo(str[j+1])<0){
1147      String temp = str[j];
1148      str[j] = str[j+1];
1149      str[j+1] = temp;
1150     }
1151    }
1152   }
1153   for(String subStr:str)
1154     System.out.print(subStr+" ");
1155 }
1156 }
1157 【程序41】
1158 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
1159 public class Prog41{
1160 public static void main(String[] args){
1161   int n;
1162   n = fun(0);
1163   System.out.println("原来有"+n+"个桃子");
1164 }
1165 private static int fun(int i){
1166   if(i==5)
1167     return 1;
1168   else
1169     return fun(i+1)*5+1;
1170 }
1171 }
1172 【程序42】
1173 题目:809*??=800*??+9*??+1
1174 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
1175 public class Prog42{
1176 public static void main(String[] args){
1177   int n = 0;
1178   boolean flag = false;
1179   for(int i=10;i<100;i++)
1180     if(809*i==800*i+9*i+1){
1181      flag = true;
1182      n = i;
1183      break;
1184     }
1185   if(flag)
1186     System.out.println(n);
1187   else
1188     System.out.println("无符合要求的数!");
1189 }
1190 }
1191 【程序43】
1192 题目:求0—7所能组成的奇数个数。
1193 public class Prog43{
1194 public static void main(String[] args){
1195   int count = 0;
1196   //声明由数字组成的数
1197   int n = 8;
1198   //一位数
1199   count = n/2;
1200   //两位数
1201   count += (n-1)*n/2;
1202   //三位数
1203   count += (n-1)*n*n/2;
1204   //四位数
1205   count += (n-1)*n*n*n/2;
1206   //五位数
1207   count += (n-1)*n*n*n*n/2;
1208   //六位数
1209   count += (n-1)*n*n*n*n*n/2;
1210   //七位数
1211   count += (n-1)*n*n*n*n*n*n/2;
1212   System.out.println("0-7所能组成的奇数个数:"+count);
1213 }
1214 }
1215 【程序44】
1216 题目:一个偶数总能表示为两个素数之和。
1217 import java.util.Scanner;
1218 public class Prog44{
1219 public static void main(String[] args){
1220   System.out.print("请输入一个偶数:");
1221   Scanner scan = new Scanner(System.in);
1222   int n = scan.nextInt();
1223   scan.close();
1224   if(n%2!=0){
1225     System.out.println("您输入的不是偶数!");
1226     return;
1227   }
1228   twoAdd(n);
1229 }
1230 //偶数分解为素数之和
1231 private static void twoAdd(int n){
1232   for(int i=2;i<n/2+1;i++){
1233    if(isPrime(i)&&isPrime(n-i)){
1234     System.out.println(n+"="+(i)+"+"+(n-i));
1235     break;
1236    }
1237   }
1238 }
1239 //判断素数
1240 private static boolean isPrime(int m){
1241   boolean flag = true;
1242   for(int i=2;i<Math.sqrt(m)+1;i++){
1243    if(m%i==0){
1244     flag = false;
1245     break;
1246    }
1247   }
1248   return flag;
1249 }
1250 }
1251 【程序45】
1252 题目:判断一个素数能被几个9整除
1253 import java.util.Scanner;
1254 public class Prog45{
1255 public static void main(String[] args){
1256   System.out.print("请输入一个数:");
1257    Scanner scan = new Scanner(System.in);
1258    long l = scan.nextLong();
1259    long n = l;
1260    scan.close();
1261    int count = 0;
1262    while(n>8){
1263     n /= 9;
1264     count++;
1265    }
1266    System.out.println(l+"能被"+count+"个9整除。");
1267 }
1268 }
1269 【程序46】
1270 题目:两个字符串连接程序
1271 public class Prog46{
1272 public static void main(String[] args){
1273   String str1 = "lao lee";
1274    String str2 = "牛刀";
1275    String str = str1+str2;
1276    System.out.println(str);
1277 }
1278 }
1279 【程序47】
1280 题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
1281 import java.util.Scanner;
1282 public class Prog47{
1283 public static void main(String[] args){
1284   System.out.print("请输入7个整数(1-50):");
1285   Scanner scan = new Scanner(System.in);
1286   int n1 = scan.nextInt();
1287   int n2 = scan.nextInt();
1288   int n3 = scan.nextInt();
1289   int n4 = scan.nextInt();
1290   int n5 = scan.nextInt();
1291   int n6 = scan.nextInt();
1292   int n7 = scan.nextInt();
1293   scan.close();
1294   printStar(n1);
1295   printStar(n2);
1296   printStar(n3);
1297   printStar(n4);
1298   printStar(n5);
1299   printStar(n6);
1300   printStar(n7);
1301 }
1302 static void printStar(int m){
1303   System.out.println(m);
1304   for(int i=0;i<m;i++)
1305     System.out.print("*");
1306   System.out.println();
1307 }
1308 }
1309 【程序48】
1310 题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
1311 public class Prog48{
1312 public static void main(String[] args){
1313   int n = 1234;
1314   int[] a = new int[4];
1315   for(int i=3;i>=0;i--){
1316     a[i] = n%10;
1317     n /= 10;
1318   }
1319   for(int i=0;i<4;i++)
1320     System.out.print(a[i]);
1321   System.out.println();
1322   for(int i=0;i<a.length;i++){
1323     a[i] += 5;
1324     a[i] %= 10;
1325   }
1326   int temp1 = a[0];
1327   a[0] = a[3];
1328   a[3] = temp1;
1329   int temp2 = a[1];
1330   a[1] = a[2];
1331   a[2] = temp2;
1332   for(int i=0;i<a.length;i++)
1333     System.out.print(a[i]);
1334 }
1335 }
1336 【程序49】
1337 题目:计算字符串中子串出现的次数
1338 public class Prog49{
1339 public static void main(String[] args){
1340   String str = "I come from County DingYuan Province AnHui.";
1341   char[] ch = str.toCharArray();
1342   int count = 0;
1343   for(int i=0;i<ch.length;i++){
1344    if(ch[i]==' ')
1345      count++;
1346   }
1347   count++;
1348   System.out.println("共有"+count+"个字串");
1349 }
1350 }
1351 【程序50】
1352 题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
1353 import java.io.*;
1354 public class Prog50{
1355 //定义学生模型
1356 String[] number = new String[5];
1357 String[] name = new String[5];
1358 float[][] grade = new float[5][3];
1359 float[] sum = new float[5];
1360 public static void main(String[] args) throws Exception{
1361   Prog50 stud = new Prog50();
1362   stud.input();
1363   stud.output();
1364 }
1365 //输入学号、姓名、成绩
1366 void input() throws IOException{
1367   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
1368   //录入状态标识
1369   boolean isRecord = true;
1370   while(isRecord){
1371    try{
1372      for(int i=0;i<5;i++){
1373       System.out.print("请输入学号:");
1374       number[i] = br.readLine();
1375       System.out.print("请输入姓名:");
1376       name[i] = br.readLine();
1377       for(int j=0;j<3;j++){
1378        System.out.print("请输入第"+(j+1)+"门课成绩:");
1379        grade[i][j] = Integer.parseInt(br.readLine());
1380       }
1381       System.out.println();
1382       sum[i] = grade[i][0]+grade[i][1]+grade[i][2];
1383      }
1384        isRecord = false;
1385       }catch(NumberFormatException e){
1386         System.out.println("请输入一个数字!");
1387     }
1388   }
1389 }
1390 //输出文件
1391 void output() throws IOException{
1392   FileWriter fw = new FileWriter("E://java50//stud.txt");
1393   BufferedWriter bw = new BufferedWriter(fw);
1394   bw.write("No.  "+"Name  "+"grade1  "+"grade2  "+"grade3  "+"average");
1395   bw.newLine();
1396   for(int i=0;i<5;i++){
1397     bw.write(number[i]);
1398     bw.write("  "+name[i]);
1399     for(int j=0;j<3;j++)
1400       bw.write("  "+grade[i][j]);
1401     bw.write("  "+(sum[i]/5));
1402     bw.newLine();
1403   }
1404   bw.close();
1405 }
1406 }

 

posted @ 2016-07-05 22:37  红酒人生  阅读(2873)  评论(0编辑  收藏  举报