public static void main(String[] args) {
// TODO Auto-generated method stub
int price = 0;
int sum = 0;
int count = 1;
System.out.print("输入余额");
int money = scanner();
System.out.println("一共" + money + "元");
String [][] arrarr = {
{"1.小米","1999"},
{"2.苹果","2999"},
{"3.联想","3999"},
{"4.vivo","4999"}
};
System.out.println("==================");
for(int i = 0; i < arrarr.length; i ++) {
count ++;
}
for(int i = 0; i < arrarr.length; i ++) {
for(int j = 0; j < arrarr[i].length; j ++)
System.out.print(arrarr[i][j]);
System.out.println();
}
System.out.println("==================");
System.out.println("请选择商品序号");
System.out.print("0 结算");
while(true) {
int num = scanner();
if(num < count && num > 0) {
System.out.print("请继续选择商品序号");
price = total(arrarr,num);
sum = sum + price;
}
else if (num == 0){
System.out.println("==================");
System.out.println("一共" + sum + "元");
if(money(money, sum) >= 0) {
System.out.print("您还剩余 :" + money(money, sum) + "元");
break;
}
else {
System.out.println("余额不足,请充值");
}
}
else {
System.out.print("请输入正确商品序号");
continue;
}
}
}
public static int scanner() {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
return num;
}
public static int total(String [][] arrarr, int num) {
int result = Integer.parseInt(arrarr[num-1][1]);
int sum = 0;
sum = sum + result;
return sum;
}
public static int money(int mon, int total) {
int m = 0;
m = mon - total;
return m;
}
}
=========================
其中字符串转整形baidu了一下,可以用 Integer.parseInt(String)函数表示
2022.10.25