目录
- 一、综合练习
-
- 1.1 飞机票
- 1.2 找素数
- 1.3 随机产生5位验证码
- 1.4 评委打分
- 1.5 数字加密和解密
-
- 1.6 抽奖系统
- 1.7 双色球彩票
- 二、二维数组
-
- 2.1 二维数组的静态初始化和动态初始化
- 2.2二维数组的内存
- 2.3 两种特殊情况
- 2.4 练习
一、综合练习
1.1 飞机票
package Comprehensive_Exericse;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票原价:");
int price = sc.nextInt();
System.out.println("请输入当前月份:");
int month = sc.nextInt();
System.out.println("请输入对应舱位(0 头等舱, 1 经济舱):");
int seat = sc.nextInt();
if (month >= 5 && month <= 10){
int ticket = getPrice(price,seat,0.9,0.85);
System.out.println("机票价格为:"+ ticket + "元");
}else if ((month >= 1 && month <= 4) ||(month >= 11 & month <= 12)){
int ticket = getPrice(price,seat,0.7,0.65);
System.out.println("机票价格为:"+ ticket + "元");
}else {
System.out.println("月份错误");
}
}
public static int getPrice(int price,int seat,double sale0,double sale1){
if (seat == 0){
price = (int) (price *sale0);
}else if (seat == 1){
price = (int) (price*sale1);
}else {
System.out.println("舱位错误");
}
return price;
}
}
1.2 找素数
package Comprehensive_Exericse;
public class test2 {
public static void main(String[] args) {
int count = 0;
for(int i = 101;i <= 200; i++){
boolean flag = true;
for (int j = 2; j < i; j++) {
if (i % j == 0){
flag = false;
break;
}
}
if (flag){
System.out.println("当前的数字" + i +"是素数");
count++;
}
}
System.out.println("共有素数" + count + "个");
}
}
1.3 随机产生5位验证码
package Comprehensive_Exericse;
import java.util.Random;
public class test3 {
public static void main(String[] args) {
char[] chars = new char[52];
for (int i = 0; i < chars.length; i++) {
if(i <= 25){
chars[i] = (char) (97 + i);
}else {
chars[i] = (char) (65 + i - 26);
}
}
Random r = new Random();
String result = "";
for(int i = 0; i < 4; i++){
int index = r.nextInt(chars.length);
result = result + chars[index];
}
int num = r.nextInt(10);
result = result + num;
System.out.println(result);
}
}
1.4 评委打分
package Comprehensive_Exericse;
import java.util.Scanner;
public class test5 {
public static void main(String[] args) {
int[] scoreList = getScore();
for (int i = 0; i < scoreList.length; i++) {
System.out.print(scoreList[i] + " ");
}
System.out.println(" ");
System.out.println("去掉最高分:" + getMax(scoreList));
System.out.println("去掉最低分:" + getMin(scoreList));
System.out.println("最终平均得分:"+ getAvarage(scoreList, getMax(scoreList),getMin(scoreList)));
}
public static int[] getScore(){
int[] scoreList = new int[6];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scoreList.length;) {
System.out.println("请输入第" + (i+1) +"位评委打分:");
int score = sc.nextInt();
if (score > 100|score < 0){
System.out.println("分数无效,请重新输入。");
}else {
scoreList[i] = score;
i++;
}
}
return scoreList;
}
public static int getMax(int[] arr){
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max){
max = arr[i];
}
}
return max;
}
public static int getMin(int[] arr){
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min){
min = arr[i];
}
}
return min;
}
public static float getAvarage(int[] arr,int max,int min){
int totalScore = 0;
for (int i = 0; i < arr.length; i++) {
totalScore += arr[i];
}
float avarage = (totalScore - max - min) / 4;
return avarage;
}
}
1.5 数字加密和解密
1.5.1 数字加密
package Comprehensive_Exericse;
public class test6 {
public static void main(String[] args) {
int[] arr = {1,9,8,3};
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] + 5;
arr[i] = arr[i] % 10;
}
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int number = 0;
for (int i = 0; i < arr.length; i++) {
number = number * 10 + arr[i];
}
}
}
1.5.2 数字解密
package Comprehensive_Exericse;
public class test8 {
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);
}
}
1.6 抽奖系统
package Comprehensive_Exericse;
import java.util.Random;
public class test10choujiang {
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(arr.length);
int temp = arr[i];
arr[i] = arr[randomIndex];
arr[randomIndex] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + "元的奖金被抽出");
}
}
}
1.7 双色球彩票
package Comprehensive_Exericse;
import java.util.Random;
import java.util.Scanner;
public class test11 {
public static void main(String[] args) {
int[] prizeArr = creatNum();
System.out.println("===================");
for (int i = 0; i < prizeArr.length; i++) {
System.out.print(prizeArr[i]+" ");
}
System.out.println("===================");
int[] userInputArr = userInputNum();
int redCount = 0;
int blueCount = 0;
for (int i = 0; i < userInputArr.length - 1; i++) {
int currentRed = userInputArr[i];
for (int j = 0; j < prizeArr.length - 1; j++) {
if (currentRed == prizeArr[j]){
redCount++;
break;
}
}
}
if (userInputArr[userInputArr.length-1] == prizeArr[prizeArr.length-1]){
blueCount++;
}
if (redCount == 6 && blueCount == 1){
System.out.println("恭喜你中了一等奖!1000万!");
}else if (redCount == 6 && blueCount == 0){
System.out.println("恭喜你中了二等奖!500万!");
}else if (redCount == 5 && blueCount == 1){
System.out.println("恭喜你中了三等奖!1000元!");
}else if ((redCount == 5 && blueCount == 0)||(redCount == 4 && blueCount == 1)){
System.out.println("恭喜你中了四等奖!200元!");
}else if ((redCount == 4 && blueCount == 0)||(redCount == 3 && blueCount == 1)){
System.out.println("恭喜你中了五等奖!10元!");
}else if ((redCount == 2 && blueCount == 1)||(redCount == 1 && blueCount == 1)||(redCount == 0 && blueCount == 1)){
System.out.println("恭喜你中了六等奖!5元!");
}else {
System.out.println("感谢参与!");
}
}
public static int[] creatNum(){
int[] arr = new int[7];
Random r = new Random();
for (int i = 0; i < 6;) {
int redNum = r.nextInt(33) + 1;
if (!contains(arr,redNum)){
arr[i] = redNum;
i++;
}
}
int blueNum = r.nextInt(16) + 1;
arr[arr.length-1] = blueNum;
return arr;
}
public static boolean contains(int[] arr, int num){
for (int i = 0; i < arr.length; i++) {
if (arr[i] == num){
return true;
}
}
return false;
}
public static int[] userInputNum(){
int[] arr = new int[7];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 6; ) {
System.out.println("请输入1-33的整数作为第"+ (i+1) +"个红球号码:");
int rednum = sc.nextInt();
if (rednum >= 1 && rednum <=33){
if (!contains(arr, rednum)){
arr[i] = rednum;
i++;
}else {
System.out.println("当前数字已存在,请重新输入");
}
}else {
System.out.println("d当前数字超过范围,请重新输入");
}
}
System.out.println("请输入1-16的整数作为蓝球号码");
int bluenum = sc.nextInt();
arr[arr.length - 1] = bluenum;
return arr;
}
}
二、二维数组
2.1 二维数组的静态初始化和动态初始化
package TwoDimArr;
public class TwoDimArr {
public static void main(String[] args) {
int[][] arr1 = new int[][]{{1,2,3},{4,5,6,7}};
int[][] arr2 = {{1,2,3},{4,5,6,7}};
int[][] arr3 = {
{1,2,3},
{4,5,6,7}
};
System.out.println(arr3[0][0]);
for (int i = 0; i < arr3.length; i++) {
for (int j = 0; j < arr3[i].length; j++) {
System.out.print(arr3[i][j] + " ");
}
System.out.println();
}
}
}
package TwoDimArr;
public class TwoDimArrDemo2 {
public static void main(String[] args) {
int[][] arr1 = new int[3][5];
arr1[0][0] = 10;
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1[i].length; j++) {
System.out.print(arr1[i][j] + " ");
}
System.out.println();
}
}
}
2.2二维数组的内存
2.3 两种特殊情况
- 特殊情况一:
后面的括号不写内容,自己创建一维数组,再把其地址值赋过去。虽然有点麻烦,但好处是可以输入不一样长度的、自定义的一维数组。
- 特殊情况二:
虽然已经定义了一维数组的长度将其初始化了,但是后面又再赋值,就相当于把新的一维数组的地址存到二维数组的值的部分了,因此以前初始化的一维数组就没用了,就会被当垃圾清除掉。
2.4 练习
package TwoDimArr;
public class test {
public static void main(String[] args) {
int[][] allYear = {
{22, 66, 44},
{77, 33, 88},
{25, 45, 65},
{11, 66, 99}
};
int yearSum = 0;
for (int i = 0; i < allYear.length; i++) {
int[] quarterArr = allYear[i];
int quarterSum = getSum(quarterArr);
System.out.println("第" + (i + 1) + "个季度的总营业额为:" + quarterSum);
yearSum += quarterSum;
}
System.out.println("本年度营业额为:" + yearSum);
}
public static int getSum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
}