您的位置:首页 > 其它

随机生成+if语句示例

2020-06-06 14:52 197 查看

运气测试器eg:

package xiafly;

public class TestIf {
public static void main(String[] args) {

double x = Math.random();   //  随机生成【0,1)的随机数
int a = 1 + (int) (Math.random() * 6);
int b = 1 + (int) (Math.random() * 6);
int c = 1 + (int) (Math.random() * 6);
int count = a + b + c;

System.out.println("\t第一颗骰子:" + a + "\n\t第二颗骰子:" + b + "\n\t第三颗骰子:" + c + "\n\t总分:" + count);

if (count > 15) {
System.out.println("\t手气不错,挣大钱");
}
if (10 <= count && count <= 15) {
System.out.println("\t手气一般,继续玩");
}
if (count < 10) {
System.out.println("\t手气差,赶紧溜");
}
}
}

运行结果:

第一颗骰子:6
第二颗骰子:6
第三颗骰子:4
总分:16
手气不错,挣大钱

Process finished with exit code 0

就运行两次,手气不错,/手动狗头/
年龄随机生成eg:

package xiafly;

public class Testif01 {
public static void main(String[] args) {

int age = (int) (100 * Math.random());

System.out.print("年龄:" + age + "\t属于");

if (age < 16) {
System.out.printf("青少年,喜欢玩");
} else if (age <= 28) {
System.out.printf("青年,要学习");
} else if (age <= 50) {
System.out.printf("中年,要工作");
} else if (age <= 70) {
System.out.printf("老年,要养生");
} else {
System.out.printf("古稀之年");
}
}
}

运行结果:

年龄:10	属于青少年,喜欢玩
Process finished with exit code 0

我才10岁…

迟到是要扣钱的eg:

package xiafly;

public class LateTime {

public static int late(int LateMinute, double salary) {
int money = 0;

if (LateMinute < 11) {
System.out.println("警告!!!不能迟到!!!");
} else if (LateMinute < 21) {
money = 100;
} else if (LateMinute < 31) {
money = 200;
} else if (LateMinute < 61) {
money = (int) (salary / (21.75 * 2));         //21.75 月平均工作日
} else {
money = (int) ((salary / 21.75) * 3);
}
System.out.println("罚款:" + money);

return money;
}

//方法的重载,可以根据形参的不同自动匹配
public static int add() {
return 0;
}

public static int add(int a, int b) {
return 1;

}

public static void main(String[] args) {

late(26, 6000);
add();
add(1, 1);

}
}

运行结果:

罚款:200

Process finished with exit code 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: