目录
编程思维
使用所学的
Java
技术解决问题的思维方式和编写代码实现出来的能力
关于提升编程思维和编程能力的建议
编程思维和编程能力不是一朝一夕形成的,需要时间的沉淀和大量练习。
具体措施:勤于练习代码,勤于思考,孰能生巧。
前期:先模仿,后期:再创新
1.案例:找素数
100~200之间的素数,并打印素数的个数;
public class Test {
public static void main(String[] args) {
int count=0;
System.out.print("100~200之间的素数:");
for(int i=100;i<=200;i++){
if(isPrimeNumber(i)){
System.out.print(i+" ");
count++;
}
}
System.out.println();
System.out.println("素数的个数:"+count);
}
//判断是否为素数
public static boolean isPrimeNumber(int x){
if(x==2){
return true;
}
for(int i=2;i<Math.sqrt(x);i++){
if(x%i==0){
return false;
}
}
return true;
}
}
2.案例:开发验证码
public class Demo {
public static void main(String[] args) {
System.out.println(password(5));
}
//随机生成一个n位数的密码
public static String password(int n){
Random random=new Random();
int index;
String str="";
for (int i = 0; i < n; i++) {
char ch;
//index值为0代表生成一个0~9的数字,1代表生成一个大写字母,2代表生成一个小写字母
index=random.nextInt(3);
switch (index){
case 0:
str+=random.nextInt(10);
break;
case 1:
ch=(char)(random.nextInt(26)+65);
str+=ch;
break;
case 2:
ch=(char)(random.nextInt(26)+97);
str+=ch;
break;
}
}
return str;
}
}
3.案例:数组元素复制
需求:
把一个数组中的元素复制到另一个新数组中去。
分析:
需要动态初始化一个数组,长度与原数组一样。
遍历原数组的每个元素,依次赋值给新数组。
输出两个数组的内容。
public class Demo02 {
public static void main(String[] args) {
int[] arr1={1,3,4,5,62,6};
int[] arr2=new int[arr1.length];
// 方法一
for (int i = 0; i < arr1.length; i++) {
arr2[i]=arr1[i];
}
System.out.println("arr2:"+Arrays.toString(arr2));
// 方法二
int[] arr3=new int[arr1.length];
System.arraycopy(arr1,0,arr3,0,arr1.length-1);
System.out.println("arr3:"+Arrays.toString(arr3));
}
}
4.案例:评委打分案例
需求 :
在唱歌比赛中,有6名评委给选手打分,分数范围是[0 - 100]之间的整数。选手的最后得分为:去掉最 高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。
分析:
① 把6个评委的分数录入到程序中去 ----> 使用数组
② 遍历数组中每个数据,进行累加求和,并找出最高分、最低分。
③ 按照分数的计算规则算出平均分。
public class Demo03 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入六位评委打的分数:");
String str=scanner.nextLine();
String[] strArray=str.split(" ");
double sum=0;
int max=0;
int min=0;
for (int i = 0; i < strArray.length; i++) {
int num=Integer.parseInt(strArray[i]);
min=num;
sum+=num;
max=Math.max(max,num);
min=Math.min(min,num);
}
//减去最高分和最低分
sum=sum-max-min;
double avg=sum/(strArray.length-2);
System.out.println("该选手的平均分:"+avg);
}
}
5.案例:数字加密
需求:
某系统的数字密码,比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上 5 , 再对10求余,最后将所有数字反转,得到一串新数。
6.抢红包
分析
将每位数据存入到数组中去,遍历数组每位数据按照规则进行更改,把更改后的数据从新存入到数组中。
将数组的前后元素进行交换,数组中的最终元素就是加密后的结果。
public class Demo04 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一串数字密码:");
String s=scanner.nextLine();
System.out.println("加密后:"+encryption(s));
}
public static String encryption(String s){
String str="";
int num=Integer.parseInt(s);
int mod=10;
int div=1;
for(int i=0;i<s.length();i++,mod*=10,div*=10){
str+=((num%mod/div+5)%10);
}
return str;
}
}
需求:
一个大V直播抽奖,奖品是现金红包,分别有{2, 588 , 888, 1000, 10000}五个奖金。请使用代码模拟抽奖, 打印出每个奖项,奖项的出现顺序要随机且不重复。打印效果如下:(随机顺序,不一定是下面的顺序)
888 元的奖金被抽出588 元的奖金被抽出10000 元的奖金被抽出1000 元的奖金被抽出2 元的奖金被抽出
分析
定义一个数组用于存储这些奖金金额。
定义一个数组用于记录已经抽到的金额。
每次抽奖都随机一个索引,取出索引对应的奖金金额,判断该金额之前是否抽中过,抽中过从新抽一次
。
public class Demo05 {
public static void main(String[] args) {
int[] rewords={2,588,888,1000,10000};
int[] exists=new int[rewords.length];
// 用来记录已抽中元素个数
int count=0;
Random random=new Random();
int index=-1;
while (true){
index=random.nextInt(5);
if(isExists(exists,rewords[index])){
System.out.println(rewords[index]+"元的奖品已经被抽出");
continue;
}
exists[count]=rewords[count++];
System.out.println("恭喜你!抽中了"+exists[count-1]+"元的奖品");
if(count==5){
break;
}
}
}
// 判断x元素是否之前被抽中
public static boolean isExists(int[] exists,int x){
for (int i = 0; i < exists.length; i++) {
if(exists[i]==x){
return true;
}
}
return false;
}
}
活动地址:CSDN21天学习挑战赛