一、目的与编程思维
1、目的:复习前半段学习的学习的java编程知识,能够使用所学的知识解决问题,提升同学们的编程能力。
涉及到的知识点:
- 变量、数组
- 运算符:基本运算符、关系运算符、逻辑运算符
- 程序流程控制:if、switch、for、while、死循环、循环嵌套
- 跳转关键字:break、continue、return
- 方法
- 。。。。
2、编程思维:使用所学的Java技术解决问题的思维方式和编写代码实现出来的能力。
关于提升编程思维和编程能力的建议:
- 不是一朝一夕形成的,需要时间的沉淀和大量的练习。
- 具体措施:勤于练习代码,勤于思考,熟能生巧。
- 前期:先模仿;后期:再创新
二、案例一:买飞机票
注:遇到判断值匹配的时候选择switch分支结构实现;
遇到判断区间范围的时候使用if分支结构实现。
import java.util.Scanner;
public class Demo {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("请输入机票的原价:");
double originalPrice = s.nextDouble();
int ticketMonth;
while(true) {
System.out.println("请输入月份:");
ticketMonth = s.nextInt();
if (ticketMonth < 1 || ticketMonth > 12) {
System.out.println("输入的月份不合规范,请重新输入!");
}else {
break;
}
}
System.out.println("请输入机舱类型:");
String cang = s.next();
calculate(originalPrice,ticketMonth,cang);
}
public static void calculate(double originalPrice, int ticketMonth, String cang) {
double n = 1;
if (ticketMonth >= 5 && ticketMonth <= 10) {
switch (cang) {
case "头等舱":
n = 0.9;
break;
case "经济舱":
n = 0.85;
break;
default:
System.out.println("Default!");
return;
}
} else {
switch (cang) {
case "头等舱":
n = 0.7;
break;
case "经济舱":
n = 0.65;
break;
default:
System.out.println("Default!");
return;
}
}
double price = originalPrice * n;
System.out.println("您购买的原价为:" + originalPrice + "元," + ticketMonth + "月份的," + cang + "现票价为:" + price + "元。");
}
}
三、案例二:找素数
public class Demo{
public static void main(String args[]){
boolean flag;
int sum = 0;
for(int i = 101;i <= 200;i++){
flag = check(i);
if(flag){
sum++;
System.out.println(i);
}
}
System.out.println("一共有" + sum + "个素数");
}
public static boolean check(int n){
for(int i = 2;i < n / 2;i++){
if(n % i == 0)
return false;
}
return true;
}
}
四、案例四:开发验证码
核心实现逻辑:
1、定义一个String类型的变量存储验证码字符
2、定义一个for循环,循环5次
3、随机生成0、1、2的数据,依次代表当前位置要生成数字、大写字母、小写字母
4、把0、1、2交给switch生成对应类型的随机字符,把字符交给String变量
5、循环结束后,返回String类型的变量即是所求的验证码结果
import java.util.Random;
public class Demo{
public static void main(String args[]){
Random r = new Random();
String s = "";
for(int i = 0;i < 5;i++){
int index = r.nextInt(3);
switch(index){
case 0:
s += r.nextInt(10);
break;
case 1:
s += (char)(r.nextInt(26) + 65);
break;
case 2:
s += (char)(r.nextInt(26) + 97);
break;
default:
System.out.println("Error!");
}
}
System.out.println(s);
}
}
五、案例四:素组元素复制
注:数组拷贝的含义
需要创建新数组,把原来数组的元素赋值过来
public class Demo{
public static void main(String args[]){
int[] array1 = new int[]{11,22,33,44,55,66};
int[] array2 = array1; //数组赋值
printArray(array1);
printArray(array2);
printArray(copyArray(array1)); //数组复制方法拷贝
}
public static int[] copyArray(int[] array){
int[] newArray = new int[array.length];
for(int i = 0;i < array.length;i++){
newArray[i] = array[i];
}
return newArray;
}
public static void printArray(int[] array){
System.out.print("[");
for(int i = 0;i < array.length - 1;i++){
System.out.print(array[i] + ",");
}
System.out.print(array[array.length - 1] + "]" + '\n');
}
}
六、案例五:评委打分
import java.util.Random;
public class Demo{
public static void main(String args[]){
Random r = new Random();
int[] scores = new int[6];
for(int i = 0;i < scores.length;i++){
scores[i] = r.nextInt(100);
System.out.println("the " + (i + 1) + " scores is :" + scores[i]);
}
averageScores(scores);
}
public static void averageScores(int[] scores){
int minScores = scores[0];
int maxScores = scores[0];
double averageScores = 0;
int sum = scores[0];
for(int j = 1;j < scores.length;j++){
if(minScores > scores[j]){
minScores = scores[j];
}
if(maxScores <scores[j]){
maxScores = scores[j];
}
sum += scores[j];
}
averageScores = (double) (sum - minScores - maxScores) / (scores.length - 2);
System.out.println("the minScores is :" + minScores);
System.out.println("the maxScores is :" + maxScores);
System.out.println("the average scores is :" + averageScores);
}
}
七、案例六:数字加密
import java.util.Scanner;
public class Demo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("please input the originalNumber:");
int originalNumber = scanner.nextInt();
int len = lenNumber(originalNumber);
int[] array = getArray(originalNumber,len);
print(array);
int[] miArray = getMiArray(array);
print(miArray);
}
public static int lenNumber(int originalNumber){
int count = 0;
while(originalNumber >= 1){
originalNumber = originalNumber / 10;
count++;
}
return count;
}
public static int[] getArray(int originalNumber,int len){
int[] array = new int[len];
for(int i = len - 1;i >= 0;i--){
array[i] = originalNumber % 10;
originalNumber = originalNumber / 10;
}
return array;
}
public static int[] getMiArray(int[] array){
int[] miArray = new int[array.length];
for(int i = 0;i < array.length;i++){
array[i] = (array[i] + 5) % 10;
miArray[array.length - i - 1] = array[i];
}
return miArray;
}
public static void print(int[] array){
for(int i= 0;i < array.length;i++){
System.out.print(array[i]);
}
System.out.println();
}
}
八、案例七:模拟双色球
import java.util.Random;
import java.util.Scanner;
public class Demo{
public static void main(String[] args) {
int[] luckyNumber = createLuckNumber();
int[] userNumber = userInputNumber();
System.out.println("==============================您投注的号码为=======================================");
print(userNumber);
System.out.println("===============================中奖的号码为=======================================");
print(luckyNumber);
System.out.println("================================开奖结果为=======================================");
judge(luckyNumber,userNumber);
}
//定义生成随机产生的中奖号码
public static int[] createLuckNumber(){
int[] luckyNumber = new int[7];
Random random = new Random();
//利用for循环和while死循环生成不重复的随机数组
for (int i = 0; i < luckyNumber.length - 1; i++) {
while (true){
boolean flag = true;
int data = random.nextInt(33) + 1;
for (int j = 0; j < i; j++) {
if (luckyNumber[j] == data){
flag = false;
break;
}
}
if (flag){
luckyNumber[i] = data;
break;
}
}
}
luckyNumber[6] = random.nextInt(16) + 1;
return luckyNumber;
}
//输入用户下注的号码
public static int[] userInputNumber(){
Scanner scanner = new Scanner(System.in);
int[] userNumber = new int[7];
System.out.println("请输入您要投注的号码:");
for (int i = 0; i < userNumber.length - 1; i++) {
while (true) {
System.out.println("请输入投注的第" + (i + 1) + "个红球号码(1 ~ 33)为:");
int data = scanner.nextInt();
boolean flag = true;
if (data < 1 || data > 33) {
System.out.println("投注号不符合规范,请重新投注!");
flag = false;
} else {
for (int j = 0; j < i; j++) {
if (userNumber[j] == data){
System.out.println("投注号码重复,请重新投注!");
flag = false;
break;
}
}
}
if (flag){
userNumber[i] = data;
break;
}
}
}
while (true) {
System.out.println("请输入投注的蓝球号码(1 ~ 16)为:");
int data = scanner.nextInt();
boolean flag = true;
if(data < 1 || data > 16){
System.out.println("投注的号码不规范!请重新投注~");
flag = false;
}
if (flag){
userNumber[6] = data;
break;
}
}
return userNumber;
}
//判断用户中将情况
public static void judge(int[] a1,int[] a2){
int sum = 0;
for (int i = 0; i < a1.length - 1; i++) {
for (int j = 0; j < a2.length - 1; j++) {
if (a2[j] == a1[i]){
sum++;
}
}
}
//判断蓝球是否中奖
boolean blueBall = false;
if (a1[6] == a2[6]){
blueBall = true;
}
if (sum == 6){
if (blueBall){
System.out.println("红球命中6个,蓝球命中,获得一等奖,奖金1000万");
}else {
System.out.println("红球命中6个,蓝球未命中,获得二等奖,奖金500万");
}
} else if (sum == 5) {
if (blueBall){
System.out.println("红球命中5个,蓝球命中,获得三等奖,奖金3000元");
}else {
System.out.println("红球命中5个,蓝球未命中,获得四等奖,奖金200元");
}
} else if (sum == 4) {
if (blueBall){
System.out.println("红球命中4个,蓝球命中,获得四等奖,奖金200元");
}else {
System.out.println("红球命中4个,蓝球未命中,获得五等奖,奖金10元");
}
}else if (sum == 3 && blueBall) {
System.out.println("红球命中3个,蓝球命中,获得五等奖,奖金10元");
}else if (sum < 3 && blueBall){
System.out.println("红球命中" + sum + "个,蓝球命中,奖金5元");
}else{
System.out.println("很遗憾,您未中奖");
}
}
public static void print(int[] array){
System.out.print("{");
for (int i = 0; i < array.length; i++) {
System.out.print(i < (array.length - 1) ? (array[i] + ",") : array[i] );
}
System.out.println("}");
}
}
注:定义两个数组用于存放userNumber(用户投注的号码)和luckyNumber(随机产生的中奖号码),同时定义两个方法createLuckyNumber(),userInputNumber()产生和输入对应的号码。在定义一个judge()方法,判定中奖结果,在该方法中,用sum变量记录6个红球的命中个数,blueBall变量确定蓝球是否命中,并以此来确定中奖结果和金额。