JAVA----小案例,幸运会员,打印正三角
1.接收一个4位数会员号
2.生成随机数并乘以10
3.算出会员号中的百位数字的数字号与生成的随机数比较
4.如果相等则是幸运会员,反之不是
package learnday5;
import java.util.Scanner;
public class FenzhiExercise {
public static void main(String[] args) {
exercise();
}
private static void exercise() {
Scanner input = new Scanner(System.in);
System.out.println("接收的4位数会员号");
int num = input.nextInt();
if(num < 1000||num>=10000){
System.out.println("输入的会员号不合规格");
return;
}
int bai = num / 100 % 10;//0-9
int random = (int)(Math.random() * 10);
System.out.println(random);
if(bai != random){
System.out.println(num+"不是幸运会员");
return;
}
System.out.println(num+"是幸运会员");
input.close();
}
}
案例二
先打印正三角,然后再基础上在打印一个倒三角,成为棱形
package learnday5;
/**
* @author: Wang
* @className: Exercise02
* @description:
* @date: 2022/10/8 17:17
* @version: 0.1
* @since: 1.8
*/
public class Exercise02 {
public static void main(String[] args) {
execrise();
}
private static void execrise() {
int totalRow = 7;
int little = totalRow/2;
int many = little+1;
for (int i = 1; i <=many; i++) {
for (int k = 1; k <=many-i ; k++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}//上面打印正三角,下面打印倒三角
for (int i = 1; i <=little; i++) {
for (int k = 1; k <=i ; k++) {
System.out.print(" ");
}
for (int j = 1; j <=totalRow- 2 *i ; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
// *
// ***
// *****
// *******
// *****
// ***
// *
案例三
计算用户输入的日期离1900年1月1日相距多少天
package learnday5;
public class Exercise04 {
public static void main(String[] args) {
exercise();
}
//计算用户输入的日期离1900年1月1日相距多少天
private static void exercise() {
int year = 2021;
int month = 9;
int totalDay = 20;
for (int y = 1900; y < year; y++) {
if (y % 4 == 0 && y % 100 != 0 || (y % 400 == 0)) {
totalDay += 366;
} else {
totalDay += 365;
}
}
for (int m = 1; m < month; m++) {
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 10:
totalDay += 31;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || (year % 400 == 0)) {
totalDay += 29;
} else {
totalDay += 28;
}
default:
totalDay += 30;
break;
}
}
System.out.println("总天数" + totalDay);
}
}