文章目录
综合案例
1.卖飞机票
需求:
机票价格按照淡季旺季、头等舱和经济舱收费,输入机票原价、月份和头等舱或经济舱。
按照如下规则计算机票价格:旺季(5~10月)头等舱9折,经济舱8.5折;淡季(11月到来年4月)头等舱7折,经济舱6.5折。
import java.util.Scanner;
public class TextDemo1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the original airfareb price.");
double money = sc.nextDouble();
System.out.println("Please enter the month.");
int month = sc.nextInt();
System.out.println("Please enter your cabin type(first class please enter true;econimy class please enter false).");
boolean type = sc.nextBoolean();
if(month >= 5 && month <= 10 ){
if( type == true){
money *= 0.9;
} else {
money *= 0.85;
}
}else if((month >= 1 && month <=4)||(month >= 6 && month <= 12){
if(type == false){
money *= 0.7;
} else {
money *= 0.65;
}
} else{
System.out.println("The month you entered is invalid.");
}
System.out.println("The ultimate price is " + money);
}
}
代码有重复,将舱位判断抽取成一个方法
IDEA快捷键:Ctrl+P,提示形参
import java.util.Scanner;
public class Demo1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the original airfareb price.");
double money = sc.nextDouble();
System.out.println("Please enter the month.");
int month = sc.nextInt();
if(month > 12 || month < 1){
System.out.println("The month you entered is invalid.");
return;
} else{
System.out.println("Please enter your cabin type(first class please enter true;econimy class please enter false).");
boolean type = sc.nextBoolean();
if(month >= 5 && month <= 10){
money = getPrice(money,type,0.9,0.85);
} else{
money = getPrice(money,type,0.7,0.65);
}
System.out.println("The ultimate price is " + money);
}
}
public static double getPrice(double money,boolean type, double v1,double v2){
if(type == true ){
money *= v1;
}else {
money *= v2;
}
return money;
}
}
IEDA快捷键:Ctrl+Alt+M,自动抽取方法
2.找质数
判断101~200之间有多少个素数,并输出所有素数。
IDEA快捷键:200.fori
public class TextDemo2{
public static void main(String[] args){
for(int i = 101;i <= 200;i++){
if(isPrime(i)){
System.out.println(i);
}
}
}
public static boolean isPrime(int n){
boolean flag = false;
for(int i = 2;i < n;i++){
if(n % i == 0){
return false;
}
}
return true;
}
}
3.开发验证码
需求:
定义方法实现随机产生一个5位的验证码
验证码格式:
长度为5
前四位是大写字母或者小写字母
最后一位是数字
import java.util.Scanner;
import java.util.Random;
public class TextDemo3{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Random r = new Random();
char [] chs = new char[52];
for(int i = 0;i < chs.length / 2;i++){
chs[i] = (char) (97 + i);
}
for(int i = chs.length /2 ;i < chs.length;i++){
chs[i] = (char)(65 + i - chs.length / 2);
}
String s = Character.toString(chs[ r.nextInt(52)]) + Character.toString(chs[ r.nextInt(52)]) + Character.toString(chs[ r.nextInt(52)]) + Character.toString(chs[ r.nextInt(52)]) + Integer.toString(r.nextInt(10));
System.out.println(s);
}
}
“” + ‘a’ =“a”
or
import java.util.Scanner;
import java.util.Random;
public class Demo2{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Random r = new Random();
char [] chs = new char[52];
String result = "";
for(int i = 0;i < chs.length ;i++){
if(i < chs.length / 2) {
chs[i] = (char) (97 + i);
} else {
chs[i] = (char)(65 + i - chs.length / 2);
}
}
for(int i = 0;i < 4;i++){
result += chs[ r.nextInt(52)];
}
result += r.nextInt(10);
System.out.println(result);
}
}
4.数组元素的复制
把一个数组中的元素复制到另一个新数组中去。
public class TextDemo4{
public static void main(String[] args){
int [] array = {1,2,3,4,5};
int [] arr = new int [array.length];
for(int i = 0, j = 0;i < array.length;i++,j++){
arr[j] = array[i];
}//复制到新数组中,也可以:
/*for(int i = 0;i <array.length;i++){
arr[i]=array[i];
}*/
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i] + " ");
}
}
}
5.评委打分
需求:
在唱歌比赛中,有6位评委给选手打分,分数范围是[0,100]之间的整数。选手最后得分为:去掉最高分、最低分后的四个评委的平均分,请完成上述过程并计算出选手的得分。
import java.util.Scanner;
public class TextDemo5{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int [] array = new int [6];
for(int i = 0;i < array.length;){
System.out.println("Please enter the " + (i + 1) + " score.");
int score = sc.nextInt();
if(score >= 0 && score <= 100){
array[i] = score;
i++;//如果在0~100,则存入数组中,否则不存入
} else{
System.out.println("The content you entered is invalid.Please enter again.");
}
}
int maxIndex = 0;
for(int i = 1;i < array.length;i++){
if(array[i] > array[maxIndex]){
maxIndex = i;
}
}
int minIndex = 0;//IDEA快捷键:选中maxIndex ,Shift+F6,批量修改
for(int i = 1;i < array.length;i++){
if(array[i] < array[minIndex]){
minIndex = i;
}
}
array[maxIndex] = 0;
array[minIndex] = 0;
int sum = 0;
for(int i = 0;i < array.length;i++){
sum += array[i];
}
double average = 0.0;
average = sum / (array.length - 2);
System.out.println(average);
}
}
可以直接在数组中将最大值和最小值修改为0,再将修改后的数组累加,但是改动了数组
or不修改数组,先累加数组中的值,再减去最大值和最小值
import java.util.Scanner;
public class Demo3{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int [] array = new int [6];
for(int i = 0;i < array.length;){
System.out.println("Please enter the " + (i + 1) + " score.");
int score = sc.nextInt();
if(score >= 0 && score <= 100){
array[i] = score;
i++;
} else{
System.out.println("The content you entered is invalid.Please enter again.");
}
}
int max = getMax(array);;
int min = getMin(array);
int sum = 0;
for(int i = 0;i < array.length;i++){
sum += array[i];
}
double average = 0.0;
average =( sum - max - min)/ (array.length - 2);
System.out.println(average);
}
public static int getMax(int [] array){
int max = array[0];
for(int i = 1;i < array.length;i++){
max = max > array[i] ? max :array[i];
}
return max;
}
public static int getMin(int [] array){
int min = array[0];
for(int i = 1;i < array.length;i++){
min = min < array[i] ? min :array[i];
}
return min;
}
}
6.数字加密
需求:
某系统的数字密码(大于0),比如1983,采用加密方式进行传输。
规则如下:
先得到每位数,然后每位数都加上5,再对10求余,最后将所有数字反转,得到一串新数。
import java.util.Scanner;
public class TextDemo6{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number.");
int n = sc.nextInt();
int sum = 0;
while(n > 0){
int senior = (n % 10 + 5) % 10;
n /= 10;
sum = sum * 10 + senior;
}
System.out.println(sum);
}
}
将数字的每一位数放到数组中,加5,再模10;最后倒转
import java.util.Scanner;
public class Demo4{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number");
int n = sc.nextInt();
//因为在获取数字位数时,n会变,所以定义一个新的变量,存储n的初始值,即为输入的数字
int number = n;
int i = 0,j = 0;
int count = 0;
int result = 0;
//因为数字都是大于0的,首先知道数字的位数,用count记录
while(n > 0){
i++;
count++;
n /= 10;
}
//定义一个数组,长度就是为count
int [] array = new int [count];
//将数字中的每一位按顺序存到数组中去
for(i = count - 1;i >= 0;i--){//先获取的是最后一位,所以倒着存入数组中
array[i] = number % 10;
number /= 10;
}
//将数组中的每一位数加5,再模10
for( i = 0;i < count;i++){
array[i] = (array[i] + 5)%10;
}
//将数组中的元素倒转
for( i = 0, j = count - 1;i < j;i++,j--){
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
//最后将数组中有顺序的元素变成一个十进制数
for(i = 0;i < count;i++){
result = result * 10 +array[i];
}
System.out.println(result);
}
}
7.数字解密
import java.util.Scanner;
public class TextDemo7{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number.");
int n = sc.nextInt();
int number = n;
int count = 0;
int i,j;
int result = 0;
while(n > 0){
n /= 10;
count++;
}
int [] array = new int [count];
//将数字存入数组中并倒转,因为先获得的是个位,然后存入0索引中,相当于倒转了一次
for(i = 0;i < count;i++){
array[i] = number % 10;
number /= 10;
}
/*取模的反操作:
取模后:数字在1~9
取模前:最小为1 + 5 = 6,最大为9 + 5 = 14
所以:
如果数字在1~4,取模前就是(1 + 10 = 11)~(4 + 10 = 14);
如果数字在5~9,取模前就是本身
再将数组中的数字减5*/
for(i = 0;i < count;i++){
if(array[i] >= 1 && array[i] <= 4){
array[i] += 10;
}
array[i] -= 5;
}
//最后将数组中有顺序的元素变成一个十进制整数
for(i = 0;i < count;i++){
result = result * 10 + array[i];
}
System.out.println(result);
}
}
8.抢红包
需求:
一个大V直播抽奖,奖品是现金红包,分别有{2,588,888,1000,10000}五个奖金。请用代码模拟抽奖,打印出每个奖项,奖项出现的顺序要随机且不重复。打印效果如下:(随机顺序,不一定是下面的顺序)
888元的奖金被抽出
588元的奖金被抽出
10000元的奖金被抽出
1000元的奖金被抽出
2元的奖金被抽出
(打乱数组)
import java.util.Random;
public class TextDemo8{
public static void main(String[] args){
Random r = new Random();
int [] array = {2,588,888,1000,10000};
int i,j;
for(i = 0;i < array.length;i++){
j = r.nextInt(array.length);//随机产生的是索引,将索引i与索引j的元素内容对换,就是将数组中的元素对换,重复多次,满足条件随机且不重复
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
for(i = 0;i < array.length;i++){
System.out.println("A bonus of " + array[i] + " yuan was drawn out. ");
}
}
}
新创建一个数组,判断随机生成的索引在元数组中的值在新数组中是否存在,不存在则复制到新数组中
import java.util.Random;
public class Demo5{
public static void main(String[] args){
Random r = new Random();
int [] array = {2,588,888,1000,10000};
int [] arr = new int [array.length];
int i,j;
for(i = 0;i < array.length;){
j = r.nextInt(array.length);
if(!isExist(array[j],arr)){
arr[i] = array[j];
i++;//如果在新数组中不存在该值,新数组的索引+1
}
}
for(i = 0;i < arr.length;i++){
System.out.println("A bonus of " + arr[i] + " yuan was drawn out. ");
}
}
public static boolean isExist(int n,int [] arr){
for(int i = 0;i < arr.length;i++){
if(n == arr[i]){
return true;
}
}
return false;
}
}
9.模拟双色球
投注号码由6个红色球号码(不能重复)和1个蓝色球号码(可以跟红球号码重复)组成。红色球号码从1~33中选择;蓝色球号码从1~16中选择。
奖等,中奖条件(红+蓝)
一等奖
6+1
二等奖
6+0
三等奖
5+1
四等奖
5+0
4+1
五等奖
4+0
3+1
六等奖
2+1
1+1
0+1
import java.util.Random;
import java.util.Scanner;
public class TextDemo9{
public static void main(String[] args){
Random r = new Random();
Scanner sc = new Scanner(System.in);
int [] array = new int [7];
int [] arr = new int [array.length];
int i,j,tmp;
int count1 = 0,count2 = 0;
for(i = 0;i < array.length - 1;){
tmp = r.nextInt(33) + 1;
if(!isExist(tmp,arr)){
array[i] = tmp;
i++;
}
}
array[array.length - 1] = r.nextInt(16) + 1;
for(i = 0;i < arr.length - 1;){
System.out.println("Please enter " + (i + 1) + "th red ball number.");
tmp = sc.nextInt();
if(tmp < 1 || tmp >33 ){
System.out.println("The number you entered is invalid.Please enter again.");
} else{
arr[i] = tmp;
i++;
}
}
while(true){
System.out.println("Please enter the bule ball number.");
tmp = sc.nextInt();
if(tmp > 16 || tmp < 1){
System.out.println("The number you entered is invalid.Please enter again.");
} else {
arr[array.length - 1] = tmp;
break;
}
}
for(i = 0;i < array.length - 1;i++){
for(j = 0;j < arr.length - 1;j++){
if(array[i] == arr[j]){
count1++;
break;
}
}
}
if(array[array.length - 1] == arr[arr.length - 1]){
count2++;
} if(count2 == 0){
if(count1 == 6){
System.out.println("The second prize");
} else if( count1 == 5){
System.out.println("The fourth prize");
} else if( count1 == 4){
System.out.println("The fifth prize");
}else {
System.out.println("No prize.");
}
} else {
if(count1 == 6){
System.out.println("The first prize");
} else if( count1 == 5){
System.out.println("The third prize");
} else if( count1 == 4){
System.out.println("The fourth prize");
} else if( count1 == 3){
System.out.println("The fifth prize");
} else if( count1 == 2 || count1 == 1 || count1 == 0){
System.out.println("The sixth prize");
}else {
System.out.println("No prize.");
}
}
}
public static boolean isExist(int n,int [] arr){
for(int i = 0;i < arr.length;i++){
if(n == arr[i]){
return true;
}
}
return false;
}
}
二维数组
1.二维数组的应用场景
数组中存数组
当我们需要把数据分组管理的时候,就需要用到二维数组。
(1)二维数组的初始化
二维数组的静态初始化
格式
数据类型 [][] 数组名 = new 数据类型 [][] {{元素1,元素2},{元素1元素2}};
简化格式
数据类型 [][] 数组名 = {{元素1,元素2},{元素1,元素2}};
一位数组的长度可以不一样
习惯格式
将每个一维数组写在一行
int [][] arr = {
{1,2,3},
{4,5,6,7}
};
获取元素
System.out.println(arr[0][0]);//1
System.out.println(arr[0][4]);//8
System.out.println(arr[2][0]);//ArrayIndexOutOfBoundsException
遍历二维数组
for(i = 0;i < arr.length;i++){//arr.length -> 二维数组的长度
for(j = 0;j < arr[i].length;j++){//arr[i].length -> 一维数组的长度
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
二维数组的动态初始化
格式
数据类型 [][] 数组名 = new 数据类型 [m][n];
/*m表示这个二维数组,可以存放多少个一维数组,即二维数组的长度,也即一维数组的个数
n表示每一个一维数组,可以存放多少个元素,即一维数组的长度
*/
二维数组的遍历
public class Demo6{
public static void main(String[] args){
int [][] arr = new int [3][5];
arr[0][0] = 10;//未被初始化的地方,默认初始化为0
for(int i = 0;i < arr.length;i++){
for(int j = 0;j < arr[i].length;j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
(2)二维数组的内存图
public static void main(String[] args){
int [][] arr = new int [3][5];
}
先在栈空间中的main方法空间中划分一块内存,名字为int [][]
因为有new关键字,因为是2,会在堆中创建一个长度为2的二维数组,并将该数组的地址存到int [][]中
因为是二维数组,且因为是2和3,会在堆中创建2个长度为3的一维数组
并将后来创建的两个数组的第一个数组的地址存到最先创建的长度为2的数组中的0索引的位置上
将后来创建的两个数组的第二个数组的地址存到最先创建的那个数组的索引为1的位置上
特殊情况1
public static void main(String[] args){
int [][] arr = new int [2][];//只创建了一个二维数组,一维数组未创建
//自己创建一维数组。好处:如果是Java创建,所以的一维数组长度一样;自己创建,一维数组的长度可以不同
int [] arr1 = {11,22};
int [] arr2 = {44,55,66};
//将创建的两个一维数组的地址赋值到二维数组中去
arr[0] = arr1;
arr[1] = arr2;
}
特殊情况2
public static void main(String[] args){
int [][] arr = new int [2][3];
//自己创建两个新的一维数组
int [] arr1 = {11,22};
int [] arr2 = {44,55,66};
//将两个新的一维数组赋值到最先创建的二维数组中,那么Java创建的一维数组就会失去用处,会从内存中消失
arr[0] = arr1;
arr[1] = arr2;
}
(3)二维数组的练习
某商城每个季度的营业额如下:单位(万元)
第一季度:22,66,44
第二季度:77,33,88
第三季度:25,45,65
第四季度:11,66,99
要求计算出每个季度的总营业额和全年的总营业额
public class Demo7{
public static void main(String[] args){
int sum = 0;
int yearSum = 0;
int [][] arr = {
{22,66,44},
{77,33,88},
{25,45,65},
{11,66,99}
};
for(int i = 0;i < arr.length;i++){
sum = getSum(arr[i]);//传的是二维数组的元素 -> 一维数组的首地址
System.out.println("The revenue of the " + ( i + 1) + "th quarter is " + sum + ".");
yearSum += sum;
}
System.out.println("The revenue of the whole year is " + yearSum + ".");
}
public static int getSum(int [] arr){
int sum = 0;
//累加二维数组的元素 -> 一维数组
for(int i = 0;i < arr.length;i++){
sum += arr[i];
}
return sum;
}
}