抽奖活动

实例:

import java.util.Random;
public class Test {
	 final static int  num = 4;//设置抽奖号码字符位数为4位
	 
     public static void main(String[] args){
    	 Random rd = new Random();//创建对象
    	 String str = "",s;
    	 
    	 for ( int i = 0 ; i < num ; i++ ){//连续生成四个范围在0到1之间的随机数
               s = rd.nextInt(2)+"";
    		   str = str + randomSelection(s);
    	 }
    	 
    	 System.out.println("你的抽奖号码为"+str);
    	 System.out.print( describe(str));
     }
     
     
     //将随机数转换成对应字符的函数
     public static String randomSelection(String str){
    	 if ( str.equals("1")){
    		 return "★";
    	 }else{
    		 return "☆";
    	 }
     }
     
     
     //输出中奖信息的函数
     public static String describe(String str){
    	 if ( str.equals("★★★★")){
    		 return "恭喜你中了本次活动的一等奖,奖金一个亿";
    	 }else if( str.equals("★★★☆") ){
    		 return "恭喜你中了本次活动的二等奖,奖金八千万";
    	 }else if( str.equals("★★☆☆") ){
    		 return "恭喜你中了本次活动的三等奖,奖金五千万";
    	 }else{
    		 return "欧!你没中奖,谢谢参与!";
    	 }
     } 
}

猜你喜欢

转载自blog.csdn.net/weixin_44365021/article/details/85622130