1 飞机票
代码实现:
import java.util.Scanner;
public class F1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入票价: ");
double jia = input.nextDouble();
System.out.print("请输入月份: ");
int yue = input.nextInt();
System.out.print("请输入你所坐飞机舱(经济舱或头等舱) ");
String can = input.next();
methodjia(jia,yue,can);
}
private static void methodjia(double jia, int yue, String can) {
if (yue>=5 && yue<=10){
if (can.equals("经济舱")){
jia=jia*0.85;
}else if(can.equals("头等舱")) {
jia = jia * 0.9;
}else
System.out.println("输入错误");
return;
} else if((yue>10 && yue<=12) || (yue<5 && yue>0)){
if (can.equals("头等舱")){
jia=jia*0.7;
}else if(can.equals("经济舱")){
jia=jia*0.65;}
else
System.out.println("输入错误");
return;
} else if(yue<0||yue>12){
System.out.println("错误");
return;}
System.out.println(jia);
}
}
2 生成一个五位数验证码
代码实现:
import java.util.Random;
public class Yzm {
public static void main(String[] args) {
char []arr = new char[52];
Random rand = new Random();
for (int i = 0; i < 26; i++) {
arr[i] = (char) (65+i);
}
for (int i = 0; i <26; i++) {
arr[i+26] = (char) (97+i);
}
String s ="";
for (int i = 0; i < 4; i++) {
char k = arr[rand.nextInt(52)];
s=s+k;
}
int i = rand.nextInt(10);
s=s+i;
System.out.println(s);
}
}
3 数字加密
代码实现:
import java.util.Scanner;
public class Jm {
public static void main(String[] args) {
int num = new Scanner(System.in).nextInt();//输入一个数组
jiami(intToArray3(num));//方法调用
}
private static void jiami(int[] arr) {//加密方法每个位数+5,每个位数对10取模,再倒序
for (int i = 0; i < arr.length; i++) {
arr[i]+=5;
}
for (int i = 0; i < arr.length; 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 num = 0;
for (int i = 0; i < arr.length; i++) {
num= num*10+arr[i];
}
System.out.println(num);
}
private static int[] intToArray3(int num){
int len = String.valueOf(num).length();//得到这个数的长度以便创建数组
int[] resultArray = new int[len];//创建数组
while (num != 0){//给数组一一赋值
resultArray[len-1] = num % 10;
num = num / 10;
len--;
}
return resultArray;//返回位数组成的数组
}
}