【Java基础】编程思维训练

个人简介
> 📦个人主页:轻狂客_零度的主页
> 🏆学习方向:JAVA后端开发
> 📣种一棵树最好的时间是十年前,其次是现在!
> ⏰往期文章:【Java基础】数组
> 🧡喜欢的话麻烦点点关注喔,你们的支持是我的最大动力。
前言:
目的:
复习前半段课程学习的Java编程知识,能够使用所学的知识解决问题,提升我们的编程能力。
涉及到的知识点
变量、数组
运算符:基本运算符、关系运算符、逻辑运算符…
程序流程控制:if、switch;for、while;死循环、循环嵌套
跳转关键字:break、continue、return。
方法
…
编程思维
使用所学的 Java 技术解决问题的思维方式和编写代码实现出来的能力。
关于提升编程思维和编程能力的建议 1. 编程思维和编程能力不是一朝一夕形成的,需要时间的沉淀和大量练习。 2. 具体措施:勤于练习代码,勤于思考,孰能生巧。 3. 前期:先模仿,后期:再创新。
活动地址:CSDN21天学习挑战赛
目录
前言:
1. 案例一: 买飞机票
2. 案例二: 找素数
3. 案例三: 开发验证码
4. 案例四: 数组元素的复制
5. 案例五: 评委打分
6. 案例六: 数字加密
7. 案例七: 模拟双色球[拓展]
1. 案例一: 买飞机票
需求:
1). 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。
2). 按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。
分析:
1). 定义一个方法可以进行键盘录入机票原价、月份和机舱类型。
2). 使用if判断月份是是旺季还是淡季,使用switch分支判断是头等舱还是经济舱。
3). 选择对应的折扣进行计算并返回计算的结果。
1. 遇到判断值匹配的时候选则什么结构实现?
使用switch分支结构实现
2. 遇到判断区间范围的时候选则什么结构实现?
使用if 分支结构实现
package com.example.bbb;import java.util.Scanner;public class demo {public static void main(String[] args) {// 1. 在main方法内:// (1.1) 创建一个键盘录入的对象,用于用户录入机票原价、仓位类型、月份信息Scanner sc = new Scanner(System.in);// (1.2) 用户开始录入,并接收录入的机票原价、仓位类型、月份信息System.out.println("请您输入机票原价:");double money = sc.nextDouble();System.out.println("请您输入月份(1-12):");int month = sc.nextInt();System.out.println("请您输入仓位类型:");String type = sc.next();// 3. 在main方法内:// (3.1) 调用方法:将录入的机票原价、仓位类型、月份信息传递给形参,输出最终价格System.out.println("机票优惠后的价格:" + calc(money, type, month));}// 2. 定义机票最终优惠价格的计算方法calc:// (2.1) 设置三个形参:doble money, String type, int month,用于接收用户录入的实参:机票原价、仓位类型、月份信息public static double calc(double money, String type, int month) {// (2.2) 使用if分支判断:if (month >= 5 && month <=10) { // (1) 录入的月份是否 >=5 且 <=10,// 是则判断为旺季// (2.2.1) 使用switch分支判断:switch (type) { // (1) 录入的仓位类型是头等舱还是经济舱// 是头等舱,录入的机票原价 乘以 0.9 等于 优惠9折后的机票价格case "头等舱":money *= 0.9; // 等价于 money = money * 0.9break;// 是经济舱,录入的机票原价 乘以 0.85 等于 优惠8.5折后的机票价格case "经济舱":money *= 0.85; // 等价于 money = money * 0.85break;// 如果都不是,default:// 说明仓位类型录入有误~,System.out.println("您输入的仓位类型有误~");// 无法进行价格计算,表示为 -1money = -1;}} else if (month == 11 || month == 12 || month >= 1 && month <= 4) { // (2) 录入的月份是否 ==11 或 ==12 或 >=1 且 <=4,// 是则判断为淡季// (2.2.2) 使用switch分支判断:switch (type) { // (1) 录入的仓位类型是头等舱还是经济舱// 是头等舱,录入的机票原价 乘以 0.7 等于 优惠7折后的机票价格case "头等舱":money *= 0.7;break;// 是经济舱,录入的机票原价 乘以 0.65 等于 优惠6.5折后的机票价格case "经济舱":money *= 0.65;break;// 如果都不是,default:// 说明仓位类型录入有误~,System.out.println("您输入的仓位类型有误~");// 无法进行价格计算,表示为 -1money = -1;}} else {// (3) 录入的月份小于1 且 大于12时,// 说明录入的月份有误~,System.out.println("您输入月份有误~");// 无法进行价格计算,表示为-1money = -1;}// (2.3) 返回最终价格return money;}
}
2. 案例二: 找素数
题目: 判断101-200之间有多少个素数,并输出所有素数
说明:
素数: 如果除了1和它本身以外,不能被其他正整数整除, 就叫素数
分析:
101-200之间的数据可以采用循环依次拿到; 每拿到一个数,判断该数是否是素数。
判断规则是:从2开始遍历到该数的一半的数据,看是否有数据可以整除它,有则不是素数,没有则是素数。
本次案例中是如何确定出该数是素数的,具体如何实现?
遍历2开始到该数的一半的数据去判断是否有整除它的。package com.example.bbb;import java.lang.Math;
public class demo2 {public static void main(String[] args) {boolean shu = true;int num,count = 0; // num为那两个数字中间;count为计数器for (num=101;num<=200;num++){for (int t = 2;t <= Math.sqrt(num);t++){if (num % t == 0){shu = false;break;}}if (shu == true){count++;System.out.println(num+"为素数");}shu = true;}System.out.println("素数个数为:"+count);}
} 
3. 案例三: 开发验证码
需求:
定义方法实现随机产生一个5位的验证码,每位可能是数字、大写字母、小写字母。
分析:
1. 定义一个方法,生成验证码返回:方法参数是位数、方法的返回值类型是String。
2. 在方法内部使用for循环生成指定位数的随机字符,并连接起来。
3. 把连接好的随机字符作为一组验证码进行返回。
随机验证码的核心实现逻辑是如何进行的?
1). 定义一个String类型的变量存储验证码字符。
2). 定义一个for循环,循环5次。
3). 随机生成0|1|2的数据,依次代表当前位置要生成数字|大写字母|小写字母。
4). 把0、1、2交给switch生成对应类型的随机字符,把字符交给String变量。
5). 循环结束后,返回String类型的变量即是所求的验证码结果。
package com.example.bbb;import java.util.Random;public class demo3 {//目标:随机生成5位验证码(大小写字母和数字)public static void main(String[] args) {//3.引用方法String code=yanzhengma(5);System.out.println("验证码是:"+code);}//1.定义方法,返回验证码public static String yanzhengma(int n){ //n是验证码位数,本题为5//2.随机产生验证码Random r=new Random(); //得到Random类的r对象String code=""; //定义一个字符串,放那5位验证码for (int i = 0; i < n; i++) {int type=r.nextInt(3);//随机在0-2中产生一个数字,这代表的是大写or小写or数字switch (type){case 0: // 0代表是大写字母类型// 大写字母是从 A=65 开始到 Z=65+25,所以随机生成0-25 +65,就能代表任意一个大写字母了char ch1 = (char)(r.nextInt(26)+65); //强制char型code+=ch1;break;case 1: // 1代表是小写字母类型,a=97 z=97+25char ch2 = (char)(r.nextInt(26)+97); //强制char型code+=ch2;break;case 2: // 2代表是数字型int ch3=r.nextInt(10); //0-9code+=ch3;break;}}return code;}
}
4. 案例四: 数组元素的复制
需求:
把一个数组中的元素复制到另一个新数组中去。
分析:
需要动态初始化一个数组,长度与原数组一样。
遍历原数组的每个元素,依次赋值给新数组。
输出两个数组的内容。
数组的拷贝是什么意思?
需要创建新数组,把原来数组的元素赋值过来。
package com.example.bbb;public class demo4 {public static void main(String[] args) {int [] arr1 = {11,22,33,44};int[] arr2 =new int[arr1.length];copy(arr1 , arr2);printArray(arr1);printArray(arr2);}public static void printArray(int[] arr){System.out.print("[");for (int i = 0; i < arr.length; i++) {System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");}System.out.println("]");}public static void copy(int[] arr1, int[] arr2){//正式完成数组的复制for (int i = 0; i < arr1.length ; i++) {arr2[i] = arr1[i];}}
}
5. 案例五: 评委打分
需求 :
在唱歌比赛中,有6名评委给选手打分,分数范围是[0 - 100]之间的整数。选手的最后得分为:去掉最 高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。
分析:
1. 把6个评委的分数录入到程序中去 ----> 使用数组
int[ ] scores = new int [ 6 ];
2. 遍历数组中每个数据,进行累加求和,并找出最高分、最低分。
3. 按照分数的计算规则算出平均分。
如何实现评委打分案例?
1). 定义一个动态初始化的数组用于存储分数数据。
2). 定义三个变量用于保存最大值、最小值和总和。
3). 遍历数组中的每个元素,依次进行统计。
4). 遍历结束后按照规则计算出结果即可。
package com.example.bbb;import java.util.Scanner;public class demo5 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int[] scores = new int[6];for (int i = 0; i < scores.length; i++) {System.out.println("请输入第" + (i + 1) + "个评委的评分:");int score = sc.nextInt();scores[i] = score;}System.out.println(avg(scores));}public static double avg(int[] arry) {for (int i = 0; i < arry.length - 1; i++) {for (int j = 0; j < arry.length - i - 1; j++) {if (arry[j] > arry[j + 1]) {int tem = arry[j + 1];arry[j + 1] = arry[j];arry[j] = tem;}}}int sum = 0;for (int i = 1; i < arry.length - 1; i++) {sum += arry[i];}double avy1 = sum * 1.0 / (arry.length - 2);return avy1;}
}
6. 案例六: 数字加密
需求:
某系统的数字密码,比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上 5 , 再对10求余,最后将所有数字反转,得到一串新数。

