Java基本功练习二(显示日历,石头剪子布游戏,找完全数等)

来源:互联网 发布:visio有mac版吗 编辑:程序博客网 时间:2024/06/06 03:47

      本作者是基于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;elsecountDayPerMonth = 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 pcwinint 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);elsepc_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++;}elsenumber++;}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基本功练习的第二篇,写的好痛苦啊,希望对各位童鞋有所帮助,谢谢!

0 0
原创粉丝点击