if语句编写分时问候
switch语句及表达式使用
打印九九乘法表
打印菱形图案
编写双色球彩票选号案例
import java.util.Arrays;
import java.util.Random;
public class EX1 {
// 双色球
public static void main(String[] args) {
// 生成随机数
Random rand =new Random();
// 数组对象初始化,数组成员为六个;(红球为六注)
int[] rb = new int[6];
for (int i=0;i<rb.length;i++) {
// 判断数组中是否存在生成的随机数
int t=rand.nextInt(1,34);
// boolean 数据类型 boolean 变量存储为 8位(1 个字节)的数值形式,但只能是 True 或是 False。
// 而不能用0和1代替,并且一定要小写。
Boolean f =true;
// foreach语句遍历输出,正序输出
for (int n:rb) {
if (n == t) {
f=false;
--i;
break;
}
}
if(f){
rb[i]=t;
}
}
// 生成随机篮球数组成员
int bb = rand.nextInt(1,17);
System.out.printf("红球:%s", Arrays.toString(rb));
System.out.printf("红球:%d%n",bb);
}
}
编写猜数游戏
百钱百鸡(算法)
公鸡3钱/个;母鸡5钱/个,雏鸡1钱/3个
public class For6 {
public static void main(String[] args) {
int temp = 0;
for (int i = 0; i < 100; i += 5) {
for (int j = 0; j < 100; j += 3) {
for (int k = 0; k < 100; k++) {
int money = i + j + k;
int only = i / 5 + j / 3 + 3 * k;
temp++;
if (only == 100 && money == 100) {
System.out.printf("公鸡:%d 母鸡:%d 小鸡:%d %n", i / 5, j / 3, 3 * k);
}
}
}
}
System.out.println("运行了" + temp + "次");
}
}
微信时间显示效果案例
@SuppressWarnings("ALL")
public class EX2 {
public static void main(String[] args) {
String msg = "你的电话是多少?";
// Calendar time = Calendar.getInstance();
long time =1666183599323L;
System.out.printf("留言时间时:%tF %<tT%n",time);
long now = System.currentTimeMillis();
System.out.printf("现在时间是:%tF %<tT%n",now);
long result;
result = now -time;
long mm = result / (1000 * 60);
if (mm < 1) {
System.out.printf("信息:%s 刚刚", msg);
} else if (mm / 60 < 1) {
System.out.printf("信息:%s %d分钟前", msg,mm);
} else if (mm / (60 * 24) < 1) {
System.out.printf("信息:%s %d小时前", msg, mm / 60);
} else {
System.out.printf("信息:%s %d天前", msg, mm / (60 * 24));
}
}
}