案例一:买飞机票
需求:
机票按照淡季旺季头等舱和经济舱收费、输入机票原件、月份和头等舱或经济舱
按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11-4月)头等舱7折,经济舱6.5折。
自写版:
import java.util.Scanner;
public class PlaneTicket {
public static void main(String[] args) {
Scanner ns = new Scanner(System.in);
System.out.println("请输入机票原价:");
double YJ = ns.nextDouble();
System.out.println("请输入起飞月份:");
int mon = ns.nextInt();
System.out.println("请输入座舱等级:1.头等舱;2.经济舱");
int Types = ns.nextInt();
Cacu(YJ, mon, Types);
}
public static void Cacu(double a, int b, int c){
switch(b){
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
a = c==1? 0.9*a:0.85*a;
System.out.println("打折后机票价格为"+a+"元");
return;
case 11:
case 12:
case 1:
case 2:
case 3:
case 4:
a = c==1? 0.7*a:0.65*a;
System.out.println("打折后机票价格为"+a+"元");
return;
default:
System.out.println("请输入正确的月份");
}
}
}
教程版本:区别在于:更好完成了要求,补完整了一些输入错误的处理方式。尤其是学到了:
字符串应该用switch匹配
import java.util.Scanner;
public class PlaneTicket {
public static void main(String[] args) {
Scanner ns = new Scanner(System.in);
System.out.println("请输入机票原价:");
double YJ = ns.nextDouble();
System.out.println("请输入起飞月份:");
int mon = ns.nextInt();
System.out.println("请输入座舱等级:");
String Types = ns.next();
double my = Cacu(YJ, mon, Types);
System.out.println("您当前购买机票的价格是"+my+"元¥");
}
public static double Cacu(double money, int month, String type){
if (month>=5&&month<=10){
switch (type){
case "经济舱":
money *=0.85;
break;
case "头等舱":
money *=0.9;
break;
default:
System.out.println("您输入的舱位不正确~");
money = -1;
break;
}
}else if (month == 11||month==12||month>=1&&month<=4){
switch (type){
case "经济舱":
money *= 0.65;
break;
case "头等舱":
money *=0.7;
break;
default:
System.out.println("您输入的舱位不正确~");
money = -1;
break;
}
}else {
System.out.println("您输入的月份有问题");
money = -1;
}
return money;
}
}
案例二:找素数
自写版:
public class FindSuNum {
public static void main(String[] args) {
int f = 0;
System.out.println("101~200之间的素数分别是:");
for (int i = 101; i <= 200 ; i++) {
for (int j = 2; j <= i; j++) {
if (i%j==0&&i!=j){
break;
}
if(i==j){
f++;
System.out.print(i+"\t");
}
}
}
System.out.println("\n共"+f+"个");
}
}
案例三:开发验证码
需求:定义方法实现随机产生一个5位的验证码,每位可能是数字、大写字母、小写字母;
自写版:
要点:
注意switch中写break;
当使用ASCII码定义char字符时,后面跟数字可以直接当作ASCII码,右面跟变量时要写成强行转换。(6~7行)
String扩容直接 str名 = str名+字符;(10~11行)
import java.util.Random;
public class SafriCode {
public static void main(String[] args) {
/*基础知识
int b = 66;
char a = (char)b;
强行格式转换
String str = "";
str = str+b1;
String扩容方法
*/
String str = Code();
System.out.println(str);
}
public static String Code(){
Random nr = new Random();
String str = "";
for (int i=1; i<=5;i++) {
int a = nr.nextInt(3)+1;
int b = nr.nextInt(26)+65;
int c = nr.nextInt(26)+97;
int d = nr.nextInt(10);
switch(a){
case 1:
char b1 = (char)b;
str = str+b1;
break;
case 2:
char c1 = (char)c;
str = str+c1;
break;
case 3:
str = str+(int)d;
break;
}
}
return str;
}
}
教程版:
优点在于:把随机生成字符部分代码放在了switch之中,减少了无效计算,提升了程序运行效率;
也没有定义很多变量名,而是直接把随机数强打格式加入字符串;节省栈空间内存
import java.util.Random;
public class SafriCode {
public static void main(String[] args) {
String str = Code(5);
System.out.println(str);
}
public static String Code(int n){
Random nr = new Random();
String str = "";
for (int i = 0; i < n; i++) {
int type = nr.nextInt(3);
switch (type){
case 0:
char CH = (char)(nr.nextInt(26)+65);//大写为0
str += CH;
break;
case 1:
char ch = (char)(nr.nextInt(26)+97);//小写为1
str += ch;
break;
case 2:
//数字为2
str += (int)nr.nextInt(10);
break;
}
}
return str;
}
}
案例四:数组赋值
需求:把一个数组中的元素复制到另一个数组中去;
自写版:
需要注意的是,
int[] arr2 = arr1;//不能叫做复制,只能叫创建快捷方式;没有真正备份内容,当arr1内容丢失时,arr2内容也会丢失
public class CopyArr4 {
public static void main(String[] args) {
int[] arr1 = {1,234,213,512,5,14,513,6,12,61346,13,46,136,1,6};
int[] arr2 = arr1;//不能叫做复制,只能叫创建快捷方式;没有真正备份内容,当arr1内容丢失时,arr2内容也会丢失
int[] arr3 = Copy(arr1);
int[] arr4 = {};
System.out.println("三个数组地址分别是"+"\n"+"arr1:"+arr1+"\n"+"arr2:"+arr2+"\n"+"arr3:"+arr3);
Print(arr1);
Print(arr2);
Print(arr3);
Print(arr4);
}
public static int[] Copy(int[] arr){
int[] arrc = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
arrc[i] = arr[i];
}
return arrc;
}
public static void Print(int[] arr){
System.out.print("输入数组的数据为[");
for (int i = 0; i < arr.length; i++) {
if (i!=arr.length-1){ //这几行
System.out.print(arr[i]+","); //写成
}else{ //System.out.print(i==arr.length-1 ?
// arr[i] : arr[i]+",");也对
System.out.print(arr[i]);
}
}
System.out.println("]");
}
}
输出结果:
arr1:[I@1b6d3586
arr2:[I@1b6d3586
arr3:[I@4554617c
输入数组的数据为[1,234,213,512,5,14,513,6,12,61346,13,46,136,1,6]
输入数组的数据为[1,234,213,512,5,14,513,6,12,61346,13,46,136,1,6]
输入数组的数据为[1,234,213,512,5,14,513,6,12,61346,13,46,136,1,6]
输入数组的数据为[]
案例五:评委打分
需求:唱歌节目六位评委给选手打分(0-100之间的整数),选手最后得分为去掉最高分去掉最低分的4位评委平均分;
自写版
发现问题:sout后面写arr[i]+','使用单引号引起逗号时,会计算出逗号的ASCII码将其相加,应该使用双引号引起逗号。
import java.util.Scanner;
public class Marking {
public static void main(String[] args) {
Scanner ns = new Scanner(System.in);
double[] marks = new double[6];
System.out.println("请六位评委依次打分:");
for (int i = 0; i < marks.length; i++) {
System.out.print("第"+(i+1)+"位:");
marks[i] = ns.nextDouble();
}
Score(marks);
}
public static void Score(double[] arr){
if (arr.length>0) {
System.out.print("六位评委打分分别为:【");
double max=arr[0], min = arr[0],sum = 0;
for (int i = 0; i < arr.length; i++) {
max = arr[i]>max? arr[i]:max;
min = arr[i]<min? arr[i]:min;
sum+=arr[i];
System.out.print(i==arr.length-1? (int)arr[i]:(int)arr[i]+"\t");
}
sum = sum-min-max;
double score = sum/4;
System.out.println("】\n去掉一个最高分 "+(int)max+" 和一个最低分 "+(int)min+" 最终平均得分为:"+score);
}
}
}
教程版:
使用int代替double,最后计算阶段
double 平均分 = (总和减最高最低分)*1.0 / (length-2);//length-2代替4,增强复用性。
作为平均分的计算,减少了内存的无效占用,使代码更高效;
案例六:数字加密
需求:4位密码采用加密方式进行传输:规则如下:得到每位数,然后每位数都加上5,再对10取余,最后将所有数字反转,得到一串新数;
自写版:
import java.util.Scanner;
public class MiMa {
public static void main(String[] args) {
int n = 4;
System.out.println("请输入"+n+"位数字密码:");
Scanner ns = new Scanner(System.in);
int mima = ns.nextInt();
if (mima>=1000&&mima<=9999){
int ge = mima%10, shi = mima/10%10, bai = mima/100%10, qian = mima/1000%10;
ge=(ge+5)%10*1000;
shi=(shi+5)%10*100;
bai=(bai+5)%10*10;
qian=(qian+5)%10;
int jiami = ge+shi+bai+qian;
System.out.println("您的密码"+mima+"加密完成,加密后:"+jiami);
}else{
System.out.println("请检查您输入的密码位数,并重新发送请求");
}
}
}
优点:数字一次可以一次输入完成,不用一下一下点回车;缺点,算法复杂,复用性不强;
教程版本:
缺点:需要一位一位输入,
优点:可以改变输入位数,复用性强
亮点:19~22行的交换顺序;
import java.util.Scanner;
public class MiMa {
public static void main(String[] args) {
System.out.print("请输入需要加密的数字个数:");
Scanner ns = new Scanner(System.in);
int length = ns.nextInt();
int[] arr = new int[length];
for (int i = 0; i < arr.length; i++) {
System.out.print("请输入您需要加密的第"+(i+1)+"个数字:");
int numb = ns.nextInt();
arr[i] = numb;
}
PrintArr(arr);
for (int i = 0; i < arr.length; i++) {
arr[i] = (arr[i]+5)%10;
}
PrintArr(arr);
for(int i = 0, j = arr.length-1; i<j; i++,j--){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
PrintArr(arr);
}
public static void PrintArr(int[] arr){
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i==arr.length-1? arr[i]:arr[i]+",");
}
System.out.println("]");
}
}
案例七:双色球
从(1~33)中选六个不重复红球,从(1~16)中选一个蓝球,
0+1,1+1,2+1——六等奖:5元 红+绿=10,11,12
3+1,4+0——五等奖:10元 红+绿=13,4
5+0,4+1——四等奖:200元 红+绿=5,14
5+1——三等奖:3000元 红+绿=15
6+0——二等奖:最高500万 红+绿=6
6+1——一等奖:最高1000万 红+绿=16
自写版:
随机选球,多加一个循环检查重复,重复则i--,重新赋值;
检验中奖使用case(红+绿)
import java.util.Random;
public class TwoColourBall {
public static void main(String[] args) {
int[] yourb = new int[7];
yourb = YourChose();
System.out.print("您的号码:");
Print(yourb);
int[] Bb = new int[7];
Bb = BounceBalls();
System.out.print("中奖号码:");
//这里两个方法程序一样,调用两次也对,省下一遍方法代码
Print(Bb);
Cont(yourb,Bb);
}
public static int[] YourChose(){
Random nr = new Random();
int[] yourb = new int[7];
for (int i = 0; i < yourb.length-1; i++) {
yourb[i] = nr.nextInt(33)+1;
for (int j = 0; i-j>0; j++) {
if (yourb[i]==yourb[j]){
i--;
break;
}
}
}
yourb[yourb.length-1] = nr.nextInt(16)+1;
return yourb;
}
public static int[] BounceBalls(){
Random nr = new Random();
int[] Bb = new int[7];
for (int i = 0; i < Bb.length-1; i++) {
Bb[i] = nr.nextInt(33)+1;
for (int j = 0; i-j>0; j++) {
if (Bb[i]==Bb[j]){
i--;
break;
}
}
}
Bb[Bb.length-1] = nr.nextInt(16)+1;
return Bb;
}
public static void Cont(int[] arr1, int[] arr2){
int r = 0,g = 0;
for (int i = 0; i < arr1.length-1; i++) {
for (int j = 0; j < arr2.length-1; j++) {
if (arr2[j]==arr1[i]){
r++;
}
}
}
if (arr2[arr2.length-1]==arr1[arr1.length-1]){
g = 10;
}
switch (r+g){
case 10:
case 11:
case 12:
System.out.println("恭喜您中奖了!六等奖————奖金5元");
break;
case 4:
case 13:
System.out.println("恭喜您中奖了!五等奖————奖金10元");
break;
case 5:
case 14:
System.out.println("恭喜您中奖了!四等奖————奖金200元");
break;
case 15:
System.out.println("恭喜您中奖了!三等奖————奖金3000元");
break;
case 6:
System.out.println("恭喜您中奖了!二等奖————奖金5000000元");
break;
case 16:
System.out.println("恭喜您中奖了!一等奖————奖金10000000元");
break;
default:
System.out.println("很遗憾,您没有中奖。");
}
}
public static void Print(int[] arr){
System.out.print("红球号码[");
for (int i = 0; i < arr.length-1; i++) {
System.out.print(i==arr.length-2?arr[i]+"]":arr[i]+"\t");
}
System.out.println("绿球号码["+arr[arr.length-1]+"]");
}
}