如何在java中添加带有int变量的浮点变量?

olqngx59  于 2021-07-08  发布在  Java
关注(0)|答案(3)|浏览(355)

我是java新手,所以我也在做一些练习。这个要求我得到一张账单,得到利息,计算出那张账单的利息,然后把账单加上利息还给我。
代码如下:

import java.util.Scanner;
public class BillsAndInterest{
    public static void main(String[] args){
        double bill;
        System.out.println("How mutch is the bill payment? ");
           bill = new Scanner(System.in).nextDouble();

        System.out.println("What's the interest in this bill in per cent?");
           double interest = new Scanner(System.in).nextDouble();

        double interestCalculated = interest / 100 * bill;
           System.out.println("Your interest is " + interestCalculated + " dinheiros! \n So the amount is " + bill + interestCalculated + " dinheiros!");
    }
}

所以问题是,如果我的账单是100第纳尔,利息是25,那么账单+利息必须是125,但是代码返回100.025.0第纳尔!
我试过换衣服 double interestCalculatedint interestCalculated ,但我有个错误:
账单和利息。java:15:错误:不兼容的类型:从double到int interestcalculated=interest/100*bill;^的可能有损转换1个错误
有人能帮我吗?有什么特别的课程需要我帮忙吗?

rqqzpn5f

rqqzpn5f1#

问题在于声明:

System.out.println("Your interest is " + interestCalculated + 
   " dinheiros! \n So the amount is " + 
   bill + interestCalculated + " dinheiros!");

对…的论点 System.out.println 是一组文字和变量通过 + 操作员。如果没有任何括号,则 + 操作只是按照从左到右的顺序进行计算,就好像语句是这样写的:

String $T1 = "Your interest is " + interestCalculated;
String $T2 = $T1 + " dinheiros! \n So the amount is ";
String $T3 = $T2 + bill;                   // String + number = String
String $T4 = $T3 + interestCalculated;     // String + number = String
String $T5 = $T4 + " dinheiros!";
System.out.println($T5);

当然,你想要的是 bill 以及 interestCalculated 加在一起,然后将总和合并到整个字符串结果中,类似于:

String $T1 = "Your interest is " + interestCalculated;
String $T2 = $T1 + " dinheiros! \n So the amount is ";
double $T3 = bill + interestCalculated;     // number + number = number
String $T4 = $T2 + $T3;                     // String + number = String
String $T5 = $T4 + " dinheiros!";
System.out.println($T5);

强制这种求值顺序的一种方法是在其前后加上括号 bill + interestCalculated :

System.out.println("Your interest is " + interestCalculated + " dinheiros! \n So the amount is " + (bill + interestCalculated) + " dinheiros!");

另一种方法是在单独的变量中计算总数,并在字符串表达式中使用该变量:

double totalAmount = bill + interestCalculated;
System.out.println("Your interest is " + interestCalculated + " dinheiros! \n So the amount is " + totalAmount + " dinheiros!");

还有第三种选择,你可能更喜欢:

System.out.printf(
    "Your interest is %.2f dinheiros! %n So the amount is %.2f dineheiros!%n", 
    interestCalculated, 
    bill + interestCalculated);

阅读更多关于 printf 在这里

eyh26e7m

eyh26e7m2#

你应该只有一个 Scanner . 这个词是“much”(不是“mutch”)。可以用一条语句声明和初始化变量。25%的利息总额是 1.25 (不是 0.25 )消息应该是乘法而不是加法。比如,

Scanner scan = new Scanner(System.in);
System.out.println("How much is the bill payment? ");
double bill = scan.nextDouble();

System.out.println("What's the interest in this bill in per cent?");
double interest = 1 + (scan.nextDouble() / 100);

double interestCalculated = interest * bill;
System.out.println("Your interest is " + interestCalculated 
        + " dinheiros!\nSo the amount is " + bill
        + " + " + interest + "% dinheiros!");
pcrecxhr

pcrecxhr3#

你可以试试下面的代码。
您只需要声明scanner类对象一次,并且您的计算是正确的,但是在打印数字而不是 bill + interestCalculated 尝试 (bill + interestCalculated) ```
class BillsAndInterest{
public static void main(String[] args){
double bill;
Scanner scanner=new Scanner(System.in);
System.out.println("How mutch is the bill payment? ");
bill = scanner.nextDouble();

    System.out.println("What's the interest in this bill in per cent?");
    double interest = scanner.nextDouble();

    double interestCalculated = interest / 100 * bill;
    System.out.println("Your interest is " + interestCalculated + " dinheiros! \n So the amount is " + (bill + interestCalculated) + " dinheiros!");
}

}


### 这是给我低于o/p

How mutch is the bill payment?
100
What's the interest in this bill in per cent?
25
Your interest is 25.0 dinheiros!
So the amount is 125.0 dinheiros!

相关问题