本作者是基於Eclipse編寫的程序,當然也可以用TextPad或NetBeans進行編程。不管用哪一種,學習的重點都是將具體問題轉化成程序化語言的能力,學習的是這種思想。希望童鞋們和我一樣,每天進步一點點!廢話少說,進入正題
示例一:用戶輸入某一年,程序輸出顯示這一年每個月的日曆。(你能自己設計出來嗎?)請看運行效果圖(只展示了前六月的六張圖)。
這道題目有幾個坑:
1)如何顯示對齊以符合要求;
2)如何確定每月第一天是星期幾;
3)如何判斷某一月有多少天;
4)閏年的情況要考慮周到。
基於Eclipse的實現代碼如下:
package shiyanPractice;
import java.util.Scanner;
public class ShiYanPractice {
public static void main(String[] args) {
//輸入年份,顯示相應年份的日曆
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of year: ");
int year = input.nextInt();//輸入想要顯示的年份
int month = 1;
int day = 0;
//12個月,所以循環12次,每次顯示一個月的日曆
for(int i = 0;i < 12;i++){
int h,q,m,j,k;
month = i+1;
day = 1; //只需算出每個月的第一天是周幾
m = month;
q = day;
//當月份爲1或2時,要將其視爲上一年的13或14月,並且相應的年份改爲上一年
if(m == 1 || m == 2){
m+=12;
year -= 1;
}
j = year/100; //求出h表示的星期幾,其中0表示週日,1表示週六,2表示週一,直到6表示週五
k = year%100;
//澤勒一致性公式
h = (q+(int)(26*(m+1)/10)+k+(int)(k/4)+(int)(j/4)+5*j)%7;
//計算完h之後要將year的值改回來
if(month == 1 ||month == 2)
year += 1;
//用於將h轉換成對應的星期幾
String dayTranslate = "";
switch(h){
case 0:dayTranslate = "Saturday";break;
case 1:dayTranslate = "Sunday";break;
case 2:dayTranslate = "Monday";break;
case 3:dayTranslate = "Thusday";break;
case 4:dayTranslate = "Wednesday";break;
case 5:dayTranslate = "Thursday";break;
case 6:dayTranslate = "Friday";break;
}
//用以顯示日曆的每個月的擡頭
String monthFor = "";
switch(month){
case 1:monthFor = "January";break;
case 2:monthFor = "February";break;
case 3:monthFor = "March";break;
case 4:monthFor = "April";break;
case 5:monthFor = "May ";break;
case 6:monthFor = "June ";break;
case 7:monthFor = "July ";break;
case 8:monthFor = "August";break;
case 9:monthFor = "September";break;
case 10:monthFor = "October";break;
case 11:monthFor = "November";break;
case 12:monthFor = "December";break;
}
//顯示每個月的擡頭
String title = "\t"+monthFor+" "+year+"\n"
+"Sun "+"Mon "+"Tue "+"Wed "+"Thu "+"Fri "+"Sat\n";
System.out.println(title);
//將h轉換成相應的星期幾的數字
int dayCount = 0;
switch(h){
case 0:dayCount = 6;break;
case 1:dayCount = 0;break;
case 2:dayCount = 1;break;
case 3:dayCount = 2;break;
case 4:dayCount = 3;break;
case 5:dayCount = 4;break;
case 6:dayCount = 5;break;
}
//某個月第一天在日曆中位置的確認
for(int jj = 1;jj <= dayCount;jj++){
System.out.print(" ");
}
int countToAnotherLine = dayCount+1;//將第一天的位置數加1,方便用於後續轉行的控制
//計算每個月的天數
int countDayPerMonth = 31;
switch(month){
case 1:countDayPerMonth = 31;break;
case 2:
if((year % 4 == 0 && year % 100 != 0)||(year % 400 ==0))
countDayPerMonth = 29;
else
countDayPerMonth = 28;
break;
case 3:countDayPerMonth = 31;break;
case 4:countDayPerMonth = 30;break;
case 5:countDayPerMonth = 31;break;
case 6:countDayPerMonth = 30;break;
case 7:countDayPerMonth = 31;break;
case 8:countDayPerMonth = 31;break;
case 9:countDayPerMonth = 30;break;
case 10:countDayPerMonth = 31;break;
case 11:countDayPerMonth = 30;break;
case 12:countDayPerMonth = 31;break;
}
//依次輸出某個月的每一天
for(int ii = 1;ii <= countDayPerMonth;ii++){
if(countToAnotherLine % 7 == 0){
System.out.printf("%3d ",ii);
System.out.println();
countToAnotherLine++;
}
else{
System.out.printf("%3d ",ii);
countToAnotherLine++;
}
}
System.out.println();
}
}
}
示例二:石頭剪子布的遊戲。用戶和計算機玩石頭剪子布的遊戲,直到用戶或計算機連續贏兩次,遊戲結束,並顯示每次的結果和最終的結果(是用戶還是計算機獲勝)。(很簡單是嗎?自己編寫實現看看再說。)運行效果圖如下所示(四種情況的運行情況,編程時要考慮周全):
此題的坑在“連續”兩字上,要處理好交替贏和中間有平局的情況。下面是實現代碼:
package shiyanPractice;
import java.util.Scanner;
public class ShiYanPractice {
public static void main(String[] args) {
//石頭剪子布的遊戲,電腦或用戶贏兩次以上就獲勝,遊戲結束
Scanner input = new Scanner(System.in);
int pc_user_Win = 0;//1 userwin,2 pcwin
int pc_user_WinLastTime = 0;
int pcData = 0;
int userData = 0;
System.out.println("石頭、剪子、布遊戲,由用戶和電腦玩,誰連續贏兩次就勝出!");
while(true){
pcData = (int)(Math.random()*3+1);
System.out.print("玩家請輸入,1是剪刀,2是石頭,3是布:");
userData = input.nextInt();
//統計誰贏,並將pc_user_Win標誌位賦值,輸出顯示贏家
switch(userData){
case 1:{
if(userData - pcData == -2){
pc_user_Win = 1;
System.out.println("User Win!");
break;
}
else if(userData - pcData == -1){
pc_user_Win = 2;
System.out.println("Pc Win!");
break;
}
else{
System.out.println("User and Pc is same!");
break;
}
}
case 2:{
if(userData - pcData == 1){
pc_user_Win = 1;
System.out.println("User Win!");
break;
}
else if(userData - pcData == -1){
pc_user_Win = 2;
System.out.println("Pc Win!");
break;
}
else{
System.out.println("User and Pc is same!");
break;
}
}
case 3:{
if(userData - pcData == 1){
pc_user_Win = 1;
System.out.println("User Win!");
break;
}
else if(userData - pcData == 2){
pc_user_Win = 2;
System.out.println("Pc Win!");
break;
}
else{
System.out.println("User and Pc is same!");
break;
}
}
}
//判斷是否連續贏兩次,是就輸出誰贏,並結束循環
if(pc_user_Win != 0){//排除因爲等於零的情況的誤判,因爲等於零代表沒有勝負
if(pc_user_WinLastTime == pc_user_Win && pc_user_Win == 1){
System.out.println("User Win the game!");
break;
}
else if(pc_user_WinLastTime == pc_user_Win && pc_user_Win == 2){
System.out.println("Pc Win the game!");
break;
}
}
//只有將非零的勝負標誌值賦值給記錄上一次勝負情況的標誌位
//否則可能出現pc贏一次,平一次,再贏一次而不顯示pc獲勝的情況
if(pc_user_Win == 0);
else
pc_user_WinLastTime = pc_user_Win;
pc_user_Win = 0;//將標誌位置爲無效,否則將影響下一次判斷
}
}
}
示例三:找10000以內的完全數。完全數是指一個正整數等於除它本身之外其他所有除數之和。例如:6是第一個完全數,因爲6=1+2+3;下一個完全數是28=14+7+4+2+1。10000以內的完全數有四個,編程找出。
此題相對來說比較容易,但還是得自己動手實踐,實踐可以檢查自己的問題並提高自我。運行效果圖如下:
實現代碼如下所示:
package shiyanPractice;
import java.util.Scanner;
public class ShiYanPractice {
public static void main(String[] args) {
//找出完全數的程序
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int data = input.nextInt();
System.out.println(0+"~"+data+"之間的完全數顯示如下:");
for(int iData = 0;iData < data;iData++){
int number = 2;
int sumOfNumber = 1;
while(number <= iData){
if(iData % number == 0){
if(number != iData)
sumOfNumber += number;
number++;
}
else
number++;
}
if(iData == sumOfNumber &&iData != 1){
System.out.println(iData);
}
}
}
}
示例四:用戶輸入一個短整型數據(16位的),然後顯示這個整數的16比特形式。
運行效果如圖所示:
提示:此題的關鍵是對數據位操作是否熟悉。可以先自己嘗試!
實現代碼如下所示:
package shiyanPractice;
import java.util.Scanner;
public class ShiYanPractice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("輸入需要輸入整型數的次數: ");
int n = input.nextInt();
for(int i = 0;i < n; i++){
System.out.print("Enter an integer: ");
int value = input.nextInt();
System.out.print("The 16 bits are: ");
int mask = 1;
for(int j = 15;j >=0; j--){
int temp = value>>j;
int bit = temp & mask;
System.out.print(bit);
}
System.out.println();
}
}
}
這是Java基本功練習的第二篇,寫的好痛苦啊,希望對各位童鞋有所幫助,謝謝!