综合练习
目的:
复习前半段的知识,并使用所学知识解决问题,提升编程能力。
自动抽取方法:ctrl+alt+M
变量的自动修改: shift+F6
案例一:
public class AnLi1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票原价");
double price = sc.nextDouble();
System.out.println("请输入月份");
int month = sc.nextInt();
System.out.println("请输入舱室");
String cang = sc.next();
double pay = pay(price, month, cang);
System.out.println("当前的价格是 "+ pay);
}
public static double pay(double price,int month,String cangshi){
double pay = 0;
if (month>=1&&month<=12&&cangshi.equals("头等舱")||cangshi.equals("经济舱")){
if (month>=5&&month<=10){
//旺季
pay = getPay(price, cangshi);
}else {
pay = getaDouble(price, cangshi);
}
}else
System.out.println("数据输入错误。");
return pay;
}
private static double getPay(double price, String cangshi) {
double pay;
if (cangshi.equals("头等舱")){
pay= price *0.9;
}else {
pay= price *0.85;
}
return pay;
}
private static double getaDouble(double price, String cangshi) {
double pay;
if (cangshi.equals("头等舱")){
pay= price *0.7;
}else {
pay= price *0.65;
}
return pay;
}
}
案例二:
public class AnLi2 {
public static void main(String[] args) {
//定义变量用来记录素数的个数
int count = 0;
//先获取这个数的平方根,之后在从1到平方根判断是否有质数
for (int i = 101; i <=200 ; i++) {
//定义一个标记值判断是否被整除
boolean falg=true;
int panfang = panfang(i);
if (panfang == 0){
System.out.println("程序出错");
}
for (int j = 2; j <panfang ; j++) {
if (i%j==0){
falg = false;
//break只跳出一层循环
break;
}
}
if (falg){
//未被整除,为素数
System.out.println("素数为: "+i);
count++;
}
}
System.out.println("素数的个数: "+count);
}
public static int panfang(int number){
int gen = 0;
for (int i = 1; i < number ; i++) {
if (i*i == number){
// System.out.println("平方根为: "+i);
gen = i;
}
if (i*i>number){
// System.out.println("平方根为: "+(i-1));
gen = i;
}
}
return gen;
}
}
案例三:
实现方法一:
package day07;
import java.util.Random;
public class AnLi3 {
public static void main(String[] args) {
//生成验证码
Random r = new Random();
//生成字符串接收验证码
String ma = "";
//根据i判断生成大写还是小写
for (int j = 0; j < 4; j++) {
int i = r.nextInt(2)+1;
if (i==1){
//生成大写
char Char = (char)(r.nextInt(26)+65);
System.out.println(Char);
ma = ma + Char;
} else if (i==2) {
//生成小写
char Char = (char)(r.nextInt(26)+97);
System.out.println(Char);
ma = ma + Char;
}
}
//加入数字
int i = r.nextInt(10);
ma = ma + i;
System.out.println("最终的验证码为 "+ ma);
}
}
实现方法二:
package day07;
import java.util.Random;
public class AnLI3_2 {
public static void main(String[] args) {
//将大写和小写字母都装入数组中,生成随机数作为索引来获取随机的字母
//定义数组
char[] chs = new char[52];
//填入字母
for (int i = 0; i < chs.length; i++) {
if (i<25){
chs[i] = (char) (97+i);
}else {
chs[i] = (char) (65+i - 26);
}
}
//创建一个String接收生成的随机数
String result = "";
//随机抽取4次
Random r = new Random();
for (int i = 0; i < 4; i++) {
int random = r.nextInt(chs.length);
result = result+chs[random];
}
//获取随机数字
int number = r.nextInt(10);
result = result + number;
System.out.println(result);
}
}
案例四:
public static void main(String[] args) {
//定义老数组
int[] arr = {12,13,141,5,16,17,18,19,23,24,25};
//定义新数组
int[] arr2 = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
arr2[i] = arr[i];
}
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i]+" ");
}
}
案例五:
package day07;
import java.util.Scanner;
public class AnLI5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//创建一个数组储存评委打分
double[] grades = new double[6];
for (int i = 0; i < grades.length; i++) {
while (true) {
System.out.println("请第"+ (i+1)+"位评委打分");
double grade = sc.nextDouble();
if (grade<=100&&grade>=0){
//存入数组
grades[i] = grade;
break;
}else
System.out.println("输入的分数有误请重新输入");
}
}
//获取最高和最低分
double min = grades[0];
double max = grades[0];
for (int i = 0; i < grades.length; i++) {
if (min>grades[i]){
min = grades[i];
}
if (max<grades[i]){
max = grades[i];
}
}
System.out.println("最小值"+min);
System.out.println("最大值"+max);
//获取总分
double sum = 0;
double avg = 0;
for (int i = 0; i < grades.length; i++) {
sum+=grades[i];
}
System.out.println("总分"+sum);
avg = (sum - max - min)/(grades.length-2);
System.out.println("平均分 "+avg);
}
}
案例六:
方法一(自己写的):
public class ANLi6 {
public static void main(String[] args) {
//获取数字密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入数组密码");
int password = sc.nextInt();
//开始进行加密
//得到每位数字,写入数组
int[] arr = Split(password);
//进行加密
int encrypt = encrypt(arr);
System.out.println(encrypt);
}
public static int[] Split(int password){
// int[] arr = new int[];
int optration = password;
int count = 0;//记录位数
while(optration!=0){
// password%10;//%取余
optration=optration/10;// /整除
count++;
}
int[] arr = new int[count];
int i = 0;
while(password!=0){
arr[i] = password%10;//%取余
password=password/10;// /整除
i++;
}
return arr;
}
public static int encrypt(int[] arr){
for (int i = 0; i < arr.length; i++) {
//加密
arr[i] = (arr[i]+5)%10;
}
//反转数组
int temp = arr[0];
for (int i = 0,j=arr.length-1; i <j; i++,j--) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int sum = 0;
//用于为不同的位赋值
int multiplier = 1;
for (int i = 0; i < arr.length; i++) {
sum += arr[i]*(multiplier);
multiplier = multiplier*10;
}
System.out.println(sum);
return sum;
}
}
方法二(只有将反转后的数组转为数字是有区别):
public class AnLi6_2 {
public static void main(String[] args) {
//获取数字密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入数组密码");
int password = sc.nextInt();
//开始进行加密
//得到每位数字,写入数组
int[] arr = Split(password);
//进行加密
int encrypt = encrypt(arr);
System.out.println(encrypt);
}
public static int[] Split(int password){
// int[] arr = new int[];
int optration = password;
int count = 0;//记录位数
while(optration!=0){
// password%10;//%取余
optration=optration/10;// /整除
count++;
}
int[] arr = new int[count];
int i = 0;
while(password!=0){
arr[i] = password%10;//%取余
password=password/10;// /整除
i++;
}
return arr;
}
public static int encrypt(int[] arr){
for (int i = 0; i < arr.length; i++) {
//加密
arr[i] = (arr[i]+5)%10;
}
//反转数组
int temp = arr[0];
for (int i = 0,j=arr.length-1; i <j; i++,j--) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int sum = 0;
//用于为不同的位赋值
// int multiplier = 1;
for (int i = arr.length-1; i >=0; i--) {
sum = sum*10+arr[i];
}
System.out.println(sum);
return sum;
}
}
案例7:
public static void main(String[] args) {
int[] arr = {8,3,4,6};
for (int i = 0,j = arr.length-1; i < j; i++,j--) {
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
for (int i = 0; i < arr.length; i++) {
if (arr[i]>=0&&arr[i]<=4){
arr[i] = arr[i] + 10;
}
}
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i]-5;
}
int number = 0;
for (int i = 0; i < arr.length; i++) {
number = number*10 + arr[i];
}
System.out.println(number);
}
案例八:
方法一(效率较低):
package day07;
import java.util.Random;
public class AnLi8 {
public static void main(String[] args) {
//定义数组
int [] arr= {2,588,888,1000,10000};
int[] arrs= new int[arr.length];
//用于记录已经有几个被抽取,记录个数,并为新数组提供下标。
int n = 0;
//随机获取,并与已经获取的数据对比如果有则重新抽取
Random r=new Random();
for (int i = 0; i < arr.length; i++) {
while (true) {
//抽取
int i1 = r.nextInt(5);
int number = arr[i1];
boolean flag = true;
for (int i2 = 0; i2 < arrs.length; i2++) {
if (number==arrs[i2])
flag=false;
}
//没有重复
if (flag){
System.out.println("您抽到的是:"+ number);
arrs[n] = number;
n++;
break;
}
}
}
}
}
方法二(随机打乱数组的顺序,效率高):
public static void main(String[] args) {
//定义数组
int [] arr= {2,588,888,1000,10000};
//随机打乱数组
Random r = new Random();
for (int i = 0; i < arr.length; i++) {
int randomIndex = r.nextInt(5);
//将随机数和数组的索引换位
int temp = arr[i];
arr[i] = arr[randomIndex];
arr[randomIndex] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println("恭喜你获得:"+ arr[i]);
}
}
案例九:
public class AnLi9 {
public static void main(String[] args) {
//定义一个数组存抽到的球
Random r = new Random();
Scanner sc = new Scanner(System.in);
//存红球
int [] redarr = new int[6];
//存篮球
int [] bluearr = new int[1];
for (int i = 0; i < redarr.length; i++) {
while (true) {
//存红球
int number = r.nextInt(33)+1;
boolean flag = true;
for (int i1 = 0; i1 <i; i1++) {
if (number == redarr[i1]){
flag = false;
}
}
if (flag){
redarr[i] = number;
break;
}
}
}
bluearr[0] = r.nextInt(16)+1;
for (int i = 0; i < redarr.length; i++) {
System.out.print(redarr[i]+" ");
}
System.out.println(bluearr[0]);
//定义一个数组存选到的号
int [] reduser = new int[6];
int [] blueuser = new int[1];
for (int i = 0; i < 7; i++) {
if (i<=5){
while (true) {
//存红球
System.out.println("亲输入红球的数字");
int number = sc.nextInt();
if (number>=1&&number<=33){
boolean flag = true;
for (int i1 = 0; i1 <i; i1++) {
if (number == reduser[i1]){
flag = false;
System.out.println("输入数据重复");
}
}
if (flag){
reduser[i] = number;
break;
}
}else {
System.out.println("输入错误请重新输入");
}
}
}else {
while (true) {
System.out.println("亲输入蓝球的数字");
int number = sc.nextInt();
if (number>=1&&number<=16){
blueuser[0] = number;
break;
}else {
System.out.println("请重新输入");
}
}
}
}
for (int i = 0; i < reduser.length; i++) {
System.out.println(reduser[i]);
}
System.out.println(blueuser[0]);
//判断中了几个球
int redcount =0;
int bluecount = 0;
for (int i = 0; i < redarr.length; i++) {
for (int j = 0; j < reduser.length; j++) {
if (redarr[i] == reduser[j]){
redcount++;
}
}
}
if (bluearr[0]==blueuser[0]){
bluecount++;
}
System.out.println(redcount);
System.out.println(bluecount);
if ((bluecount==1&&redcount==0)||(bluecount==1&&redcount==1)||(bluecount==1&&redcount==1)){
System.out.println("恭喜获得:5元");
} else if ((bluecount==1&&redcount==3)||(bluecount==0&&redcount==4)) {
System.out.println("恭喜获得:10元");
} else if ((bluecount==1&&redcount==4)||(bluecount==0&&redcount==5)) {
System.out.println("恭喜获得:200元");
} else if ((bluecount==1&&redcount==5)) {
System.out.println("恭喜获得:3000元");
} else if ((bluecount==0&&redcount==6)) {
System.out.println("恭喜获得:500w元");
} else if ((bluecount==1&&redcount==6)) {
System.out.println("恭喜获得:1000w元");
}else {
System.out.println("什么都没中,远离赌博");
}
}
}
二维数组
静态初始化:
练习一:
public class AnLi10 {
public static void main(String[] args) {
//静态初始化一个二维数组
int[][] arr = {
{22,66,44},
{77,33,88},
{25,45,65},
{11,66,99}
};
//遍历
int sun = 0;
for (int i = 0; i < arr.length; i++) {
int[] ints = arr[i];
int jidu = jidu(ints);
System.out.println("第"+(i+1)+"季度的收益"+jidu);
sun = sun+jidu;
}
System.out.println("总营业额"+sun);
}
public static int jidu(int[] arr){
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum+=arr[i];
}
return sum;
}
}