案例一
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;标签:java,int,打印,System,----,num,println,out From: https://www.cnblogs.com/wang1999an/p/16769890.html
/**
* @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();
}
}
}
// *
// ***
// *****
// *******
// *****
// ***
// *