day06
练习
demo01
package day_06;
import java.time.LocalDate;
import java.util.Scanner;
public class day06_demo01{
// 用户购买机票时,机票原价会按照淡季、旺季,头等舱还是经济舱的情况进行相应的优惠,优惠方案如下:
// 5-10月为旺季,头等舱9折,经济舱8.5折; 11月到来年4月为淡季,头等舱7折,经济舱6.5折,
// 请开发程序计算出用户当前机票的优惠价。
public static double discount( String Class, float price) {//economy class/first class
LocalDate date = LocalDate.now();
System.out.println("当前时间为:"+date);
int month = date.getMonthValue();//month保存当前月份
if (Class.equals("经济舱")) {
if (month >= 5 && month <= 10) {
return 0.85 * price;
} else {
return 0.65 * price;
}
} else if (Class.equals("头等舱")) {
if (month >= 5 && month <= 10) {
return 0.9 * price;
} else {
return 0.7 * price;
}
}
return -1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你要买的舱位等级(头等舱/经济舱):");
String level = scanner.next();
System.out.print("请输入当前机票价格:");
float price =scanner.nextFloat();
System.out.println("当前机票的优惠价为:"+discount(level,price));
}
}
demo02
package day_06;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class day06_demo02 {
// 开发一个程序,可以生成指定位数的验证码,每位可以是数字、大小写字母。
public static String[] verify(int n){
Random random = new Random();
String[] arr= new String[n];
for (int i = 0; i< arr.length;i++){
switch (random.nextInt(2)){
case 0:
arr[i] = String.valueOf(random.nextInt(10));
break;
case 1:
if (random.nextBoolean()){
arr[i] = String.valueOf((char)(random.nextInt(26)+66));
}else{
arr[i] = String.valueOf((char)(random.nextInt(26)+98));
}
break;
}
}
System.out.print("随机验证码为:");
for (int i = 0 ;i <arr.length;i++){
System.out.print(arr[i]);
}
// System.out.println(Arrays.toString(arr));
return arr;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要创建验证码的位数:");
verify(scanner.nextInt());
}
}
demo03
package day_06;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class day06_demo03 {
// 在唱歌比赛中,可能有多名评委要给选手打分,分数是[0 - 100]之间的整数。
// 选手最后得分为:去掉最高分、最低分后剩余分数的平均分,请编写程序能够录入多名评委的分数,并算出选手的最终得分。
public static void getScore(int[] score) {
int max = score[0];
int min = score[0];
int sum = 0;
for (int i = 0; i < score.length; i++) {
max = max > score[i] ? max : score[i];
min = min < score[i] ? min : score[i];
}
for (int i = 0; i < score.length; i++) {
sum += score[i];
}
System.out.println("最终得分是:"+(sum-(max+min))/(score.length-2));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入评委人数:");
int n = scanner.nextInt();
int[] arr= new int[n];
Random random = new Random();
for (int i = 0;i< arr.length;i++){
arr[i] = random.nextInt(100);
}
System.out.println(Arrays.toString(arr));
getScore(arr);
}
}
demo04
package day_06;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class day06_demo04 {
// 某系统的数字密码是一个四位数,如1983,为了安全,需要加密后再传输,加密规则是:
// 对密码中的每位数,都加5 ,再对10求余,最后将所有数字顺序反转,得到一串加密后的新数,请设计出满足本需求的加密程序!
public static void encode(int[] arr){
for (int i =0;i<arr.length;i++){
arr[i] = (arr[i]+5)%10;
}
System.out.println("+5%10后:"+ Arrays.toString(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;
}
System.out.println("翻转后:"+ Arrays.toString(arr));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入密码位数:");
int n = scanner.nextInt();
int[] arr= new int[n];
Random random = new Random();
for (int i = 0;i< arr.length;i++){
arr[i] = random.nextInt(10);
}
System.out.println(Arrays.toString(arr));
encode(arr);
}
}
demo05
package day_06;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class day06_demo05 {
// 请把一个整型数组,例如存了数据:11,22,33,拷贝成一个一模一样的新数组出来。
public static int[] arrCopy(int[] arr){
int[] aCopyArr = new int[arr.length];
for (int i = 0;i<arr.length;i++){
aCopyArr[i] = arr[i];
}
return aCopyArr;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数组位数:");
int[] arr= new int[scanner.nextInt()];
Random random = new Random();
for (int i = 0;i< arr.length;i++){
arr[i] = random.nextInt(10);
}
System.out.println("原始数组为:"+Arrays.toString(arr));
// int[]aCopyArr = new int[arr.length];
int[]aCopyArr = arrCopy(arr);
System.out.println("拷贝数组为:"+Arrays.toString(aCopyArr));
}
}
demo06
package day_06;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class day06_demo06 {
/*一个大V直播时发起了抢红包活动,分别有:9、666、188、520、99999五个红包。
请模拟粉丝来抽奖,按照先来先得,随机抽取,抽完即止,注意:一个红包只能被抽一次,先抽或后抽哪一个红包是随机的,
示例如下(不一定是下面的顺序):
*/
public static void pullRedPacket(int[] arr) {//必中
int length = arr.length;
int index = 0;
Scanner scanner = new Scanner(System.in);
Random random = new Random();
while (length != 0){
System.out.print("请按任意键完成抽奖:");
String s = scanner.next();
if (arr.length == 1){
System.out.println("恭喜你,抽中了:"+arr[0]);
System.out.println("活动结束。。。。。");
return;
}
index = random.nextInt(arr.length-1);
while (arr[index] == 0){
index = random.nextInt(arr.length-1);
}
System.out.println("恭喜你,抽中了:"+arr[index]);
arr[index] = arr[arr.length-1];
arr[arr.length-1] = 0;
length--;
}
System.out.println("活动结束。。。。。");
}
public static void pull(int[] arr) {//有几率中
Scanner scanner = new Scanner(System.in);
Random random = new Random();
for (int i = 0;i<arr.length;i++){
System.out.print("请按任意键完成抽奖:");
String s = scanner.next();
int index = random.nextInt(arr.length);
if (arr[index] == 0){
System.out.println("该红包已经被抽完了");
i--;
}else{
System.out.println("抽中了"+arr[index]+"元");
arr[index] = 0;
}
}
System.out.println("活动结束。。。。。");
}
public static int[] makePacket(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入红包个数:");
int[] arr= new int[scanner.nextInt()];
Random random = new Random();
for (int i = 0;i< arr.length;i++){
arr[i] = random.nextInt(5)+1;
}
System.out.println("红包为:"+ Arrays.toString(arr));
return arr;
}
public static void main(String[] args) {
pull(makePacket());
}
}
demo07
package day_06;
public class day06_demo07 {
// 判断101-200之间有多少个素数,并输出所有素数
// 说明:除了1和它本身以外,不能被其他正整数整除,就叫素数。
// 比如:3、7就是素数,而9、21等等不是素数。
public static int judgePrimeNumber(int min,int max) {
int count = max - min +1;
boolean flag = true;
for (int i = min ;i <max+1;i++){
System.out.println(min+"到"+max+"之间的素数:");
for (int j = 2;j<i/2;j++){
if (i%j==0){//如果不是素数,则count--
count --;
flag = false;
break;
}else {
flag = true;
}
}
if (flag){
System.out.println(i);
}
}
System.out.println(min+"到"+max+"素数共有:"+count+"个");
return count;
}
public static void main(String[] args) {
System.out.println("素数个数:"+judgePrimeNumber(101,200));
}
}
demo08
package day_06;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class day06_demo08 {
// 模拟双色球:用户投注一组号码
// 6个红球号码的范围是1-33之间,且不能重复;1个蓝球号码的范围在:1-16之间。
public static void main(String[] args) {
int[] userNumbers = userSelectNumbers();
int[] luckNumbers = createLuckNumbers();
judge(userNumbers,luckNumbers);
}
// - 6个红球+1个蓝球 ,奖金1000万
//- 6个红球+0个蓝球,奖金500万
//- 5个红球+1个蓝球,奖金3000块
//- 5个红球+0个蓝球,或者4个红球+1个蓝球,奖金200块
//- 4个红球+0个蓝球,或者3个红球+1个蓝球,奖金10块
//- 小于3个红球+1个蓝球,奖金5块
//- 如果前面的都不成立,就中奖,算你为福利事业做贡献了。
//用户投注一组号码(前6个是红球,最后一个是篮球),并返回用户投注的号码
public static int[] userSelectNumbers() {
int[] arr = new int[7];
int red = 6;//信标
while(red != 0){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一组要投注的红球号码(1-33之间,不能重复):");
label:for (int i = 0; i < arr.length - 1; i++){//循环遍历填充数组
int number = scanner.nextInt();
if (number > 33 || number < 1){//如果超范围,则重置数组和信标
System.out.println("红球号码的范围是1-33之间,请重新输入:");
red = 6;
Arrays.fill(arr,0);
break;
}else {
for (int j = 0;j<= i;j++){//如果有重复数据,则重置数组和信标
if (arr[j] == number){
System.out.println("不能有重复号码!请重新输入:");
red = 6;
Arrays.fill(arr,0);
break label;
}
}
arr[i] = number;
red--;
}
}
}
System.out.println("请输入要投注的蓝球号码:");
int blue = 1;
while (blue!=0){
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if (number>16 || number<0 ){
System.out.println("蓝球号码的范围是1-16之间,请重新输入:");
}else{
arr[arr.length-1]=number;
blue--;
}
}
System.out.println("下注的号码组为: "+Arrays.toString(arr));
return arr;
}
public static int[] createLuckNumbers() {
int[] arr = new int[7];
int red = 6;
int blue = 1;
Random random = new Random();
for (int i = 0;i<red;i++){
arr[i] = random.nextInt(33)+1;
}
arr[arr.length-1] = random.nextInt(16)+1;
System.out.println("开奖的号码组为: "+Arrays.toString(arr));
return arr;
}
// - 6个红球+1个蓝球 ,奖金1000万
//- 6个红球+0个蓝球,奖金500万
//- 5个红球+1个蓝球,奖金3000块
//- 5个红球+0个蓝球,或者4个红球+1个蓝球,奖金200块
//- 4个红球+0个蓝球,或者3个红球+1个蓝球,奖金10块
//- 小于3个红球+1个蓝球,奖金5块
//- 如果前面的都不成立,就中奖,算你为福利事业做贡献了。
public static void judge(int[] userNumbers, int[] LuckNumbers) {
int red = 0;//红球相等个数
if (userNumbers[userNumbers.length-1] == LuckNumbers[LuckNumbers.length-1]){//如果蓝色球一样,再遍历红色球一样的个数
for (int i = 0;i<userNumbers.length-1;i++){
for (int j = 0;j<LuckNumbers.length-1;j++){
if (userNumbers[i]==LuckNumbers[j]){
red++;
}
}
}
switch (red){
case 6:
System.out.println("奖金1000万");
break;
case 5:
System.out.println("奖金3000块");
break;
case 4:
System.out.println("奖金200块");
break;
case 3:
System.out.println("奖金10块");
break;
default:
System.out.println("奖金5块");
}
}else{
for (int i = 0;i<userNumbers.length-1;i++){
for (int j = 0;j<LuckNumbers.length-1;j++){
if (userNumbers[i]==LuckNumbers[j]){
red++;
}
}
}
switch (red){
case 6:
System.out.println("奖金500万");
break;
case 5:
System.out.println("奖金3000块");
break;
case 4:
System.out.println("奖金10块");
break;
default:
System.out.println("你为福利事业做贡献了");
}
}
}
}
标签:arr,java,int,day06,util,import,public
From: https://www.cnblogs.com/xkh-blogs/p/17231085.html