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 }
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM