Java学习 练习题

水果结算程序
import java.util.Scanner;

public class Fruit {
// 水果结算程序 IPO:输入 处理 输出
// 普通顾客 12元/kg 会员价 11元/kg
// 买满 10kg * 0.8 优惠券 满100 -10 200 -30 300 -50 500以上 -90
// 一开始提示输入

public static void main(String[] args) {

for (int i = 1; i > 0; i++) // 用循环 控制输入 可以一直输入 并且计算价格
{

Scanner input = new Scanner(System.in);
System.out.println("输入 1 或 0(1是会员 0不是会员):");
int number = input.nextInt();

double price = 0; //定义一个price 以防后续报错
double weight = 0;

if (number == 1)
price = 11;

else if(number == 0)
price = 12;
else
System.out.println("error");

//会员称重
if (number == 1) {
System.out.println("您是尊贵的苹果VIP");
System.out.println("请输入苹果的重量(kg):");
weight = input.nextDouble();
price = price * weight;
}
//非会员称重
else if (number == 0)
{
System.out.println("您不是苹果VIP");
System.out.println("请输入苹果的重量(kg):");
weight = input.nextDouble();
price = price * weight;
}

if (weight >= 10) // 重量大于10kg 可以享受八折优惠
price *= 0.8;

// 满减折扣
double discount = 0;
if
((price >= 100) && (price < 200))
discount= 10;

else if ((price >= 200) && (price < 300))
discount= 30;

else if ((price >= 300) && (price < 500))
discount= 50;

else if (price >= 500) //此处如果用 else 0-100 也会按照减去90的优惠处理
discount= 90;

price-= discount;

System.out.println("您应该支付的金额为:" + price);

}
}
}
posted @ 2022-09-14 09:08  luyuan66  阅读(38)  评论(0)    收藏  举报