案例一 买飞机票
package com.liu.demo;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票原价");
double money = sc.nextDouble();
System.out.println("请输入机票月份");
int month = sc.nextInt();
System.out.println("请选择仓位选择");
String type = sc.next();
System.out.println("机票优惠后的价格是" + calc(money,month,type));
}
public static double calc(double money,int month, String type ){
if (month >= 5 && month <=10){
switch (type){
case "头等舱":
money *= 0.9;
break;
case "经济舱":
money *= 0.9;
break;
default:
System.out.println("输入仓位类型有误");
money=-1;
}
}else if(month == 11 || month ==12 || month >=1 && month <= 4){
switch (type){
case "头等舱":
money *= 0.7;
break;
case "经济舱":
money *= 0.65;
break;
default:
System.out.println("输入仓位类型有误");
money=-1;
}
}else{
System.out.println("输入的月份有误");
money = -1;
}
return money;
}
}
案例二 找素数
package com.liu.demo;
public class Test02 {
public static void main(String[] args) {
for (int i = 101; i <= 200; i++){
boolean flag = true;
for(int j = 2; j < i / 2; j++){
if(i%j == 0){
flag = false;
break;
}
}
if (flag){
System.out.print(i + "\t");
}
}
}
}
案例三 定义方法随机实现用一个5位的验证码,可以是数字,大写字母,小写字母。
package com.liu.demo;
import java.util.Random;
public class Test03 {
public static void main(String[] args) {
String code = creatCode(5);
System.out.println(code);
}
public static String creatCode(int n){
String code = "";
Random r = new Random();
for (int i = 0; i < n; i++){
int type = r.nextInt(3);
switch (type){
case 0:
char ch = (char) (r.nextInt(26) + 65);
code += ch;
break;
case 1:
char ch1 = (char) (r.nextInt(26) + 97);
code += ch1;
break;
case 2:
code += r.nextInt(10);
break;
}
}
return code;
}
}
数组元素的复制
package com.liu.demo;
public class test04 {
public static void main(String[] args) {
int[] arr1 = {11, 22, 33, 44};
int[] arr2 = new int[arr1.length];
copy(arr1, arr2);
printArray(arr1);
printArray(arr2);
}
public static void printArray(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("]");
}
public static void copy(int[] arr1, int[] arr2){
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
}
}