分析:
将每位数据存入到数组中去,遍历数组每位数据按照规则进行更改,把更改后的数据从新存入到数组中。
将数组的前后元素进行交换,数组中的最终元素就是加密后的结果。
本次案例中是如何完成数组元素的反转的?
定义两个变量分别占数组的首尾位置。
一个变量往前走,一个变量往后走,同步交换双方位置处的值。
package com.example.bbb;import java.util.Scanner;public class demo6 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入密码位数:");int n = sc.nextInt();int[] password = new int[n];getPassword(password);encryptPassword(password);printEncryptPassword(password);}public static void getPassword(int[] password) {Scanner sc = new Scanner(System.in);System.out.println("请输入密码(以空格分割):");for (int i = 0; i < password.length; i++) {int m = sc.nextInt();password[i] = m;}}public static void encryptPassword(int[] password) {for (int i = 0; i < password.length; i++) {password[i] = (password[i] + 5) % 10;}//反转 index: start end start < end 就交换int startIndex = 0;int endIndex = password.length - 1;int temp;for (int j = 0; j < password.length; j++) {if (startIndex < endIndex) {temp = password[startIndex];password[startIndex] = password[endIndex];password[endIndex] = temp;}startIndex++;endIndex--;}}public static void printEncryptPassword(int[] password) {System.out.print("[");for (int i = 0; i < password.length; i++) {System.out.print(i == password.length - 1 ? password[i] : password[i] + ",");}System.out.println("]");}
}
7. 案例七: 模拟双色球[拓展]
需求:
模拟双色球
业务分析、随机生成一组中奖号码
用户输入一组双色球号码 判断中奖情况 中奖号码由6个红球和1个篮球组成(注意:6个红球要求不能重复)
分析:
createLuckNumbers(){...} 随机生成一组中奖号码返回
可以定义方法用于返回一组中奖号码(7个数据),返回的形式是一个整型数组
userInputNumbers(){...} 用户输入一组号码
定义一个方法,该方法可以录入用户输入的6个红球和1个蓝球号码
该方法最终返回一个数组,数组中就是用户录入的号码(7个)
judge(int[ ] luckNumbers,int[ ] userNumbers){...} 传入两组号码判断用户中奖情况
package com.example.bbb;import java.util.Random;
import java.util.Scanner;public class demo7 {public static void main(String[] args) {//1.随机6个红球号码(1-33,不能重复),随机一个蓝球号码(1-16),可以采用数组装起来作为中奖号码int[] luckNumbers = createLuckNumbers();//2. 调用一个方法让用户输入7个号码,作为用户选号int[] userInputNumbers = userInputNumbers();System.out.println("您购买的号码是:");print(userInputNumbers);System.out.println("中奖号码为:");print(luckNumbers);//3.判断是否中奖judge(luckNumbers, userInputNumbers);}//生成一组中奖号码public static int[] createLuckNumbers() {//a.定义一个动态初始化的数组,存储7个数字int[] numbers = new int[7];//b.遍历数组,为每个位置生成对应号码,遍历到第六个位置,第七个为蓝球号码需要单独生成//(6个红球号码不能重复)Random r = new Random();for (int i = 0; i < numbers.length - 1; i++) {//当不重复的时候再退出死循环while (true) {int data = r.nextInt(33) + 1;//1-33 (0-32)+1//c.判断当前随机生成号码是否出现过,如果出现过需要重新随机一个,知道不重复为止才能存入数组/*** (当前生成的随机数需要与已经存入的做比较是否重复,已经存入的i代表位置) 比如已经存入2个,在存入* 第三个的时候需要判断 0,1 是否重复 相当于 i=3时,j = 1 、2*/boolean flag = true;for (int j = 0; j < i; j++) {//data值与之前的值重复,data需要重新赋值if (numbers[j] == data) {//跳出本次判断重复循环,重新生成一个flag = false;break;}}//如果判断没有重复,说明flag还是true 跳出死循环if (flag) {numbers[i] = data;break;}}}// d.为第七个位置生成(1-16)作为蓝球号码numbers[numbers.length - 1] = r.nextInt(16) + 1;return numbers;}//用户选号public static int[] userInputNumbers() {// a.定义一个数组存储7个号码int[] userNumbers = new int[7];Scanner sc = new Scanner(System.in);// b.让用户输入6个红球号码for (int i = 0; i < userNumbers.length - 1; i++) {System.out.println("请您输入第" + (i + 1) + "个红球号码(1-33)不能重复:");int data = sc.nextInt();//c.把当前录入的数据存入数组userNumbers[i] = data;}System.out.println("请输入第7个蓝球号的号码(1-16):");userNumbers[userNumbers.length - 1] = sc.nextInt();return userNumbers;}//判断中奖情况public static void judge(int[] luckNumbers, int[] userNumbers) {//a.定义两个变量定义红蓝球中的个数int countRed = 0;int countBlue = 0;//b.统计红球猜中个数for (int i = 0; i < luckNumbers.length - 1; i++) {if (luckNumbers[i] == userNumbers[i]) {countRed++;}}//c.统计蓝球猜中没有if (luckNumbers[luckNumbers.length - 1] == userNumbers[userNumbers.length - 1]) {countBlue++;}System.out.println("红球:" + countRed);System.out.println("蓝球:" + countBlue);if (countRed == 6 && countBlue == 1) {System.out.println("恭喜您中了1000万!");} else if (countRed == 6 && countBlue == 0) {System.out.println("恭喜您中了500万!");} else if (countRed == 5 && (countBlue == 1 || countBlue == 0)) {System.out.println("恭喜您中了3000元!");} else if (countRed == 4 && (countBlue == 1 || countBlue == 0)) {System.out.println("恭喜您中了200元!");} else if ((countRed == 2 || countRed == 3) && countBlue == 1) {System.out.println("恭喜您中了10元!");} else if ((countRed == 1 && countBlue == 1) || countBlue == 1) {System.out.println("恭喜您中了5元!");} else {System.out.println("很遗憾!您没有中奖!");}}//打印数组public static void print(int[] numbers) {for (int i = 0; i < numbers.length; i++) {System.out.print(i != numbers.length - 1 ? numbers[i] + " " : numbers[i]+"\n");}}}

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!





