导语:总结了一些流程控制的小案例以及用Java编写了一个根据输入年份和月份打印对应月份的日历的一个小程序,并且初步理解了一下Java的封装性。
日历案例
//日历
public class Calendar {
private int month;
private int year;
private int day = 1;
private int tt;//制表符的个数,判断月份的第一天是星期几
private int addMonth;//输入对应的月份的天数
public Calendar() {
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
//输入日历
public void addCalendar() {
System.out.println("一\t" + "二\t" + "三\t" + "四\t" + "五\t" + "六\t" + "七\t");
int days = 1;
int line = 0;//代表换行的变量
for(int i = 0 - tt;i < addMonth;i++ ) {
if(i < 0) {
System.out.print("\t");
}else {
System.out.print( days++ + "\t");
}
line++;
if(line % 7 == 0) {
System.out.println();
}
}
}
//输入制表符的个数 判断输入的月份是星期几
public void addTt() {
int monthNumber = 0;//保存当年月的天数
int yearNumber = 0;//保存当年距离2000年的天数
//计算输入的日期到2000年1月1日有多少天
for(int i = 2000;i < year;i++) {
if( !(i % 100 == 0) && i % 4 == 0 || i % 400 == 0 ) {
yearNumber += 366;
}else {
yearNumber += 365;
}
}
for(int i = 1;i < month;i++) {
if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
monthNumber += 31;
addMonth = 31;
}else if(i == 2){
if( !(year % 100 == 0) && year % 4 == 0 || year % 400 == 0 ) {
monthNumber += 29;
addMonth = 29;
}else {
monthNumber += 28;
addMonth = 28;
}
}else {
monthNumber += 30;
addMonth = 30;
}
}
if((monthNumber + yearNumber) % 7 < 2) {
tt = (monthNumber + yearNumber) % 7 + 5;
}else {
tt = (monthNumber + yearNumber) % 7 - 2;
}
}
//测试类
import java.util.Scanner;
public class CalendarTest {
public static void main(String[] args) {
Calendar cal = new Calendar();
Scanner input = new Scanner(System.in);
System.out.println("请输入年份");
int years = input.nextInt();
System.out.println("请输入月份");
int months = input.nextInt();
//写入年份和月份
cal.setMonth(months);
cal.setYear(years);
cal.addTt();
cal.addCalendar();
}
}```
## 流程循环小案例
```java
//99乘法表
public class List9_9 {
public static void main(String[] args) {
for(int i = 1;i <= 9;i++) {
for(int j = 1;j <= i;j++) {
System.out.print(j + "*" + i + "=" + i*j + " ");
}
System.out.println();
}
}
}
//100以内5的倍数
public class Mulitple_5 {
public static void main(String[] args) {
for(int i = 1;i < 100;i++) {
if(i % 5 ==0) {
System.out.println(i);
}
}
}
}
//输出100以内的质数
public class PrimeNumber_of_100 {
public static void main(String[] args) {
//方法一
for(int i = 2;i < 100;i++) {
for(int j = 2;j <= i;j++) {
if(i % j == 0) {
break;
}else if(j == i-1) {
System.out.println(i);
}
}
}
//方法二
list:for(int i = 2;i < 100;i++) {
for(int j = 2;j < i;j++) {
if(i % j == 0) {
continue list;
}
}
System.out.println(i);
}
}
}
//打印靠左三角形
public class TriangleGraphics {
public static void main(String[] args) {
TriangleGraphics tri = new TriangleGraphics();
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数字");
int nums = input.nextInt();
tri.Triangle(nums);
input.close();
}
public void Triangle(int nums) {
for(int i = 1;i <= nums;i++) {
for(int j = 1;j <= i;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
//打印靠右三角形
public class TriangleGraphics2 {
public static void main(String[] args) {
TriangleGraphics2 tri = new TriangleGraphics2();
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数字");
int nums = input.nextInt();
tri.Triangle1(nums);
input.close();
}
public void Triangle1(int nums) {
for(int i = nums;i > 0;i--) {
int x = 0;
for(int j = i - 1;j > 0;j--) {
System.out.print(" ");
x++;
}
for(int a = 1;a <= nums - x;a++) {
System.out.print("*");
}
System.out.println();
}
}
}
//打印等腰三角形
public class TriangleGraphics3 {
public static void main(String[] args) {
TriangleGraphics3 tri = new TriangleGraphics3();
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数字");
int nums = input.nextInt();
tri.Triangle1(nums);
input.close();
}
public void Triangle1(int nums) {
for(int i = nums;i > 0;i--) {
int x = 0;
for(int j = i - 1;j > 0;j--) {
System.out.print(" ");
x++;
}
for(int a = 1;a <= nums - x;a++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
//打印空心等腰三角形
public class TriangleGraphics4 {
public static void main(String[] args) {
TriangleGraphics4 tri = new TriangleGraphics4 ();
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数字");
int nums = input.nextInt();
tri.Triangle1(nums);
input.close();
}
public void Triangle1(int nums) {
for(int i = nums;i > 0;i--) {
int x = 0;
for(int j = i - 1;j > 0;j--) {
System.out.print(" ");
x++;
}
for(int a = 1;a <= nums - x;a++) {
if(i >= nums - 1) {
System.out.print("*");
System.out.print(" ");
}else if(i == 1) {
System.out.print("*");
System.out.print(" ");
}else {
if(a == 1 || a == nums - x ) {
System.out.print("*");
System.out.print(" ");
}else {
System.out.print(" ");
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
希望对你们有所帮助。