简体   繁体   English

条件语句 If-Else 中的逻辑错误 Java

[英]Logical Error in Conditional Statement If-Else Java

I have this given problem that is: A computer rental has a REGULAR CHARGE per hour for the first three hours or less.我有一个给定的问题是:计算机租赁在前三个小时或更短的时间内每小时收取常规费用。 The excess of three hours would be charged by 10 percent less of the regular charge per hour.超过三个小时的收费将比每小时正常收费少 10%。 And my source code is:我的源代码是:

    System.out.print("ENTER REGULAR CHARGE: ");
    int charge = scan.nextInt();
    
    System.out.print("ENTER REGULAR HOURS: ");
    int hours = scan.nextInt();
    
    scan.close();
    
    if (hours <= 3)
    {
        int bill= charge * hours;
        System.out.print("RENTAL BILL IS: " +bill);
    }
    else
    {
        double bill = (double) (charge * hours)-0.10;
        System.out.print("RENTAL BILL IS: " +bill);
    }

and the expected output should be:并且预期的 output 应该是:

ENTER REGULAR CHARGE: 4
ENTER REGULAR HOURS: 10
RENTAL BILL IS: 39

but my outcome goes:但我的结果是:

ENTER REGULAR CHARGE: 4
ENTER REGULAR HOURS: 10
RENTAL BILL IS: 39.9

I don't know if there's a problem in my formula or the variable that I used.我不知道我的公式或我使用的变量是否有问题。 It would be very helpful if someone pointed out the errors in my source code.如果有人指出我的源代码中的错误,那将非常有帮助。

You need to calculate price for 3 hours and deduct it from total hours in second condition.您需要计算 3 小时的价格,然后在第二种情况下从总小时数中扣除。 Also apply discount to the original price, multiply it to remaining hours, and add those hours price to 3-hour price.还要对原始价格应用折扣,将其乘以剩余小时数,然后将这些小时价格添加到 3 小时价格。

The code is below代码如下

import java.util.Formatter;
import java.util.Scanner;
public class string{
public static void main(String[] args){
    
    double bill= 0;
    Scanner scan = new Scanner(System.in);
    
    System.out.print("ENTER REGULAR CHARGE: ");
    double charge = scan.nextInt();

    System.out.print("ENTER REGULAR HOURS: ");
    int hours = scan.nextInt();

    scan.close();
    
    if (hours <= 3){
        // calculation for 3 hours or less
        bill= charge * hours;
        System.out.print("RENTAL BILL IS: " +bill);
    }
    else {
        // subtracting 3 hours from total hours if hours are more                              
        int remainingHrs = hours - 3; 
        
        // calculating price for 3 hours to add to remaining hours price                                         
        double priceforthreeHours = charge * 3;
        
        // calculating discountedprice (10% discount)
        double discprice = charge - (charge * 0.10);
        
        // adding it all together 3hrs + discounted price                                                        
        bill = priceforthreeHours +(remainingHrs * discprice);
        
        System.out.print("RENTAL BILL IS: ");
        
        // formatting price using java.util.Formater
        System.out.printf(" %.2f, %n",bill); 
        
     
    }
}   

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM