如何在Java中将一个浮点变量与一个整型变量相加?

huangapple go评论60阅读模式
英文:

How can I add a float variable with a int variable in Java?

问题

我对Java还不熟悉,所以我也在做一些练习。这个练习要求我获取一个账单,获取一个利息,计算该账单上的利息,然后返回账单和利息相加的结果。

以下是代码部分:

import java.util.Scanner;
public class BillsAndInterest{
    public static void main(String[] args){
        double bill;
        System.out.println("账单金额是多少?");
        bill = new Scanner(System.in).nextDouble();

        System.out.println("这张账单的利息是多少百分比?");
        double interest = new Scanner(System.in).nextDouble();

        double interestCalculated = interest / 100 * bill;
        System.out.println("您的利息是 " + interestCalculated + " 钱!\n所以总金额是 " + (bill + interestCalculated) + " 钱!");
    }
}

问题是,如果我的账单是100钱,利息是25,那么账单加利息应该是125,但代码返回 100.025.0 钱!

我尝试将 double interestCalculated 改为 int interestCalculated,但我得到了这个错误:
BillsAndInterest.java:15: 错误: 不兼容的类型: 从double到int的可能丢失转换
int interestCalculated = interest / 100 * bill;
^
1 错误


有人可以帮帮我吗?是否有一些特定的类可以寻求帮助?

英文:

I'm new at Java so I'm also working in some exercises. This one asks me to get a bill, get a interest, calculate the interest in that bill and then return the bill plus the interest.

Here's the code:

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!");
    }
}

So the problem is, if my bill is 100 dinheiros and the interest is 25, then bill + interest must be 125, but the code returns 100.025.0 dinheiros!

I've tried changing double interestCalculated to int interestCalculated, but I got this error:<br>
BillsAndInterest.java:15: error: incompatible types: possible lossy conversion from double to int
int interestCalculated = interest / 100 * bill;
^
1 error

<br>
Can someone please help me? Is there some specific class that I should look for help?

答案1

得分: 0

你只应该有一个 Scanner。单词是“much”(不是“mutch”)。你可以通过一条语句声明和初始化变量。25% 的利息总额是 1.25(不是 0.25),应该使用乘法而不是加法来计算消息。就像这样,

Scanner scan = new Scanner(System.in);
System.out.println("账单金额是多少?");
double bill = scan.nextDouble();

System.out.println("这个账单的利息是多少百分比?");
double interest = 1 + (scan.nextDouble() / 100);

double interestCalculated = interest * bill;
System.out.println("您的利息是:" + interestCalculated 
        + " 钱币!\n因此总金额是:" + bill
        + " + " + interest + "% 钱币!");
英文:

You should only have one Scanner. The word is "much" (not "mutch"). You can declare and initialize variables with one statement. And the total of interest for 25% is 1.25 (not 0.25) and should multiply instead of adding for the message. Like,

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

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

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

答案2

得分: 0

你可以尝试以下代码。

您只需要声明一次扫描器类对象,您的计算是正确的,但在打印数字时,不是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!");
    }
}

它会给我以下输出结果

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!
英文:

You can try below code.

You only need to declare scanner class object once and your calculation is correct but while printing digit instead of bill + interestCalculated try with (bill + interestCalculated)

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

        System.out.println(&quot;What&#39;s the interest in this bill in per cent?&quot;);
        double interest = scanner.nextDouble();

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

It's give me below O/P

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

答案3

得分: 0

问题出在这个语句上:

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);

当然,你想要的是将 billinterestCalculated 相加,然后将该总和合并到整个字符串结果中,类似于这样:

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 的信息,请查阅此处

英文:

The problem is with the statement:

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

The argument to System.out.println is a bunch of literals and variables connected by + operators. In the absence of any parentheses, the + operations are simply evaluated in left-to-right order, as if the statement had been written:

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

What you wanted, of course, was to have bill and interestCalculated added together and that sum then incorporated into the overall string result, something more like this:

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

One way to force that order of evaluation is to add parentheses around bill + interestCalculated:

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

Another alternative is to calculate the total in a separate variable and use that variable in the string expression:

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

There's also a third option which you might like even better:

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

Read more about printf here

huangapple
  • 本文由 发表于 2020年8月21日 11:33:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63516135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定