代码改变世界

day07作业

2018-07-18 22:45  好好学习哟  阅读(238)  评论(0编辑  收藏  举报

操作1 数组遍历

class ArrayDemo3 {

                public static void main(String[] args) {

                                //数组的遍历

                                int[] arr = {11,22,33,44,55,66};    

                                print(arr);           

                }

                public static void print(int[] arr){

for (int i = 0;i < arr.length ;i++ ) {

                                                System.out.println(arr[i]);

                                }

                }

 

}

 

操作2 取最值

class ArrayDemo4 {

                public static void main(String[] args) {

                                //数组获取最值

                                int[] arr = {33,22,331,134,55,77};

                                int max = getMax(arr);

                                System.out.println(max);

                               

                }

               

                public static int getMax(int[] arr){

                                int max = arr[0];

                                for (int i = 1;i < arr.length ;i++ ) {

                                                if (arr[i] > max) {

                                                                max = arr[i];

                                                }

                                }

                                return max;

                }

}

 

操作3 数组元素反转

class ArrayDemo5 {

                public static void main(String[] args) {

                                int[] arr = {23,34,45,56,67,78,89};

                                reverseArr(arr);

                                print(arr);

                }

 

                public static void reverseArr(int[] arr) {

                                for (int i = 0;i < (arr.length / 2) ;i++ ) {

                                                int temp = arr[i];

                                                arr[i] = arr[arr.length- 1 - i];

                                                arr[arr.length -1 - i] = temp;

                                }

                }

 

                public static void print(int[] arr) {

                                for (int i = 0;i < arr.length ;i++ ) {

                                                System.out.println(arr[i]);

 

 

                                }

                }

}

 

操作4 数组查表法

import java.util.Scanner;

class ArrayDemo6 {

                public static void main(String[] args) {

                                Scanner sc = new Scanner(System.in);

                                System.out.println("输入一个1-7的星期");

                                int index = sc.nextInt();

                               

                                System.out.println("星期" + getWeek(index));

                }

               

                public static char getWeek(int week) {

                                char[] arr = {'一','二','三','四','五','六','七'};

                                return arr[week - 1];

                }

 

}

 

操作5数组元素查找(查找指定元素第一次在数组中出现的索引)

class ArrayDemo7 {

                public static void main(String[] args) {

                                //数组元素查找(查找指定元素第一次在数组中出现的索引)

                                int[] arr = {11,23,34,45,54,34};

                                int index = getIndex(arr,34);

                                System.out.println("index = " + index);

 

                }

 

                public static int getIndex(int[] arr,int value) {

                                for (int i = 0;i < arr.length ;i++ ) {

                                                if (arr[i] == value) {

                                                                return i;

                                                }

                                }

                                return -1;

                }

 

}

 

import java.util.Scanner;

class doubleColor {

                public static void main(String[] args) {

                                int totalMoney = 0; //下注总费用

                                int price = 2;                       //单注金额

                                int count = 0;                      //购买的注数

                                boolean isBuy = false;//标记是否购买

                                int[] num = null;                //定义个数组存储双色球号码

                                Scanner input = new Scanner(System.in);

                                do {

                                                System.out.println();

                                                System.out.println("*****欢迎进入双色球彩票系统*****");

                                                System.out.println("\t1.购买彩票");

                                                System.out.println("\t2.查看开奖");

                                                System.out.println("\t3.退出");

                                                System.out.println("********************************");

                                                System.out.println("请选择菜单:");

 

                                                int choice = input.nextInt();

                                                switch (choice) {

                                                case 1:

                                                                //购买彩票

                                                                System.out.println("[双色球彩票系统 > 购买彩票]");

                                                                System.out.println("您需要下多少注");

                                                                count = input.nextInt();//买了多少注

                                                                totalMoney = count * price;//总下注钱数

 

                                                                //开始选号(6个红色球1个蓝色球)

                                                                num = new int[7];//用来存储双色球号->6红1蓝

                                                                for (int i = 0;i < num.length ;i++ ) {

                                                                                int red;

                                                                                int blue;

                                                                                if (i < num.length - 1) {

                                                                                                System.out.println("请输入6个红色球号(数字为1-33),第" + (i+1) + "个红色球号为:");

                                                                                                red = input.nextInt(33);

                                                                                                num[i] = red;

                                                                                }else {

                                                                                                System.out.println("请输入一个蓝色球号(数字为1-16)");

                                                                                                blue = input.nextInt(16);

                                                                                                num[i] = blue;

                                                                                }

                                                                }

                                                                //输出买好的彩票信息

                                                                System.out.println("您一共买了" + count + "注,共需支付" + totalMoney + "元,所选号码为:");

                                                                for (int i = 0;i < num.length ;i++ ) {

                                                                                System.out.print(num[i]+"\t");

                                                                }

                                                                System.out.println();

                                                                isBuy = true;

                                                                break;

                                                case 2:

                                                                System.out.println("[双色球彩票系统>查看开奖]");

                                                                if (isBuy) {//判断是否购买了彩票

                                                                                isBuy = false;

                                                                                //调用getLuckNum方法得到一个随机中奖号

                                                                                int luckNum[] = getLuckNum();

                                                                                //输出你自己买的彩票号

                                                                                System.out.print("已买的彩票号为:");

                                                                                for (int n:num) {

                                                                                                System.out.print(n + "\t");

                                                                                }

                                                                                System.out.println();

 

                                                                                //输出当期的中奖号码

                                                                                System.out.println("当期的中奖号为:");

                                                                                for(int n:luckNum){

                                                                                                System.out.print(n + "\t");

                                                                                }

                                                                                System.out.println();

                                                                               

                                                                                //判断获奖等级

                                                                                int result = getCompareResult(num,luckNum);

                                                                                //根据获奖等级输出结果

                                                                                if (result == 1) {

                                                                                           System.out.println("【一等奖】,恭喜你!!共下了【" + count + "】注,投资【" + totalMoney + "】元,获奖【" + (500 *count)+"】万元");

                                                                                }else if (result == 2) {

                                                                                           System.out.println("【二等奖】,恭喜你!!共下了【" + count + "】注,投资【" + totalMoney + "】元,获奖【" + (400 *count)+"】万元");

                                                                                }else if (result == 3) {

                                                                                           System.out.println("【三等奖】,恭喜你!!共下了【" + count + "】注,投资【" + totalMoney + "】元,获奖【" + (300 *count)+"】万元");

                                                                                }else if (result == 4) {

                                                                                           System.out.println("【四等奖】,恭喜你!!共下了【" + count + "】注,投资【" + totalMoney + "】元,获奖【" + (200 *count)+"】万元");

                                                                                }else if (result == 5) {

                                                                                           System.out.println("【五等奖】,恭喜你!!共下了【" + count + "】注,投资【" + totalMoney + "】元,获奖【" + (100 *count)+"】万元");

                                                                                }else if (result == 6) {

                                                                                           System.out.println("【六等奖】,恭喜你!!共下了【" + count + "】注,投资【" + totalMoney + "】元,获奖【" + (5 *count)+"】万元");

                                                                                }else {

                                                                                                System.out.println("【望再接再厉】,sorry!!共下了【" + count + "】注,投资【" + totalMoney + "】元,获奖【" + (0 *count)+"】万元");

                                                                                }

                                                                }else {

                                                                                System.out.println("请先购买彩票,再查看开奖情况");

                                                                }

                                                                break;

                                                case 3:

                                                                System.out.println("谢谢使用");

                                                                return;

                                                default:

                                                                System.out.println("输入错误");

                                                }

                                }while (true);

                               

 

                }

 

                //随机生成一个中奖号码

                public static int[] getLuckNum(){

                                int luckNum[] = new int[7];

                                for (int i = 0;i < luckNum.length ;i++ ) {

                                                if (i < luckNum.length - 1) {//红球

                                                                luckNum[i] = (int)(Math.random() * 33) + 1;//生成一个1-33的随机数

                                                }else {//蓝色球

                                                                luckNum[i] = (int)(Math.random() * 16) + 1;//生成一个1-16的随机数

                                                }

                                }

                                return luckNum;

                }

 

                //计算获奖等级

                public static int getCompareResult(int num[],int luckNum[]) {

                                //查看自己中奖情况

                                int luckLevel = 0;//中奖等级1-6

                                int redEqualCount = 0;//红球相等的数量

                                int blueEqualCount = 0;//蓝色球相等的数量

 

                                //将自己的号码与开奖号码进行比较

                                for (int i = 0;i < num.length ;i++ ) {

                                                if (i < num.length - 1) {

                                                                //比较红色球

                                                                int r = num[i];//每遍历一次将自己的一个红球号与中奖的所有号码进行比较

                                                                for (int j = 0;j < luckNum.length - 1 ;j++ ) {

                                                                                if (r == luckNum[j]) {

                                                                                                redEqualCount++;

                                                                                }

                                                                }

                                                }else {//比较蓝色球

                                                                if (num[num.length - 1] == luckNum[luckNum.length - 1]) {

                                                                                blueEqualCount++;

                                                                }

                                                }

                                }

                                //得出结果

                                if (redEqualCount == 6 && blueEqualCount == 1) {

                                                luckLevel = 1;

                                }else if (redEqualCount == 6) {

                                                luckLevel = 2;

                                }else if (redEqualCount == 5 && blueEqualCount == 1) {

                                                luckLevel = 3;

                                }else if (redEqualCount == 5 || (redEqualCount == 4 && blueEqualCount == 1)) {//四等奖

                                                luckLevel = 4;

                                }else if (redEqualCount == 4 || (redEqualCount == 3 && blueEqualCount == 1)) {//括号

                                                luckLevel = 5;

                                }else if (blueEqualCount == 1) {

                                                luckLevel = 6;

                                }

                                return luckLevel;

               

                }

}