买飞机票
import java.util.Scanner;
//买飞机票
public class demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入原价,月份,类型");
int money = sc.nextInt();
int month = sc.nextInt();
String type = sc.next();
double rs = calc(money,month,type);
System.out.println("最终价格是:" + rs);
}
public static double calc(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;
}
} else if (month == 11 || month == 12 || month <= 4 || month >= 1) {
switch (type) {
case "经济舱":
money *= 0.65;
break;
case "头等舱":
money *= 0.7;
break;
default:
System.out.println("请输入正确仓位");
money = -1;
}
} else {
System.out.println("月份有问题");
money = -1;
}
return money;
}
}
找素数
//找素数
public class demo {
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");
}
}
}
}
开发验证码
import java.util.Random;
//开发验证码
public class demo {
public static void main(String[] args) {
String code = createCode(5);
System.out.println("验证码为" + code);
}
public static String createCode(int n){
Random r = new Random();
String code = "";
for (int i = 0; i < n; i++) {
int type = r.nextInt(3);
switch (type){
case 0:
//大写字母A(65),Z(65+25)
char ch = (char) (r.nextInt(26) + 65);
code += ch;
break;
case 1:
//小写字母A(97),Z(97+25)
char ch1 = (char) (r.nextInt(26) + 97);
code += ch1;
break;
case 2:
code += r.nextInt(10);// 0-9
break;
}
}
return code;
}
}
数组元素复制
public static void main(String[] args) {
int[] arr1 = {0,1,2,3};
int[] arr2 = new int[arr1.length];
copy(arr1,arr2);
printArray(arr1);
printArray(arr2);
}
public static void copy(int[] arr1, int[] arr2){
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
}
public static void printArray(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
评委打分
public static void main(String[] args) {
int[] scores = new int[6];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.println("输入第" + (i + 1) + "个评委的分数");
scores[i] = sc.nextInt();
}
int sum = 0;
int max = scores[0];
int min = scores[0];
for (int i = 0; i < scores.length; i++) {
if (max < scores[i]){
max = scores[i];
}
if (min > scores[i]){
min = scores[i];
}
sum += scores[i];
}
double result = (sum - max - min) * 1.0 / (scores.length - 2);
System.out.println("平均分为 :" + result);
}