首页 > 解决方案 > 为什么我的输出没有显示我的输入?

问题描述

我不确定需要做什么,以便“采购摘要”的输出显示“汽油类型”。任何帮助表示赞赏。

更新:完整代码。“字符串类型 = input.nextLine();” 在运行时,汽油类型似乎没有在购买摘要中注册。我不确定其中有什么问题。


class PetrolPurchase  
{
    private String station;
    private double quant;
    private String type;
    private double price;
    private int discount;
    
    public String getstation () {return station;}
    public double getquant () {return quant;}
    public String gettype () {return type;}
    public double getprice () {return price;}
    public int getdiscount () {return discount;}
    
    public void setstation (String otherstation) {station = otherstation;}
    public void setquant (double otherquant) {quant = otherquant;}
    public void settype (String othertype) {type = othertype;}
    public void setprice (double otherprice) {price = otherprice;}
    public void setdiscount (int otherdiscount) {discount = otherdiscount;}
    
        
}

class PurchaseSummary 
{ public static void main (String [ ] args)
    {       
Scanner input = new Scanner (System.in); 

        PetrolPurchase pp = new PetrolPurchase  ();
        
        System.out.printf ("Enter Station:");
        String station = input.nextLine();
        pp.setstation (station);
        
        System.out.printf("Enter Quantity (Liter):");
        double quant = input.nextDouble();
        pp.setquant (quant);
        
        System.out.printf ("Enter Petrol Type:");
        String type = input.nextLine();
        pp.settype (type);
        input.nextLine();
        
        System.out.printf ("Enter the Petrol Price:");
        double price = input.nextDouble();
        pp.setprice (price);
        
        System.out.printf("Enter the discount (no decimal):");
        int discount = input.nextInt();
        pp.setdiscount (discount); 
        
        double truecost = quant * price;
        double dcost = discount*0.01*truecost;
        double pay = truecost - dcost;
        
        
        System.out.printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%n");
        System.out.printf("Purchase Summary:%n");
        System.out.printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%n");
        System.out.printf ("Station: %s%n", station);
        System.out.printf ("Total Petrol Liter: %s%n", pp.getquant ());
        System.out.printf ("Petrol Type: %s%n", pp.gettype());
        System.out.printf ("Price Per Liter: = %.2f%n", pp.getprice ());
        System.out.printf ("Actual Cost = %.2f%n", truecost);
        System.out.printf ("Discounted Amount %s", discount);
        System.out.printf ("%% is: %-8.5s", dcost);
        System.out.printf ("%nAmount for Payment = %.2f%n", pay);

输出

标签: java

解决方案


尝试把input.nextLine();之前type=input.nextLine();


推荐阅读