编程练习
1.买机票
public class test1 {
public static void main(String[] args) {
//1.用户输入月份,票价,仓位类型
System.out.println("请输入月份:");
Scanner sc=new Scanner(System.in);
int month=sc.nextInt();
System.out.println("请输入票价:");
double price=sc.nextDouble();
System.out.println("请输入仓位类型");
String type=sc.next();
double money=calcPrice(month,price,type);
System.out.println("打折后的机票价格为:"+money);
}
//计算票价
public static double calcPrice(int month,double price,String type){
//区分月份淡季、旺季
if(month>=5&&month<=10){
//旺季:头等舱9折,经济舱8.5折
switch (type){
case "头等舱":
price*=0.9;
break;
case "经济舱":
price*=0.85;
break;
default:
System.out.println("仓位类型输入有误");
price=-1;
}
} else if (month==11||month==12||month>=1&&month<=4) {
//淡季:头等舱7折,经济舱6.5折
switch(type){
case "头等舱":
price*=0.7;
break;
case "经济舱":
price*=0.65;
break;
default:
System.out.println("仓位类型输入有误");
price=-1;
}
}else{
System.out.println("月份信息输入错误");
price= -1; //数据有误,无法计算
}
return price;
}
}
2.随机生成n位验证码(大小写字母、数字)
public static String createCode(int n){
String code="";
Random r=new Random();
for (int i = 0; i < n; i++) {
//三种情况:1大写字母 2小写字母 3数字,三种情况随机选择
int type=r.nextInt(3)+1;
switch (type){
case 1:
//A(65)~Z(65+25)
char a= (char) (r.nextInt(26)+65); //快捷键alt+Enter格式转换
code+=a;
break;
case 2:
//a(97)~z(97+25)
char b= (char) (r.nextInt(26)+97);
code+=b;
break;
case 3:
//0~9
int c=r.nextInt(10);
code+=c;
break;
}
}
return code;
}
3.双色球案例
public class test4 {
public static void main(String[] args) {
int[] lucknums=luckNum();
System.out.println("请输入购买的双色球号码:");
int[] usernums=userNum();
System.out.println("您购买的号码是:");
print(usernums);
System.out.println("中奖号码是:");
print(lucknums);
judge(lucknums,usernums);
}
//1随机生成中奖号码:6个红(1~33不能重复),1个蓝
public static int[] luckNum(){
int[] num=new int[7];
Random r=new Random();
//生成前六个红号:
for (int i = 0; i < num.length-1; i++) {
//要判断生成的data,之前知否出现过,若出现则重新生成,否则直接赋值给数组
//当前第i个数据,用f标记:true出现过,false没出现过
while(true){
int data=r.nextInt(33)+1;
boolean f=false;
for (int j = 0; j <i ; j++) {
if(data==num[j]){
f=true;
break;
}
}
//根据f判断
if(f==false){
num[i]=data;
break;
}
}
}
//生成第7个蓝色号码:1~16
num[6]=r.nextInt(16)+1;
return num;
}
//2生成用户选择的号码
public static int[] userNum(){
int[] num=new int[7];
Scanner sc=new Scanner(System.in);
System.out.println("请输入前6个红球号码(1~33):");
for (int i = 0; i < num.length-1; i++) {
System.out.println("第"+(i+1)+"个:");
num[i]=sc.nextInt();
}
System.out.println("请输入1个蓝球号码(1~16):");
num[6]=sc.nextInt();
return num;
}
//3中奖号与用户号比对
public static void judge(int[] l,int[] u){
//中的红球数
int redNums=0;
for (int i = 0; i < l.length-1; i++) {
for (int j = 0; j < l.length-1; j++) {
if(u[i]==l[j]){
redNums++;
break;
}
}
}
//蓝球是否中
int blueNums=0;
if(u[6]==l[6]){
blueNums++;
}
//判断中奖情况
if(blueNums==1&&redNums<=2){
System.out.println("中5元");
} else if (blueNums==1&&redNums==3||blueNums==0&&redNums==4) {
System.out.println("中10元");
} else if (blueNums==1&&redNums==4||blueNums==0&&redNums==5) {
System.out.println("中200元");
} else if (blueNums==1&&redNums==5) {
System.out.println("中3000元");
} else if (redNums==6) {
System.out.println("中500w元");
} else if (redNums==6&&blueNums==1) {
System.out.println("中1000w元");
}else {
System.out.println("没中奖");
}
}
//打印
public static void print(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("]");
}
标签:int,编程,Day3,System,mt,println,month,public,out
From: https://www.cnblogs.com/mt-nika/p/17041547.html