一、单选择结构if语句
math类的使用:
Math.random()解释为在[0,1)区间中选取一个数类型为double.利用与整数乘积与强制转型为整数,可以提取随机的整数
例子1:
public class TestIf {
public static void main(String[] args) {
double a = Math.random();
int b = 1+(int)a*5;//骰子1-6整数
System.out.println(b);
/*if(a>3)
System.out.println("大数");
System.out.print("大数!!");//if后没有大括号不受影响
*/
//使用大括号是最常用的而且清晰的表达方式
if(a>3) {
System.out.println("大数");
}
}
例子:2:
double i = 6*Math.random();
double j = 6*Math random();
double k = 6*Math random();
int count = 1+(int)(i+j+k);
if(count>15){
System.out.println("今天手气不错");
}
if(count>=10){
System.out.println("今天手气还可以");
}
if(count<10){
System.out.println("今天手气一般");
}
System.out.println("得了"+country+"分");
二、双选择结构if-else
public class TestIf {
public static void main(String[] args) {
double a = Math.random();
int b = 1+(int)a*5;//骰子1-6整数
System.out.println(b);
/*if(a>3)
System.out.println("大数");
System.out.print("大数!!");//if后没有大括号不受影响
*/
//使用大括号是最常用的而且清晰的表达方式
if(a>3) {
System.out.println("大数");
}else {
System.out.println("小数");
}
}
三、多选择结构if-else if-else
double i = 6*Math.random();
double j = 6*Math.random();
double k = 6*Math.random();
int count = 1+(int)(i+j+k);
if(count>15){
System.out.println("今天手气不错");
}else if(count>=10){
System.out.println("今天手气还可以");
}else {
System.out.println("今天手气一般");
}
System.out.println("得了"+count+"分");
多重if-else的使用
public static void main(String[] args) {
double a = Math.random();
int b = 1 + (int)(6*a);
/*if(b>3) {
System.out.println("大数");
}else {
System.out.println("小数");
}
*/
for(int c=0;c<=99;c++) {
double i = 6*Math.random();
double j = 6*Math.random();
double k = 6*Math.random();
int count = 3+(int)(i)+(int)(j)+(int)(k);
if(c==0) {
if(count>=10) {
System.out.print(count+" ");
}else {
System.out.print(count+" ");
}
}else{
if(c%5==0) {
System.out.println();
}
if(count>=10) {
System.out.print(+count+" ");
}else {
System.out.print(count+" ");
}
}
}
}