第一节 买飞机票
package com.flowerDance.cases; import java.util.Scanner; public class ticketingSystem { public static void main(String[] args){ //目标:完成买飞机票的价格计算 //请用户输入机票的原价、仓位类型、月份信息 Scanner sc = new Scanner(System.in); System.out.println("请输入机票的原价:"); double price = sc.nextDouble(); System.out.println("请选择仓位类型,1代表头等舱,2代表经济舱:"); int positionType = sc.nextInt(); System.out.println("请输入月份(1-12):"); int month = sc.nextInt(); double ticketPrice = buyTicket(price,positionType,month); if (ticketPrice != -1){ System.out.println("优惠后的票价为:"+ticketPrice); } } public static double buyTicket(double price,int positionType,int month){ if (month < 1 || month >12){ System.out.println("对不起,您输入的月份有问题"); return -1; } //判断旺季还是淡季 if(month >= 5 && month <= 10){ switch (positionType){ case 1://1表示头等舱 price *= 0.9; break; case 2://2表示经济舱 price *= 0.85; break; default: System.out.println("您输入的仓位类型有误~"); price = -1; } }else { switch (positionType) { case 1://1表示头等舱 price *= 0.7; break; case 2://2表示经济舱 price *= 0.65; break; default: System.out.println("您输入的仓位类型有误~"); price = -1; } } return price; } }
总结
1、遇到判断值匹配的是时候选择什么结构实现?
使用switch分支结构实现
2、遇到判断区间范围的时候选择什么结构实现?
使用if分支结构实现
第二节 找素数
package com.flowerDance.cases; public class primeNumber { public static void main(String[] args){ //寻找101-200之间的素数并输出 primeNumber(); } public static void primeNumber(){ //得到102到200之间的数据 int quantity = 0; String primeNumber = ""; for (int i = 101; i < 201; i++) { boolean flag = true;//一开始认为是素数 //判断是否为素数 for (int j = 2; j <= i/2 ; j++) { if (i % j == 0) { flag = false; break; } } if (flag){ quantity += 1; primeNumber += i + " "; } } System.out.println("101-200之间有"+quantity+"个素数"); System.out.println(primeNumber); } }
第三节 开发验证码
package com.flowerDance.cases; import java.util.Random; public class verificationCode { public static void main(String[] args){ //需求:开发验证码 System.out.println(verification(5)); } //开发验证码 public static String verification(int num){ Random random = new Random(); String verfication = ""; for (int i = 0; i < num; i++) { int type = random.nextInt(3);//0 1 2 switch (type){ case 0: //大写字符 A:65--Z:65+25 --> 0-25 +65 verfication += (char) (random.nextInt(26)+65); break; case 1: //小写字符 a:97--Z:97+25 --> 0-25 +97 verfication += (char)(random.nextInt(26)+97); break; case 2: //数字 verfication += random.nextInt(10); break; } } return verfication; } }
第四节 数组元素的复制
package com.flowerDance.cases; public class arrCopy { public static void main(String[] args){ int[] arr = {1,2,3,4}; int[] arrCopy = new int[arr.length]; copy(arr,arrCopy); System.out.println(arr); System.out.println(arrCopy); arrPrint(arr); arrPrint(arrCopy); } public static void arrPrint(int[] arr){ System.out.print("["); for (int i = 0; i < arr.length; i++) { System.out.print(i == arr.length-1 ? arr[i] : arr[i]+"\t"); } System.out.println("]"); } public static void copy(int[] arr,int[] arrCopy){ for (int i = 0; i < arr.length; i++) { arrCopy[i] = arr[i]; } } }
第五节 评委打分
6个评委的分数,去掉最高分和最低分,算平均分
package com.flowerDance.cases; public class scoreSystem { public static void main(String[] args){ int[] score = {1,1,1,1,1,1}; System.out.println(score(score)); } public static double score(int[] grades){ //找出最大数和最小数 //初始化这样是否有问题?? // int max = 0; // int min = 100; int max = grades[0]; int min = grades[0]; //计算得分 double grade = 0; for (int i = 0; i < grades.length; i++) { max = grades[i] > max ? grades[i] : max; min = grades[i] < min ? grades[i] : min; grade += grades[i]; } System.out.println(max); System.out.println(min); grade = (grade - max - min) / (grades.length - 2); return grade; } }
第六节 数字加密
需求:某系统的数字密码:比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上5,再对10求余,最后将所有数字反转,得到一串新数
package com.flowerDance.cases; import java.util.Scanner; public class encryption { public static void main(String[] args){ //需求:数据加密 System.out.println("请输入需要加密的数字个数:"); Scanner sc = new Scanner(System.in); int length = sc.nextInt(); int[] codes = new int[length]; for (int i = 0; i < codes.length; i++) { System.out.println("请输入第"+(i+1)+"个数字:"); codes[i] = sc.nextInt(); } arrPrint(codes); arrPrint(digitalEncryption(codes)); } public static void arrPrint(int[] arr){ System.out.print("["); for (int i = 0; i < arr.length; i++) { System.out.print(i == arr.length-1 ? arr[i] : arr[i]+"\t"); } System.out.println("]"); } public static int[] digitalEncryption(int[] codes){ //对每位数进行加密操作 for (int i = 0; i < codes.length; i++) { codes[i] = (codes[i] + 5) % 10; } //反转数组 for (int i = 0; i < codes.length / 2; i++) { int temp = codes[i]; codes[i] = codes[codes.length-1-i]; codes[codes.length-1-i] = temp; } return codes; } }
第七节 双色球系统
package com.flowerDance.cases; import java.util.Random; import java.util.Scanner; public class twoColorBall { public static void main(String[] args){ //双色球系统 //1、随机6个红球号码(1-33,不能重复),随机一个篮球号码(1-16) int[] luckNumber = creatLuckNumber(); //arrPrint(luckNumber); //2、调用一个方法,让用户输入7个号码 int[] userNumber = userInputNumber(); //arrPrint(userNumber); //判断用户输入的号码和随机产生的号码的相似度 judge(luckNumber,userNumber); } public static void judge(int[] luckNumber,int[] userNumber){ //根据双色球中奖原则,记录红球相同数和蓝球相同数 int redCount = 0; int blueCount = 0; //计算红球相同数 for (int i = 0; i < userNumber.length-1; i++) { for (int i1 = 0; i1 < luckNumber.length-1; i1++) { if(userNumber[i] == luckNumber[i1]){ redCount += 1; break; } } } //计算篮球相同数 /*if (userNumber[userNumber.length-1] == luckNumber[luckNumber.length-1]) blueCount += 1;*/ blueCount = userNumber[6] == luckNumber[6] ? 1 : 0; System.out.println("本次幸运号码为:"); arrPrint(luckNumber); System.out.println("用户选择的号码为:"); arrPrint(userNumber); System.out.println("红球中奖个数为"+ redCount); System.out.println("篮球中奖个数为"+ blueCount); //判定得了几等奖 switch (redCount){ case 0: if(blueCount == 1) System.out.println("恭喜中了六等奖,获得奖金5元"); else System.out.println("未获奖----继续努力---"); break; case 1: if(blueCount == 1) System.out.println("恭喜中了六等奖,获得奖金5元"); else System.out.println("未获奖----继续努力---"); break; case 2: if(blueCount == 1) System.out.println("恭喜中了五等奖,获得奖金10元"); else System.out.println("未获奖----继续努力---"); break; case 3: if(blueCount == 1) System.out.println("恭喜中了五等奖,获得奖金10元"); else System.out.println("未获奖----继续努力---"); break; case 4: if(blueCount == 0) System.out.println("恭喜中了五等奖,获得奖金10元"); else if (blueCount == 1) System.out.println("恭喜中了四等奖,获得奖金200元"); break; case 5: if(blueCount == 0) System.out.println("恭喜中了四等奖,获得奖金200元"); else if (blueCount == 1) System.out.println("恭喜中了三等奖,获得奖金3000元"); break; case 6: if(blueCount == 0) System.out.println("恭喜中了二等奖,获得奖金500万元"); else if (blueCount == 1) System.out.println("恭喜中了一等奖,获得奖金1000万元"); break; default: System.out.println("未获奖----继续努力---"); } } public static int[] userInputNumber(){ //定义数组存储用户输入的号码 int[] userNumber = new int[7]; //用户输入号码 Scanner sc = new Scanner(System.in); //输入前6个号码 for (int i = 0; i < userNumber.length-1; i++) { System.out.println("请输入第"+(i+1)+"个号码(1-33,要求不重复):"); //要判断该号码有没有和前面重复,是否在范围内 while (true){ int num = sc.nextInt(); boolean flag = true; //验证是否在范围内 if(num <= 33 && num >=1){ //验证是否与前面重复 for (int j = 0; j < i; j++) { if (num == userNumber[j]) { System.out.println("号码重复,请重新输入:"); flag = false; break; } } }else { System.out.println("号码输入不在范围内,请重新输入:"); flag = false; } if (flag){ userNumber[i] = num; break; } } } //输入第7个号码 System.out.println("请输入第"+userNumber.length+"个号码(1-16):"); //要判断是否在范围内 while (true){ int num = sc.nextInt(); if(num <= 16 && num >=1){ userNumber[userNumber.length-1] = num; break; }else { System.out.println("号码输入不在范围内,请重新输入:"); } } return userNumber; } public static int[] creatLuckNumber(){ //1、随机6个红球号码(1-33,不能重复),随机一个篮球号码(1-16) int[] luckNumber = new int[7];//{12,13,14,0,0,0,0} // 0 1 2 3 4 5 6 //生成7位数字的号码 Random r = new Random(); for (int i = 0; i < luckNumber.length-1; i++) { while (true) {//随机一个数字直到不重复为止 //1.产生一个随机数 int num = r.nextInt(33) + 1; //2.将新产生的随机数和之前的随机数进行比较看是否相等 boolean flag = true; for (int j = 0; j < i; j++) { if (num == luckNumber[j]) { flag = false; break; } } if (flag) { luckNumber[i] = num; break; } } } //产生蓝球号码 luckNumber[luckNumber.length-1] = r.nextInt(16)+1; return luckNumber; } public static void arrPrint(int[] arr){ System.out.print("["); for (int i = 0; i < arr.length; i++) { System.out.print(i == arr.length-1 ? arr[i] : arr[i]+"\t"); } System.out.println("]"); } }
标签:JAVA,23,--,System,int,length,println,public,out From: https://www.cnblogs.com/Flower--Dance/p/16614938.html