1、計算器布局
分析:計算器窗口主要由上下兩部分組成,使用Frame默認的BorderLayout布局方式。北部使用Panel裝載-一 個保存計算結果的文本框;中部使用Panel裝載計算器的20個按鈕,采用GridLayout布局。為了維持布局方式,禁止改變Frame的大小。將計算器20個按鈕上的符號初始化在一個 String字符串中,創建按鈕時從中截取相應的字符。構建GUI時通常這樣划分代碼:將組件定義為屬性;在構造方法中構建組件;自定義init()方法設置布局;在show()方法中設置GUI的顯示屬性。
書上源碼有錯誤,錯誤代碼:
修正后,正確代碼:
源碼如下:
1 import java.awt.*; 2 3 public class AWTCalculation { 4 5 //全局變量 6 private Frame frame; 7 private TextField field; 8 private Button[] allButtons; 9 10 //構造方法用於創建組件並對其初始化 11 //生成並初始化對象 12 public AWTCalculation(){ 13 frame = new Frame("票務專用計算器"); //設置窗口名稱 14 field = new TextField(20); //20表示文本寬度,此文本框只接收20個字符 15 allButtons = new Button[20]; //20個按鈕 16 String str = "←C√±789/456*123-0.=+"; 17 for(int i = 0; i < str.length(); i++){ 18 allButtons[i] = new Button(str.substring(i,i+1)); //substring()字符串截取函數 19 } 20 } 21 //初始化窗口,設置布局 22 private void init(){ 23 //北部面板(上方面板放置文本框) 24 Panel northPanel = new Panel(); 25 northPanel.add(field); 26 //中部面板(放置計算器按鈕) 27 Panel centerPanel = new Panel(); 28 GridLayout grid = new GridLayout(5,4,10,10); //5行4列,上下間距10個像素 29 centerPanel.setLayout(grid); 30 for(int i = 0; i < allButtons.length; i++){ 31 centerPanel.add(allButtons[i]); 32 } 33 //將面板添加到窗口 34 frame.add(northPanel,BorderLayout.NORTH); 35 frame.add(centerPanel,BorderLayout.CENTER); 36 } 37 38 //設置窗口的顯示屬性 39 public void showMe(){ 40 init(); 41 frame.pack(); //設置窗口的最佳大小 42 frame.setLocation(300,200); //設置窗口顯示初始位置 43 frame.setResizable(true); //允許改變窗口的大小,false禁止改變大小 44 frame.setVisible(true); //顯示窗口 45 } 46 47 public static void main(String[] args){ 48 new AWTCalculation().showMe(); 49 } 50 }
運行結果:
2、工資計算
某學校教師的工資=基本工資+課時補貼。教授的基本工資為5000元,每學時補貼70元;副教授的基本工資為3500元,每學時補貼60元;講師的基本工資為2600元,每學時補貼55元。已知每個教師的學時數,請編程計算每個教師的每月工資數目。
1 import java.util.Scanner; 2 public class Stady { 3 public static void main(String[] args){ 4 Scanner scan = new Scanner(System.in); 5 System.out.println("*********** 工資計算 ***********\n*"); 6 System.out.print("*\t1.計算教授工資;"); 7 System.out.println("\t *"); 8 System.out.print("*\t2.計算副教授工資;"); 9 System.out.println(" *"); 10 System.out.print("*\t3.計算講師工資; *\n*"); 11 System.out.println("\t\t *"); 12 for(int i=0;i<31;i++){ 13 System.out.print("*"); 14 if(i==30) 15 System.out.print("\n"); 16 } 17 int temp; 18 while(true){ 19 System.out.print("請選擇:"); 20 temp = scan.nextInt(); 21 switch(temp){ 22 case 1: 23 System.out.print("請輸入教授學時:"); 24 int time0 = scan.nextInt(); 25 System.out.println("教授每月工資數目:"+ (5000 + 70 * time0) ); 26 break; 27 case 2: 28 System.out.print("請輸入副教授學時:"); 29 int time1 = scan.nextInt(); 30 System.out.println("副教授每月工資數目:"+ (3500 + 60 * time1) ); 31 break; 32 case 3: 33 System.out.print("請輸入講師學時:"); 34 int time2 = scan.nextInt(); 35 System.out.println("講師每月工資數目:"+ (2600 + 55* time2) ); 36 break; 37 } 38 } 39 } 40 }
3、公里計價
編寫一個北京地鐵按公里計價的程序。計價規則為: 6km (含)內3元; 6~12km(含4元;12-22km(含)5元;22~32km(含)6元;32km以上每加1元可乘坐20km.
1 import java.util.Scanner; 2 3 public class Price { 4 public static void main(String[] args){ 5 System.out.print("輸入距離:"); 6 Scanner scn = new Scanner(System.in); 7 int distance = scn.nextInt(); 8 int price; 9 if(distance <= 6 && distance > 0){ 10 price = 3; 11 } 12 else if(distance <= 12){ 13 price = 4; 14 } 15 else if(distance <= 22){ 16 price = 5; 17 } 18 else if(distance <= 32){ 19 price = 6; 20 } 21 else{ 22 if((distance - 32) % 20 != 0){ 23 price = 6 + (distance - 32) / 20 + 1; //沒滿20Km,按20Km計算 24 } 25 else{ 26 price = 6 + (distance - 32) / 20; 27 } 28 } 29 System.out.printf("price=%d\n", price); //格式化輸出 30 } 31 }
4、數據加密
某個公司傳送數據時采用加密方式,數據是四位整數,加密規則如下:每位數字都加上5,然后用和除以10的余數代替該數字,再將第一位和第四位交換,第二位和第三位交換。
1 public class Encode { 2 public static void main(String[] args){ 3 int number = (int)(Math.random() * 10000) + 1000; //隨機生成四位數 4 System.out.println("加密前:" + number); 5 int a = (number % 10 + 5) % 10; //個位 6 number /= 10; 7 int b = (number % 10 + 5) % 10; //十位 8 number /= 10; 9 int c = (number % 10 + 5) % 10; //百位 10 number /= 10; 11 int d = (number % 10 + 5) % 10; //千位 12 13 //交換1、4位 14 int temp = a; 15 a = d; 16 d = temp; 17 18 //交換2、3位 19 temp = b; 20 b = c; 21 c = temp; 22 System.out.println("加密后:" + d + c + b + a); 23 } 24 }
5、約瑟夫環
解約瑟夫問題。約瑟夫問題是一個 出現在計算機科 學和數學中的經典問題,可以這樣描述:N個人圍成一圈,從第一個開始報數,每報數到M的人出列,然后從下一一個人開始重新報數,直到最后剩下一個人。編寫程序,打印出列的順序以及最后剩下人的序號。例如N=6,M=5,出列的順序:5,4,6,2,3,剩下1。
1 public class Josephus { 2 3 /** 4 * 約瑟夫問題 5 * 6 */ 7 static int N = 6; 8 static int M = 5; 9 10 public static void main(String[] args) { 11 boolean[] person = new boolean[N+1]; //默認為false 12 13 int count=N; 14 int now=1; 15 int next=1; 16 while(count>1){ 17 if(person[now]==false){//未被殺死的報數 18 if(next==M){ 19 person[now]=true; //殺死 20 System.out.print(" " + now); 21 count--; 22 next=1; //從1開始報數 23 }else{ 24 next++; //繼續報數 25 } 26 } 27 if(now==N){ 28 now=1; 29 }else{ 30 now++; 31 } 32 } 33 34 for(int i=1; i<person.length; i++){ 35 if(person[i]==false){ 36 System.out.println("\n" + "最后剩下" + i); 37 } 38 } 39 } 40 }