其他分享
首页 > 其他分享> > 第三章第四题(随机月份)(Random month)

第三章第四题(随机月份)(Random month)

作者:互联网

**3.4(随机月份)编写一个随机产生1和12之间整数的程序,并且根据数字1,2,……,12显示相应的英文月份:January,February,……,December。

**3.4(Random month) Write a program that randomly generates an integer between 1 and 12 and displays the English month names January, February, . . . , December for the numbers 1, 2, . . . , 12, accordingly.

下面是参考答案代码:

public class RandomMonthQuestion4 {
	public static void main(String[] args) {
		int randomNumber = (int)(Math.random() * 12) + 1;

		switch(randomNumber)
		{
			case 1:
				System.out.println("random number " + randomNumber + " is for January");
				break;
			case 2:
				System.out.println("random number " + randomNumber + " is for February");
				break;
			case 3:
				System.out.println("random number " + randomNumber + " is for March");
				break;
			case 4:
				System.out.println("random number " + randomNumber + " is for April");
				break;
			case 5:
				System.out.println("random number " + randomNumber + " is for May");
				break;
			case 6:
				System.out.println("random number " + randomNumber + " is for June");
				break;
			case 7:
				System.out.println("random number " + randomNumber + " is for July");
				break;
			case 8:
				System.out.println("random number " + randomNumber + " is for August");
				break;
			case 9:
				System.out.println("random number " + randomNumber + " is for September");
				break;
			case 10:
				System.out.println("random number " + randomNumber + " is for October");
				break;
			case 11:
				System.out.println("random number " + randomNumber + " is for November");
				break;
			case 12:
				System.out.println("random number " + randomNumber + " is for December");
				break;
		}
	}
}

运行效果:
在这里插入图片描述
在这里插入图片描述

注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)

xjlovewjh 发布了47 篇原创文章 · 获赞 1 · 访问量 1858 私信 关注

标签:case,Random,第三章,random,System,month,break,println,randomNumber
来源: https://blog.csdn.net/xjlovewjh/article/details/104171061