day06作业-韩明琰
1. 数组的5个基本操作(编写代码)
一,数组遍历
package com.zkch.test;
public class Demo9 {
public static void main(String[] args) {
int[] arr = {5,85,63,52,5};
print(arr);
}
public static void print(int[] arr) {
for(int i = 0;i < arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
}
二,获取最值
package com.zkch.test;
public class Demo10 {
public static void main(String[] args) {
int[] arr = {9,5,2,7,10,68};
System.out.println("最大值为:"+getMax(arr));
System.out.println("最小值为:"+getMin(arr));
}
public static int getMax(int[] arr) {
int max = arr[0];
for(int i = 1;i < arr.length;i++) {
max = max > arr[i] ? max : arr[i];
}
return max;
}
public static int getMin(int[] arr) {
int min = arr[0];
for(int i = 1;i < arr.length;i++) {
min = min < arr[i] ? min : arr[i];
}
return min;
}
}
三,元素反转
package com.zkch.test;
public class Demo12 {
public static void main(String[] args) {
int[] arr = {52,36,29,46,25};
print(arr);
System.out.println("\n"+"===============");
reverseArray(arr);
print(arr);
}
public static void reverseArray(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.print(arr[i]+" ");
}
}
}
四,查表法
package com.zkch.test;
import java.util.Scanner;
public class Ddemo13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数(1到7)");
int week = sc.nextInt();
System.out.println("星期"+getWeek(week));
}
public static char getWeek(int week) {
char[] arr = {' ','一','二','三','四','五','六','七'};
return arr[week];
}
}
五,基本查找
package com.zkch.test;
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {25,8,5,69,47,21,25};
System.out.println("请输入一个值:");
int value = sc.nextInt();
System.out.println("该值在数组中的索引为:"+getIndex(arr,value));
}
public static int getIndex(int[] arr,int value) {
for(int i = 0;i < arr.length;i++) {
if(arr[i]==value) {
return i;
}
}
return -1;
}
}
package com.zuikc.test;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean judge = false;
int[] lottery = new int[7];
int count = 0;
int money = 0;
do {
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("购买彩票");
play(lottery);
count++;
System.out.println("继续(true)结束(false)");
judge = input.nextBoolean();
break;
case 2:
System.out.println("查看开奖");
award(lottery,money);
System.out.println("您一共下注人民币"+(2*count)+"元,累计中奖人民币"+money+"元");
System.out.println("继续(true)结束(false)");
judge = input.nextBoolean();
break;
case 3:
System.out.println("谢谢使用");
break;
default:
System.out.println("输入有误");
System.out.println("继续(true)结束(false)");
judge = input.nextBoolean();
break;
}
}while(judge);
}
public static void play(int[] lottery) {
Scanner sc = new Scanner(System.in);
for(int i = 0;i<lottery.length-1;i++) {
System.out.println("请输入第"+(i+1)+"个红球的号码(1到33):");
lottery[i] = sc.nextInt();
}
System.out.println("请输入蓝球的号码(1到16):");
lottery[6] = sc.nextInt();
System.out.print("您购买的号码为:\n红球:");
for(int i = 0;i<6;i++) {
System.out.print(lottery[i]+" ");
}
System.out.println("\n蓝球:"+lottery[6]);
}
public static void award(int[] lottery,int money) {
int[] red = new int[33];
for(int i = 0;i<33;i++) {
red[i] = i+1;
}
int[] blue = new int[16];
for(int i = 0;i<16;i++) {
blue[i] = i+1;
}
int[] redAward = new int[6];
int i = 0;
a:while(true) {
if(i==6) {
break;
}
int num = red[(int)(Math.random()*33)];
for(int j = 0;j<=i;j++) {
if(redAward[i] == num) {
continue a;
}
}
redAward[i] = num;
i++;
}
int count = 0;
for(int j = 0;j<redAward.length;j++) {
for(int k = 0; k<lottery.length;k++) {
if(lottery[k] == redAward[j]) {
count++;
}
}
}
int blueAward = (int)(Math.random()*16+1);
System.out.print("中奖号码为:\n红球:");
for(int j = 0;j<6;j++) {
System.out.print(redAward[j]+" ");
}
System.out.println("\n蓝球:"+blueAward);
boolean one = blueAward == lottery[lottery.length-1];
if(count == 6 && one) {
System.out.println("恭喜你中了一等奖10万元");
money+=100000;
}else if(count == 6) {
System.out.println("恭喜你中了二等奖5万元");
money+=50000;
}else if(count == 5 && one) {
System.out.println("恭喜你中了三等奖1万元");
money+=10000;
}else if(count == 5 || (count == 4 && one)) {
System.out.println("恭喜你中了四等奖5000元");
money+=5000;
}else if(count == 4 || (count == 3 && one)) {
System.out.println("恭喜你中了五等奖1000元");
money+=1000;
}else if(one) {
System.out.println("恭喜你中了六等奖100元");
money+=100;
}else {
System.out.println("抱歉你没有中奖");
}
}
}