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;//返回位数组成的数组
}
}
4 数字解密
import java.util.*;
public class Jiemi {
public static void main(String[] args) {
int num = new Scanner(System.in).nextInt();
jie(int01(num));
}
private static void jie(int[] arr){
for (int i = 0,j=arr.length-1; i < j; i++,j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for (int i = 0; i < arr.length; i++) {
if (arr[i]+10<15){
arr[i] = arr[i]+10;
}
}
// 4 3 2 1 0 9 8 7 6 5
// 14 13 12 11 10 9 8 7 6 5
// 9 8 7 6 5 4 3 2 1 0
for (int i = 0; i < arr.length; i++) {
arr[i]-=5;
}
int shuchu = 0;
for (int i = 0; i < arr.length; i++) {
shuchu= shuchu*10+arr[i];
}
System.out.println(shuchu);
}
private static int[] int01(int num){
int len = String.valueOf(num).length();
int[] arr = new int[len];
while (num!=0){
arr[len-1] = num%10;
num/=10;
len--;
}
return arr;
}
}
5 抢红包
User类创建
public class User {
private String name;
private int money;
public User(){
}
public User(String name,int money){
this.name=name;
this.money=money;
}
public void show(){
System.out.println("Name:"+name+" Money:"+money);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
Manage类
import java.util.ArrayList;
public class Manager extends User{
public Manager(String name, int money) {
super(name, money);
}
public Manager() {
super();
}
//需要一个集合用来存储发红包的个数以及各个金额
public ArrayList<Integer> List(int totalmoney,int count){//发的钱
ArrayList<Integer> list = new ArrayList<>();
int leftmoney = super.getMoney();//群主总共的钱
if(leftmoney < totalmoney) return list;//群主没那么多钱
int averagemoney = totalmoney/count;//平均每个人
int modemoney = totalmoney%count;//除不尽的余数加在list最后一个
for(int i = 0 ; i < count-1 ; i++){
list.add(averagemoney);
}
super.setMoney(leftmoney - totalmoney);//群主剩的钱
list.add(modemoney+averagemoney);
return list;//返回红包集合
}
}
Member类
import java.util.ArrayList;
import java.util.Random;
public class Member extends User{
public Member() {
super();
}
public Member(String name, int money) {
super(name, money);
}
public void receive(ArrayList<Integer> list){
int random = new Random().nextInt(list.size());//随即索引
int num = list.remove(random);//将这个数去除,并且取出
int money = super.getMoney()+num;//把钱加上
super.setMoney(money);//更新前的金额
}
}
调用
import java.util.ArrayList;
import java.util.Scanner;
public class Using {
public static void main(String[] args) {
Member A= new Member("贤哥",0);
Member B = new Member("超哥",0);
Member C = new Member("小哥",0);
Manager qun = new Manager("群主",10000);
System.out.println("原来的钱");
qun.show();
A.show();
B.show();
C.show();
System.out.println("================");
System.out.println("你想发多少");
int F = new Scanner(System.in).nextInt();
ArrayList<Integer> list = qun.List(F,3);
A.receive(list);
B.receive(list);
C.receive(list);
qun.show();
A.show();
B.show();
C.show();
}
}
运行结果:
5 双色球系统
代码实现:
package itheima;
import java.util.Scanner;
import java.util.Random;
public class seven {
public static void main(String[] args) {
int [] arr = creatarr();
int [] arr1 = maicaipiao();
jiage(find(arr,arr1),zuihouyige(arr,arr1));
}
private static void jiage(int num,int lannum){
int hongnum = num-lannum;
if (hongnum>=0 && lannum==1 &&hongnum<3){
System.out.println("恭喜中奖5元");
}else if ((hongnum==4&&lannum==0)||(hongnum==3 && lannum==1)){
System.out.println("恭喜中奖10元");
}else if ((hongnum==4&&lannum==1)||(hongnum==5 && lannum==0)){
System.out.println("恭喜中奖200元");
} else if(hongnum==5 && lannum==1){
System.out.println("恭喜中奖3000元");
}else if(hongnum==6 && lannum==0){
System.out.println("恭喜中奖500 0000元");
} else if (hongnum==6 && lannum==1) {
System.out.println("恭喜中奖1000 0000元");
}else
System.out.println("一分钱没中");
}
private static int zuihouyige(int [] arr1,int [] arr2){
int lannum=0;
if (arr1[6]==arr2[6]){
lannum=1;
}
return lannum;
}
private static int[] maicaipiao(){
Scanner P = new Scanner(System.in);
//int[] arr = new int[]{P.nextInt(),P.nextInt(),P.nextInt(),P.nextInt(),P.nextInt(),P.nextInt(),P.nextInt()};
int[] arr = new int[7];
for (int i = 0; i < 6;) {
System.out.println("输入第"+(i+1)+"个数:");
int num = P.nextInt();
if (num >0 && num<34) {
if (isPrime(arr,num)){
arr[i] = num;
i++;
}else
System.out.println("已经存在");
}else
System.out.println("输入范围错误");
}
System.out.println("输入第7个数:");
while(true){
int num7= P.nextInt();
if (num7>0 && num7<17) {
arr[6] = num7;
for (int i : arr) {
System.out.print(i+" ");
}
System.out.println(" ");
return arr;
}else
System.out.println("范围错误");
}
}
private static int[] creatarr() {
int[] arr = new int[7];//七色球
Random rand = new Random();
for (int i = 0; i < arr.length-1;) {
int suinum = rand.nextInt(33)+1;//前六个数1-33
if (isPrime(arr, suinum)) {
arr[i] = suinum;
i++;
}
}
arr[arr.length-1] = rand.nextInt(16)+1;//第七个数1-16
return arr;
}
private static boolean isPrime(int []arr,int n) {//是不是已经出现没有返回true
for (int i : arr) {
if (n==i){
return false;
}
}return true;
}
private static int find(int[] arr1, int[] arr2) {//多少个相同的
int i = 0, j = 0;
int count = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] == arr2[j]) {
count++;
i++; // 移动 arr1 的指针
j++; // 移动 arr2 的指针
} else if (arr1[i] < arr2[j]) {
i++;
} else {
j++;
}
}
return count;
}
}