为什么变量未初始化?

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

why is the variable not initialized?

问题

/** 销售员销售额的佣金计算规则如下:
 * 销售额在 $5,000 以下,佣金为销售额的 3%;
 * 销售额在 $5,000 到 $10,000 之间,佣金为销售额的 4%;
 * 销售额在 $10,000 到 $15,000 之间,佣金为销售额的 5%;
 * 销售额超过 $15,000,佣金为销售额的 6%。
 * 输入一个销售额,计算并打印总佣金。(dependent)*/

import java.util.Scanner;

public class Commission {
    public static void main(String[] args) {

        double salesFigure, commission = 0.0;
      
        Scanner scanner = new Scanner(System.in);
     
        System.out.println("请输入销售总额:");
        salesFigure = scanner.nextDouble();
     
        if (salesFigure <= 5000) 
           commission = salesFigure * 0.03;
        else if (salesFigure <= 10000) 
           commission = salesFigure * 0.04;
        else if (salesFigure <= 15000) 
           commission = salesFigure * 0.05;
        else
           commission = salesFigure * 0.06;

        System.out.println("销售额为 $" + salesFigure + ",佣金金额为 $" + commission + "。");

   }
}
/** 用于测试的销售额和对应的佣金计算如下:
 * 销售额    预期佣金
 * 4,000     120
 * 5,500     220
 * 12,000    600
 * 20,000    1,200 */

非常抱歉格式不正确,我刚开始使用这个工具,但我不知道为什么在以下行中出现“commission might not have been initialized”错误:

System.out.println("销售额为 $" + salesFigure + ",佣金金额为 $" + commission + "。");
英文:
/** A salesclerk gets 3% commission for sales up to $5,000, 
4% for sales from $5,000 to $10,000, 
5% for sales from $10,000 to $15,000, and 
6% for sales over $15,000. 
Enter a sales figure and print the total pay. (dependent)*/

import java.util.Scanner;

public class Commission{
    public static void main(String[] args){

        double salesFigure, commission;
      
        Scanner scanner=new Scanner(System.in);
     
        System.out.println(&quot;Enter the total sales amount:&quot;);
        salesFigure =scanner.nextDouble();
     
        if(salesFigure &gt; 5000) 
           commission= salesFigure*.03;
        if(salesFigure &gt; 5000 &amp;&amp; salesFigure &lt;=1000) 
           commission= salesFigure*.04;
        if(salesFigure &gt; 10000 &amp;&amp; salesFigure&lt;=15000) 
           commission= salesFigure*.05;
        if(salesFigure &gt; 15000) 
           commission= salesFigure*.06;

        System.out.println(&quot;With a sales amount of $&quot;+salesFigure+&quot;, $&quot;+commission+&quot; is the amount of commission earned.&quot;);

   }
}

/** For testing purposes use 
sales amounts     which should yield commission amounts of
4,000       120
5,500       220
12,000         600
20,000         1,200 */

Sorry for the formatting I'm new to this but I don't know why I'm getting the error that commission might not have been initialized on the following line:

 System.out.println(&quot;With a sales amount of $&quot;+salesFigure+&quot;, $&quot;+commission+&quot; is the amount of commission earned.&quot;);

答案1

得分: 0

你的佣金将不会在销售额低于5000的情况下被初始化,我认为你在编码时犯了一个错误,应该是这样的:

import java.util.Scanner;

public class Commission {
    public static void main(String[] args) {
        double salesFigure;
        double commission;

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入总销售额:");
        salesFigure = scanner.nextDouble();

        // 这个地方需要修正 > --> <
        if (salesFigure < 5000) commission = salesFigure * 0.03;
        if (salesFigure > 5000 && salesFigure <= 10000) commission = salesFigure * 0.04;
        if (salesFigure > 10000 && salesFigure <= 15000) commission = salesFigure * 0.05;
        if (salesFigure > 15000) commission = salesFigure * 0.06;
        System.out.println("以销售金额 $" + salesFigure + ",获得佣金金额为 $" + commission + "。");
    }
}

这样应该能解决问题。另外一个小问题是,最好将变量声明在单独的一行,这样更容易阅读。

英文:

Your commision won't get initialized if you salesFigure is below 5000, I think you made a mistake while coding it should be this:

import java.util.Scanner;
   public class Commission
   {
      public static void main(String[] args)
      {
      double salesFigure;
      double commission;
      
      Scanner scanner=new Scanner(System.in);
     
      System.out.println(&quot;Enter the total sales amount:&quot;);
      salesFigure =scanner.nextDouble();
     
      // this should fix the problem &gt; --&gt; &lt;
      if(salesFigure &lt; 5000) commission= salesFigure*.03;
      if(salesFigure &gt; 5000 &amp;&amp; salesFigure &lt;=1000) commission= salesFigure*.04;
      if(salesFigure &gt; 10000 &amp;&amp; salesFigure&lt;=15000) commission= salesFigure*.05;
      if(salesFigure &gt; 15000) commission= salesFigure*.06;
      System.out.println(&quot;With a sales amount of $&quot;+salesFigure+&quot;, $&quot;+commission+&quot; is the amount of commission earned.&quot;);

   }
}

That should fix the problem. Also small nitpick but it's best if you declare your variables on a separated line, that way it's more readable.

答案2

得分: 0

>为什么变量没有被初始化?

这是因为在你声明变量时没有进行初始化:

double salesFigure, commission;

此外,当你尝试给 commission 赋值时,所有可能的赋值情况都需要通过给定的条件:

    if(salesFigure > 5000) 
       commission = salesFigure * 0.03;
    if(salesFigure > 5000 && salesFigure <= 1000) 
       commission = salesFigure * 0.04;
    if(salesFigure > 10000 && salesFigure <= 15000) 
       commission = salesFigure * 0.05;
    if(salesFigure > 15000) 
       commission = salesFigure * 0.06;

所以你的编译器在告诉你,有可能如果所有条件都不满足,你的 commission 变量将保持未初始化状态。


为了解决这个问题,你可以将其初始化为一些默认值:

double commission = 0.0;

其次,检查你的条件。确保它们不是一些永远不可能为真的矛盾条件。

英文:

>why is the variable not initialized?

That is because you didn't initialize it when you declared it:

double salesFigure, commission;

Further more, when you try to assign a value to commission, all possible value assignment for commission needs to pass through the given conditions.

    if(salesFigure &gt; 5000) 
       commission= salesFigure*.03;
    if(salesFigure &gt; 5000 &amp;&amp; salesFigure &lt;=1000) 
       commission= salesFigure*.04;
    if(salesFigure &gt; 10000 &amp;&amp; salesFigure&lt;=15000) 
       commission= salesFigure*.05;
    if(salesFigure &gt; 15000) 
       commission= salesFigure*.06;

So your compiler if telling you that, there is a possibility that if all conditions failed, your commission variable will be left uninitialized.


To resolve the issue, you could just initialize it with some default value:

double commission = 0.0d;

Secondly, check your conditions. Make sure they are not some contradicting conditions which can never be true.

答案3

得分: 0

一个简单的解决方案是按以下方式调整检查条件:

if (salesFigure > 15000)
    commission = salesFigure * 0.06;
else if (salesFigure > 10000)
    commission = salesFigure * 0.05;
else if (salesFigure > 5000) 
    commission = salesFigure * 0.04;
else
    commission = salesFigure * 0.03;

这样你就不需要初始化 commission,因为它总会被赋值(我强烈建议在使用大量的 if / else if 语句时始终使用花括号 {})。

如果你为百分比使用一个额外的变量,代码会更加清晰:

double percentage;
if (salesFigure > 15000) {
    percentage = 6;
} else if (salesFigure > 10000) {
    percentage = 5;
} else if (salesFigure > 5000) {
    percentage = 4;
} else {
    percentage = 3;
}
commission = salesFigure * (percentage / 100.0);
英文:

A simple solution is to reverse the checks as follows:

    if (salesFigure &gt; 15000)
       commission= salesFigure*.06;
    else if (salesFigure &gt; 10000)
       commission= salesFigure*.05;
    else if (salesFigure &gt; 5000) 
       commission= salesFigure*.04;
    else
       commission= salesFigure*.03;

This way you don't need to initialize commission, as it will always receive a value (I strongly recommend always using braces {} when using lots of if / else if statements).

You can write even cleaner code if you use an additional variable for the percentage:

 double percentage;
 if (salesFigure &gt; 15000) {
     percentage = 6;
 } else if (salesFigure &gt; 10000) {
     percentage = 5;
 } else if (salesFigure &gt; 5000) {
     percentage = 4;
 } else {
     percentage = 3;
 }
 commission = salesFigure * (percentage / 100.0);

huangapple
  • 本文由 发表于 2020年10月2日 03:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64161939.html
匿名

发表评论

匿名网友

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

确定