自学Java第七天:编程案例

时间:2024-10-02 15:28:50
  • import ;
  • import .Random;
  • public class Tickets{
  • public static void main(String[] args){
  • Scanner myscanner = new Scanner(System.in);
  • int[] red = new int[6];
  • int[] myred = new int[6];
  • getarr1(red);//得到一个6个数的随机数组
  • Random r = new Random();
  • int n = (16)+1;//得到一个随机数
  • ("输入一个1-16的数:");
  • int myn = ();
  • getarr2(myred);//输入一个6个数的数组
  • int num = Compare(red,myred);//定义中红色球个数
  • boolean flag = (n==myn);
  • ("你的奖金为:"+getmoney(flag,num));
  • ("生成的随机数组为:");
  • output(red);
  • ("\n得到随机数为:"+n);
  • }
  • public static int getmoney(boolean flag,int num){
  • int money = 0;
  • if(flag){
  • switch(num){
  • case 0:
  • case 1:
  • money=5;
  • ("\n恭喜你获得六等奖!");
  • break;
  • case 2:
  • case 3:
  • money=10;
  • ("\n恭喜你获得五等奖!");
  • break;
  • case 4:
  • money=200;
  • ("\n恭喜你获得四等奖!");
  • break;
  • case 5:
  • money=3000;
  • ("\n恭喜你获得三等奖!");
  • break;
  • case 6:
  • money=10000000;
  • ("\n恭喜你获得一等奖!你要暴富啦!!!");
  • break;
  • }
  • }else{
  • switch(num){
  • case 0:
  • case 1:
  • case 2:
  • case 3:
  • money=0;
  • ("\n你就是那个非洲人吧^_^一分钱都没有o~");
  • break;
  • case 4:
  • money=10;
  • ("\n恭喜你获得五等奖!");
  • break;
  • case 5:
  • money=3000;
  • ("\n恭喜你获得三等奖!");
  • break;
  • case 6:
  • money=5000000;
  • ("\n恭喜你获得二等奖!分给我点儿呗~");
  • break;
  • }
  • }
  • return money;
  • }
  • public static void getarr1(int[] arr1){
  • Random r = new Random();
  • for (int i = 0;i < arr1.length ;i++ ){
  • arr1[i] = (33)+1;
  • }
  • }
  • public static void getarr2(int[] arr2){
  • Scanner myscanner = new Scanner(System.in);
  • ("请输入1-33的数。\n");
  • for (int i = 0;i < arr2.length ;i++ ){
  • ("请输入第"+(i+1)+"位:");
  • arr2[i] = ();
  • if (arr2[i]<1 || arr2[i]>33){
  • ("您输入的数据有误!");
  • break;
  • }
  • }
  • }
  • //求红色球相同个数
  • public static int Compare(int[] arr1,int[] arr2){
  • int num = 0;
  • for (int i = 0;i < arr1.length ;i++ ) {
  • if(arr1[i]==arr2[i])
  • num++;
  • }
  • return num;
  • }
  • public static void output(int[] arr){
  • for (int i = 0;i < arr.length ;i++ ){
  • (arr[i]+"\t");
  • }
  • }
  • }