设计编程摇色子的游戏 , 有三个色子可以摇 ; 分别用 if 和 switch 语句 实现总体点数为 15及以下没奖励 16 三等奖 17 二等奖 18 一等奖
if语句实现
import java.util.Scanner;
/**
* 设计编程摇色子的游戏 , 有三个色子可以摇 ; 分别用 if 和 switch 语句实现
总体点数为 15及以下没奖励
16 三等奖
17 二等奖
18 一等奖
* @author Amanda
*
*/
public class GameSwitch {
public static void main(String[] args){
System.out.println("摇色子游戏开始 || Game Star!");
Scanner sc = new Scanner(System.in);
System.out.println("输入数字1开始游戏,输入2离开系统");
int next = sc.nextInt();
if(next == 2)
return;//技术当前方法 剩余内容不再执行
int i = (int) (Math.random() * 6) + 1;
int j = (int) (Math.random() * 6) + 1;
int k = (int) (Math.random() * 6) + 1;
int sum = i + j + k;
System.out.println(i + "," + j + "," + k + "," + sum);
//简化 算数
sum = sum /16 == 0 ? 0 : sum % 15;
if(sum == 3){
System.out.println("恭喜获得一等奖!!!");
}else if(sum == 2){
System.out.println("恭喜获取二等奖!");
}else if(sum == 1){
System.out.println("恭喜获取三等奖!");
}else{
System.out.println("与奖金擦身而过,请再接再厉!");
}
}
}
Switch语句实现
import java.util.Scanner;
public class GameSwitch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("摇色子游戏开始 || Game Star!");
Scanner sc = new Scanner(System.in);
System.out.println("输入数字1开始游戏,输入2离开系统");
int next = sc.nextInt();
if(next == 2)
return;//技术当前方法 剩余内容不再执行
int i = (int) (Math.random() * 6) + 1;
int j = (int) (Math.random() * 6) + 1;
int k = (int) (Math.random() * 6) + 1;
int sum = i + j + k;
System.out.println(i + "," + j + "," + k + "," + sum);
//简化 算数
sum = sum /16 == 0 ? 0 : sum % 15;
switch(sum){
case 0: System.out.println("与奖金擦身而过,请再接再厉!");break;
case 1: System.out.println("恭喜获取三等奖!");break;
case 2: System.out.println("恭喜获取二等奖!");break;
case 3: System.out.println("恭喜获取一等奖!");break;
}
}
}