java实现超市编号购物小程序
String用来定义商品名称
Prise定义各个商品的价格
本人是小白,只能用这种最简单的方法,各位大佬如有更好的方法可以交流学习。
import java.util.Scanner;
public class shops {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String book="书本";
String Price="价格";
int bookPrise=12;
String Pen="铅笔";
int penPrise=1;
String Xp="橡皮";
int xpPrise=2;
String Cola="可乐";
int colaPrise=3;
String foods="零食";
int foodsPrise=5;
int amount=20;
System.out.println("商品名称"+'\t'+'\t'+ '\t' +Price+'\t'+'\n'+"1."+book+'\t'+'\t'+bookPrise+"元"+'\t'+'\n'
+"2."+Pen+'\t'+'\t'+penPrise+"元"+'\t'+'\n'+"3."+Xp+'\t'+'\t'+xpPrise+"元"+'\t'+'\n'+"4."+Cola+'\t'+'\t'+colaPrise+"元"
+'\n'+"5."+foods+'\t'+'\t'+foodsPrise+"元");
System.out.println("请输入商品序列号:");
int one=sc.nextInt();
int[] ont={1,2,3,4,5};
for(int i=1;i< args.length;i++)
{
System.out.println(ont[i]);
}
int n=1;//存储数量
do
{
if (one==ont[1]) {
System.out.print("此商品为:" + book + '\t' + "价格为:" + bookPrise+'元'+'\n'+"找零:"+(amount-bookPrise));
if((amount-bookPrise)>=1)
{
System.out.println('\n'+"你还可以购买:"+Pen+(amount-bookPrise)/penPrise+"个"+'\n'+"剩余:"+
((amount-bookPrise)-((amount-bookPrise)/penPrise))+
"元");
//System.out.print("你还可以购买:"+Cola+Xp+foods+Pen);
}
break;
} else if (one==ont[2]) {
System.out.print("此商品为:" + Pen + '\t' + "价格为:" + penPrise+'元');
System.out.println('\n'+"剩余金额:"+(amount-penPrise)+'\n'+"你还可以购买:"+Xp+((amount-penPrise)/xpPrise)+'个'+
'\n'+"剩余:"+((amount-penPrise)-((amount-penPrise)/xpPrise)));
System.out.println("请输入商品序列号:");
int s=sc.nextInt();
if(amount-penPrise<=12)
{
System.out.println("你还可以购买所有商品");
}
int t=sc.nextInt();
}
else if (one==ont[3]) {
System.out.print("此商品为:" + Xp + '\t' + "价格为:" + xpPrise+'元');
System.out.println('\n'+"剩余金额:"+(amount-xpPrise)+'\n'+"你还可以购买:"+foods+((amount-xpPrise)/foodsPrise)+'个'
+'\n'+"剩余:"+((amount-xpPrise)-(amount-xpPrise)/foodsPrise));
break;
} else if (one==ont[4]) {
System.out.print("此商品为:" + Cola + '\t' + "价格为:" + colaPrise+'元');
System.out.println('\n'+"剩余金额:"+(amount-colaPrise)+'\n'+"你还可以购买:"+Xp+((amount-colaPrise)/colaPrise)+'个');
break;
}
else if (one==ont[5]){
System.out.print("此商品为:" + foods + '\t' + "价格为:" + foodsPrise+'元');
System.out.println('\n'+"剩余金额:"+(amount-foodsPrise)+'\n'+"你还可以购买:"+Pen+((amount-foodsPrise)/penPrise)+'个');
break;
}
else
{
System.out.println("序列号无效,请重新选择商品");
}
} while(one<=5);
}
}