購物結算,購物車結算代碼
package card;
import java.util.Scanner;
public class Shoping {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("依次輸入購買物品,單價,個數:");
//第一件商品,單價,個數
String name1 = input.next();
float price1 = input.nextFloat();
int num1 = input.nextInt();
float sum1 = price1*num1;
//第二件商品,單價,個數
String name2 = input.next();
float price2 = input.nextFloat();
int num2 = input.nextInt();
float sum2 = price2*num2;
//第三件商品,單價,個數
String name3 = input.next();
float price3 = input.nextFloat();
int num3 = input.nextInt();
float sum3 = price3*num3;
System.out.println("實際交費:");
float money = input.nextFloat(); //交費
float allSum = sum1+sum2+sum3; //消費總金額
float p=money-allSum; //找零
if(p<0){
System.out.println("\t\t金額不足,支付失敗");
}
else{
System.out.println("************消費單************");
System.out.println("購買物品\t單價\t個數\t金額");
System.out.println(name1+"\t"+price1+"\t"+num1+"\t"+sum1);
System.out.println(name2+"\t"+price2+"\t"+num2+"\t"+sum2);
System.out.println(name3+"\t"+price3+"\t"+num3+"\t"+sum3);
System.out.println("============================");
System.out.println("消費總金額\t\t\t"+money);
System.out.println("實際交費\t\t\t"+allSum);
System.out.println("找錢\t\t\t"+p);
}
input.close();
}
}