Java 我的第二个if和elseif语句产生了错误的答案

Java 我的第二个if和elseif语句产生了错误的答案,java,if-statement,Java,If Statement,由于某些原因,我无法获得4张等同于100的展台票的正确代码(没有oap/儿童折扣和超过5张票的折扣,4顿晚餐的费用使总数达到180,然后乘以1.21等于217.9。如果我使用System.out.println(vatTotalNoAgeLess5);在第二个if块外,它将打印217.8,但如果我不打印,它将给我4张oap/儿童票+4顿晚餐的费用*1.21,即195.65 public static void main(String[] args) { //data dictionary

由于某些原因,我无法获得4张等同于100的展台票的正确代码(没有oap/儿童折扣和超过5张票的折扣,4顿晚餐的费用使总数达到180,然后乘以1.21等于217.9。如果我使用System.out.println(vatTotalNoAgeLess5);在第二个if块外,它将打印217.8,但如果我不打印,它将给我4张oap/儿童票+4顿晚餐的费用*1.21,即195.65

public static void main(String[] args) {
    //data dictionary
    double standT, terraceT, grossTick, handlingFee, ticketDiscount, flatFee, totalCost, agetick, over5tick,
    vatTotal, totaltick, dinnerPrice, dinnerTotal, vatTotalForAge, vatTotalNoAgeLess5;

    Scanner in = new Scanner(System.in);
    //gathering basic inputs from user  
    System.out.println("Input your name ");
    String name = in.next();
    System.out.println("Input your mobile number ");
    String mobile = in.next();
    System.out.println("How many stand tickets do you want? ");
    int standTick = in.nextInt();
    System.out.println("How many terrace tickets do you want? ");
    int terraceTick = in.nextInt();
    System.out.println("OAP/Student tickets? y/n");
    String ageDiscount = in.next();
    //basic fees as variables
    standT = 25 * standTick;
    terraceT = 20 * terraceTick;
    handlingFee = 7.50;
    dinnerPrice = 20;

    //defining the total cost variables
    grossTick = terraceT + standT;

    if(ageDiscount.equals("Y") || ageDiscount.equals("y"))
    {
        //System.out.println(grossTick);
        agetick = grossTick * 0.85;
        System.out.println("Before vat the total is " + agetick);
    }
    else if (ageDiscount.equals("N") || ageDiscount.equals("n"))
    {
        System.out.println("Before vat the total is " + grossTick);
    }
    else if(standTick + terraceTick >= 5)
    {
        over5tick = grossTick * 0.82;
        System.out.println("Before vat the total is " + over5tick);
    }
    else if (standTick + terraceTick < 5 )
    {
        System.out.println("Before vat the total is " + grossTick);
    }
    else if (ageDiscount.equals("Yes") || ageDiscount.equals("yes")  && standTick + terraceTick >= 5)
    {
        over5tick = grossTick * 0.82;
        System.out.println("Before vat the total is " + over5tick);
    }


    System.out.println("Will you be having dinner? y/n ");
    String havingDinner = in.next();

    if(havingDinner.equals("Y") || havingDinner.equals("y") || ageDiscount.equals("Y") || ageDiscount.equals("y"))
    {
        agetick = (grossTick * 0.85);
        dinnerTotal = dinnerPrice * (standTick + terraceTick);
        vatTotalForAge = ((dinnerTotal + agetick) * 1.21);
        System.out.println(vatTotalForAge);
    }
    else if (havingDinner.equals("Y") || havingDinner.equals("y")||ageDiscount.equals("N") || ageDiscount.equals("n"))
    {
        dinnerTotal = dinnerPrice * (standTick + terraceTick);
        vatTotalNoAgeLess5 = ((dinnerTotal + grossTick) * 1.21);
        System.out.println(vatTotalNoAgeLess5);
    }



    /*
    if(havingDinner.equals("Y") || havingDinner.equals("y")|| ageDiscount.equals("Y") || ageDiscount.equals("y")) 
    {
        agetick = (grossTick * 0.85);
        dinnerTotal = dinnerPrice * (standTick + terraceTick);
        vatTotalForAge = ((dinnerTotal + agetick) * 1.21);
        System.out.println(vatTotalForAge);
    }

    else if(havingDinner.equals("Y") || havingDinner.equals("y") || ageDiscount.equals("N") || ageDiscount.equals("n"))
    {
        dinnerTotal = dinnerPrice * (standTick + terraceTick);
        vatTotalNoAgeLess5 = ((dinnerTotal + grossTick) * 1.21);
        System.out.println("is");

    }
    dinnerTotal = dinnerPrice * (standTick + terraceTick);
    vatTotalNoAgeLess5 = ((dinnerTotal + grossTick) * 1.21);
    System.out.println(vatTotalNoAgeLess5);



    /*
    if( dinner > 0 || ageDiscount.equals("Yes") || ageDiscount.equals("yes"))
    {
        System.out.println("Total of dinner and vat is " +(( agetick + dinner)*1.21));
    }
    else if (dinner > 0||ageDiscount.equals("No") || ageDiscount.equals("no"))
    {
    System.out.println("Total of dinner and vat is " + ((grossTick + dinner)*1.21));
    }
    else if(dinner > 0||standTick + terraceTick >= 5)
    {
        System.out.println("Total of dinner and vat is " + (over5tick*1.21 + dinner));
    }
    else if (dinner > 0 ||standTick + terraceTick < 5 )
    {
        System.out.println("Total of dinner and vat is " + ((grossTick*1.21) + dinner));
    }
    else if (dinner > 0||ageDiscount.equals("Yes") || ageDiscount.equals("yes")  && standTick + terraceTick >= 5)
    {
        System.out.println("Total of dinner and vat is " + (over5tick*1.21 + dinner));
    }
    else if (dinner < 1 || ageDiscount.equals("No") || ageDiscount.equals("no"))
    {
    System.out.println("Total including and vat is " + grossTick*1.21);
    }
    else if(dinner < 1 ||standTick + terraceTick >= 5)
    {
        System.out.println("Total including and vat is " + over5tick*1.21);
    }
    else if (dinner < 1 ||standTick + terraceTick < 5 )
    {
        System.out.println("Total including and vat is " + grossTick*1.21);
    }
    else if (dinner < 1 || ageDiscount.equals("Yes") || ageDiscount.equals("yes")  && standTick + terraceTick >= 5)
    {
        System.out.println("Total including and vat is " + over5tick*1.21 );
    }
    */
}
publicstaticvoidmain(字符串[]args){
//数据字典
双看台、露台、grossTick、handlingFee、TicketDiscont、flatFee、totalCost、agetick、over5tick、,
vatTotal、totaltick、dinnerPrice、dinnerTotal、VatTotalFood、vatTotalNoAgeLess5;
扫描仪输入=新扫描仪(系统输入);
//收集用户的基本输入
System.out.println(“输入您的姓名”);
字符串名称=in.next();
System.out.println(“输入您的手机号码”);
字符串mobile=in.next();
System.out.println(“您想要多少张展位票?”);
int standTick=in.nextInt();
System.out.println(“您想要多少张露台票?”);
int-tick=in.nextInt();
系统输出打印项次(“OAP/学生票?是/否”);
字符串ageDiscount=in.next();
//基本费用作为变量
standT=25*standTick;
梯田t=20*t;
手续费=7.50;
晚餐价格=20;
//定义总成本变量
格罗斯蒂克=梯田+标准;
如果(ageDiscount.equals(“Y”)| | ageDiscount.equals(“Y”))
{
//系统输出打印LN(grossTick);
agetick=grossTick*0.85;
System.out.println(“增值税前的总额为”+agetick);
}
else如果(ageDiscount.equals(“N”)| | ageDiscount.equals(“N”))
{
System.out.println(“增值税前的总额为”+grossTick);
}
否则如果(立勾+梯勾>=5)
{
超过5tick=grossTick*0.82;
System.out.println(“增值税前的总额为”+超过5分);
}
否则,如果(标距+标距<5)
{
System.out.println(“增值税前的总额为”+grossTick);
}
否则如果(ageDiscount.equals(“Yes”)| ageDiscount.equals(“Yes”)&&standTick+Terrietick>=5)
{
超过5tick=grossTick*0.82;
System.out.println(“增值税前的总额为”+超过5分);
}
System.out.println(“你要吃饭吗?是/否”);
字符串havingDidney=in.next();
如果(havingbeent.equals(“Y”)| | havingbeent.equals(“Y”)| | age折扣。equals(“Y”)| | age折扣。equals(“Y”))
{
agetick=(grossTick*0.85);
晚餐总额=晚餐价格*(标准价格+标准价格);
VatTotal饲料=((晚餐总额+年龄)*1.21);
System.out.println(VATT);
}
否则如果(havingbeent.equals(“Y”)| | havingbeent.equals(“Y”)| | | | | | | | | | | | | | | Agequals.equals(“N”))
{
晚餐总额=晚餐价格*(标准价格+标准价格);
vatTotalNoAgeLess5=((晚餐总额+grossTick)*1.21);
System.out.println(vatTotalNoAgeLess5);
}
/*
如果(havingbeent.equals(“Y”)| | havingbeent.equals(“Y”)| | age折扣。equals(“Y”)| | age折扣。equals(“Y”))
{
agetick=(grossTick*0.85);
晚餐总额=晚餐价格*(标准价格+标准价格);
VatTotal饲料=((晚餐总额+年龄)*1.21);
System.out.println(VATT);
}
否则如果(havingbeent.equals(“Y”)| | havingbeent.equals(“Y”)| | | | | | | | | | | | | | | Agequals.equals(“
{
晚餐总额=晚餐价格*(标准价格+标准价格);
vatTotalNoAgeLess5=((晚餐总额+grossTick)*1.21);
System.out.println(“is”);
}
晚餐总额=晚餐价格*(标准价格+标准价格);
vatTotalNoAgeLess5=((晚餐总额+grossTick)*1.21);
System.out.println(vatTotalNoAgeLess5);
/*
如果(晚餐>0 | | | | | | | | | | | | | | | | | | |年龄折扣
{
System.out.println(“晚餐和增值税合计为”+((agetick+晚餐)*1.21));
}
否则,如果(晚餐>0 | |年龄折扣。等于(“否”)| |年龄折扣。等于(“否”))
{
System.out.println(“晚餐和增值税合计为”+((grossTick+晚餐)*1.21));
}
否则,如果(晚餐>0 | |暂停+暂停>=5)
{
System.out.println(“晚餐和增值税合计”+(超过5勾*1.21+晚餐));
}
否则,如果(晚餐>0 | |暂停+暂停<5)
{
System.out.println(“晚餐和增值税合计为”+((grossTick*1.21)+晚餐);
}
否则,如果(晚餐>0 | | | |年龄折扣。等于(“是”)| | |年龄折扣。等于(“是”)&&standTick+Terrietick>=5)
{
System.out.println(“晚餐和增值税合计”+(超过5勾*1.21+晚餐));
}
否则,如果(晚餐<1 | | | |年龄折扣等于(“否”)| |年龄折扣等于(“否”))
{
系统输出打印项次(“包括增值税在内的总额为”+grossTick*1.21);
}
否则,如果(晚餐<1 | |站立+梯田站立>=5)
{
系统输出打印项次(“包括和增值税在内的总额为”+超过5勾*1.21);
}
否则,如果(晚餐<1 | |站立+5)
{
系统输出打印项次(“包括增值税在内的总额为”+grossTick*1.21);
}
否则,如果(晚餐<1 | | | |年龄折扣等于(“是”)| |年龄折扣等于(“是”)和&standTick+Terrietick>=5)
{
系统输出打印项次(“包括和增值税在内的总额为”+超过5勾*1.21);
}
*/
}

}

大十进制使用示例

vatTotalNoAgeLess5 = BigDecimal.valueOf(dinnerTotal).add(BigDecimal.valueOf(grossTick)).multiply(BigDecimal.valueOf(1.21)).doubleValue();
示例if语句

if ((havingDinner.equals("Y") || havingDinner.equals("y")) && (ageDiscount.equals("Y") || ageDiscount.equals("y"))) {
    agetick = (grossTick * 0.85);
    dinnerTotal = dinnerPrice * (standTick + terraceTick);
    vatTotalForAge = ((dinnerTotal + agetick) * 1.21);
    System.out.println(vatTotalForAge);
} else if ((havingDinner.equals("Y") || havingDinner.equals("y")) && (ageDiscount.equals("N") || ageDiscount.equals("n"))) {
    dinnerTotal = dinnerPrice * (standTick + terraceTick);
    vatTotalNoAgeLess5 = ((dinnerTotal + grossTick) * 1.21);
    System.out.println(vatTotalNoAgeLess5);
}

与问题不直接相关,但你可以考虑使用case语句来描述可读性:也可以:<代码>均衡器>代码>使用BigDePiple双计算有麻烦。当你说“第二个if和if if语句”时,我不确定你要问什么问题,但是检查<代码>在代码< >晚餐> <代码>(havingbeent.equals(“Y”)| havingbeent.equals(“Y”)|是多余的,因为如果这些是真的,