目录
如果文章对您有帮助,就拿起小手赶紧给博主点赞💚评论❤️收藏💙 一下吧~~~赶紧动起来,让我们一起加油学习吧!
💙 💜 ❤️ 💚💙 💜 ❤️ 💚💙 💜 ❤️ 💚💙 💜 ❤️💙 💜 ❤️ 💚💙 💜 ❤️ 💚💙 💜 ❤️ 💚
🐋评委打分
分析:
代码实现:
public static void main(String[] args) {
//1、数据准备
//1.1、定义int变量保存评委分、最高分、最低分
int p1,p2,p3,p4,p5,p6,max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
//1.2、定义随机数变量
Random r = new Random();
//1.3、定义变量保存最终得分
double finalScore;
//1.4、定义变量保存日期
Date d = new Date();
//1.5、定义变量保存拼接后的日期字符串
String dateStr="";
//1.6、定义字符串,保存最终展示的所有内容
String result="";
//2、数据运算
//2.1、生成6个随机分数保存到评分中
p1 = r.nextInt(101);
p2 = r.nextInt(101);
p3 = r.nextInt(101);
p4 = r.nextInt(101);
p5 = r.nextInt(101);
p6 = r.nextInt(101);
//System.out.println(p1+"\t"+p2+"\t"+p3+"\t"+p4+"\t"+p5+"\t"+p6);
//2.2、计算最高分
max = max>=p1?max:p1;
max = max>=p2?max:p2;
max = max>=p3?max:p3;
max = max>=p4?max:p4;
max = max>=p5?max:p5;
max = max>=p6?max:p6;
//System.out.println(max);
//2.3、计算最低分
min = min<=p1?min:p1;
min = min<=p2?min:p2;
min = min<=p3?min:p3;
min = min<=p4?min:p4;
min = min<=p5?min:p5;
min = min<=p6?min:p6;
//System.out.println(min);
//2.4、计算最终得分
finalScore = (p1+p2+p3+p4+p5+p6-max-min)/4.0;
//System.out.println(finalScore);
//2.5、计算年月日字符串
dateStr+=d.getYear()+1900+"-";
dateStr+=(d.getMonth()+1)<=9?"0"+(d.getMonth()+1)+"-":(d.getMonth()+1)+"-";
dateStr+=d.getDate()<=9?"0"+d.getDate():d.getDate()+"";
//System.out.println(dateStr);
//2.6、拼接最终展示效果
result+="今日("+dateStr+")6位评委的分数分别为:"+p1+"、"+p2+"、"+p3+"、"+p4+"、"+p5+"、"+p6+"\r\n";
result+="去掉一个最高分"+max+"、去掉一个最低分"+min+"\r\n";
result+="最终得分为:"+finalScore+"分";
//3、结果展示
System.out.println(result);
}
💙 💜 ❤️ 💚💙 💜 ❤️ 💚💙 💜 ❤️ 💚💙 💜 ❤️💙 💜 ❤️ 💚💙 💜 ❤️ 💚💙 💜 ❤️ 💚
🐋超市收银管理(V3)
1)需求说明
模拟真实购物逻辑,以下述图片为需求原型,根据不同购物物品完成购物小票内容打印到控制台。
🌲 知识讲解—多位补0操作
两位数补0: x<=9?"0"+x:x+"";
多位数补0:代码实现
public static void main(String[] args) {
//多位补0
//不足6位 补0
int r = 9;//
String result1;
//两种补0方式:int (不建议,繁琐)
//10000~99999 补一个0
//1000~9999 补两个0
//100~999 补三个0
//10~99 补四个0
//0~9 补五个0
result1 = (r>=10000&&r<=99999)?"0"+r:(
(r>=1000&&r<=9999)?"00"+r:(
(r>=100&&r<=999)?"000"+r:(
(r>=10&&r<=99)?"0000"+r:(
(r>=0&&r<=9)?"00000"+r:r+""
)
)
)
);
System.out.println(result1);
System.out.println("-----------------");
//两种补0方式:String
String str = "599999";
//.length() ==5 补1个0
//.length() ==4 补2个0
//.length() ==3 补3个0
//.length() ==2 补4个0
//.length() ==1 补5个0
String result = str.length()==5?"0"+str:(
str.length()==4?"00"+str:(
str.length()==3?"000"+str:(
str.length()==2?"0000"+str:(
str.length()==1?"00000"+str:str
)
)
)
);
System.out.println(result);
}
🌲 超市收银管理—代码实现
public static void main(String[] args) {
//1、----展示所有商品信息
//1.0、定义Random、Scanner变量
Random r = new Random();
//接收数字
Scanner sc1 = new Scanner(System.in);
//接收字符串
Scanner sc2 = new Scanner(System.in);
//1.1、定义变量,保存商品信息
String pid1,pid2,pid3,
pname1,pname2,pname3;
double price1,price2,price3;
int num1,num2,num3;
//1.2、控制台信息录入
System.out.println("\t\t欢迎进入超市收银管理V3");
System.out.println("首先,请输入所有的商品信息:");
System.out.println("请输入第一个商品的名称:");
pname1 = sc2.nextLine();
System.out.println("请输入第一个商品的售价:");
price1=sc1.nextDouble();
System.out.println("请输入第二个商品的名称:");
pname2 = sc2.nextLine();
System.out.println("请输入第二个商品的售价:");
price2=sc1.nextDouble();
System.out.println("请输入第三个商品的名称:");
pname3 = sc2.nextLine();
System.out.println("请输入第三个商品的售价:");
price3=sc1.nextDouble();
//1.3、pid编号生成
pid1=r.nextInt(100000)+"";
pid2=r.nextInt(100000)+"";
pid3=r.nextInt(100000)+"";
pid1= pid1.length()==5?"0"+pid1:(
pid1.length()==4?"00"+pid1:(
pid1.length()==3?"000"+pid1:(
pid1.length()==2?"0000"+pid1:(
pid1.length()==1?"00000"+pid1:pid1
)
)
)
);
pid2= pid2.length()==5?"0"+pid2:(
pid2.length()==4?"00"+pid2:(
pid2.length()==3?"000"+pid2:(
pid2.length()==2?"0000"+pid2:(
pid2.length()==1?"00000"+pid2:pid2
)
)
)
);
pid3= pid3.length()==5?"0"+pid3:(
pid3.length()==4?"00"+pid3:(
pid3.length()==3?"000"+pid3:(
pid3.length()==2?"0000"+pid3:(
pid3.length()==1?"00000"+pid3:pid3
)
)
)
);
//1.4、第一次数据展示
System.out.println("编号\t\t品名\t\t\t售价");
System.out.println(pid1+"\t\t"+pname1+"\t\t"+price1);
System.out.println(pid2+"\t\t"+pname2+"\t\t"+price2);
System.out.println(pid3+"\t\t"+pname3+"\t\t"+price3);
//2、----计算并展示小票信息
//2.0、定义日期
Date d = new Date();
String dateStr = "";
//2.1、定义变量,保存商品信息,保存统计信息,定义Scanner变量
double count1,count2,count3,total;
int totalNum;
//2.2、接收商品购买数量
System.out.println("请您输入"+pid1+"购买数量");
num1=sc1.nextInt();
System.out.println("请您输入"+pid2+"购买数量");
num2=sc1.nextInt();
System.out.println("请您输入"+pid3+"购买数量");
num3=sc1.nextInt();
//2.3、计算数据
count1 = price1*num1;
count2 = price2*num2;
count3 = price3*num3;
total=count1+count2+count3;
totalNum = num1+num2+num3;
//2.4、拼接年月日、时分秒
dateStr+=(d.getYear()+1900)+"-";
dateStr+=((d.getMonth()+1)<=9?"0"+(d.getMonth()+1):(d.getMonth()+1))+"-";
dateStr+=(d.getDate()<=9?"0"+d.getDate():d.getDate())+" ";
dateStr+=(d.getHours()<=9?"0"+d.getHours():d.getHours())+":";
dateStr+=(d.getMinutes()<=9?"0"+d.getMinutes():d.getMinutes())+":";
dateStr+=d.getSeconds()<=9?"0"+d.getSeconds():d.getSeconds();
//2.4、展示
System.out.println("\t\t欢 迎 光 临");
System.out.println("\t\t编号\t\t品名\t\t售价\t\t数量\t\t金额");
System.out.println("-\t-\t-\t-\t-\t-\t-\t-\t");
System.out.println("\t\t"+pid1+"\t\t"+pname1+"\t"+price1+"\t\t\t"+num1+"\t\t\t"+count1);
System.out.println("\t\t"+pid2+"\t\t"+pname2+"\t"+price2+"\t\t\t"+num2+"\t\t\t"+count2);
System.out.println("\t\t"+pid3+"\t\t"+pname3+"\t"+price3+"\t\t\t"+num3+"\t\t\t"+count3);
System.out.println("-\t-\t-\t-\t-\t-\t-\t-\t");
System.out.println("\t\t3 项商品\t\t共计:"+totalNum+"件");
System.out.println("\t\t总计:"+total);
System.out.println("\t\t"+dateStr);
System.out.println("凭此小票换取发票!");
}