首页 > 解决方案 > 如果客户支付的费用低于票价

问题描述

帮帮我,我试过成功,程序但这里有错误可以吗?哪位解决后发送,如果客户支付的低于票价,我想打印低于票价的钱并打印剩余的支付,或者如果客户支付的超过票价,剩下的钱打印给客户,最后如果他支付了票款,你打印欢迎

    import java.util.Scanner;

 public class Flow {

    static Scanner input = new Scanner(System.in);
    static int age;
    static double ticketpricetax = 0.15 * 100 + 10;
    public static double remainigamount = ticketpricetax;
    public static double howmuchpay = 10;

    public static void main(String[] args) {

        System.out.println("Age");
        int age = input.nextInt();
        if (age > 12 || age <= 50) {
            System.out.println("ticket price is ");
            System.out.println(ticketpricetax);
            System.out.println("Please pay your ticket");
        }
        input.nextInt();
        if (ticketpricetax > howmuchpay) {
            System.out.println("the money less than ticket price");
        } else if (ticketpricetax > howmuchpay) {
            System.out.println("the money is greater than ticket price");
        } else {
            System.out.println("welcome");
        }
    }
}

标签: javaif-statement

解决方案


您的价格输入未存储在任何变量中,它应该是 double 而不是 int,然后您将 ticketpricetax 和 howmuchpay 与其初始化值进行比较,howmuchpay 不会随输入更新。此外,您进行了不正确的检查并遗漏了一些陈述。这是更正:

import java.util.Scanner;
public class Flow {

static Scanner input = new Scanner(System.in);
static int age;
static double ticketpricetax = 0.15 * 100 + 10;
public static double howmuchpay = 10;

public static void main(String[] args) {

    System.out.println("Age");
    int age = input.nextInt();
    if (age > 12 || age <= 50) {
        System.out.println("ticket price is ");
        System.out.println(ticketpricetax);
        System.out.println("Please pay your ticket");
    }
    howmuchpay = input.nextDouble();
    if (ticketpricetax > howmuchpay) {
        System.out.println("the money less than ticket price");
        System.out.println("rest of the money to pay = "+(ticketpricetax-howmuchpay)); 
    } else if (ticketpricetax < howmuchpay) {
        System.out.println("the money is greater than ticket price");
        System.out.println("rest of the money = "+(howmuchpay-ticketpricetax));
    } else {
        System.out.println("welcome");
    }
}
}

推荐阅读