gpt4 book ai didi

java - 错误: variable might not have been initialised in my CellPhoneBill program

转载 作者:行者123 更新时间:2023-12-02 13:07:12 24 4
gpt4 key购买 nike

编译后我收到这 3 个错误..请提前提供帮助并感谢..

  1. 错误:变量 ac 可能尚未初始化tcws=cm+tm+ac+am+ta;
  2. 错误:变量 am 可能尚未初始化tcws=cm+tm+ac+am+ta;
  3. 错误:变量 ta 可能尚未初始化tcws=cm+tm+ac+am+ta;

我的代码:

import java.lang.*;

import java.util.Scanner;

class CellPhoneBill {

public static void main(String[]args) {

double cm ,tm ,ac, am, ta, tcws, tax, taxpluscost ;

Scanner s=new Scanner(System.in);

System.out.print("Enter the number of minutes and also enter the number of text messages ");

cm= s.nextDouble();

tm= s.nextDouble();
if(cm<=50&& tm<=50) {

System.out.print("Base charge= $15.00"); }

else if(cm>50 && tm>50) {

System.out.println("Enter no of additional call minutes (if any) and additional messages(if any) ");

ac=s.nextDouble();
am= s.nextDouble();

cm=0.25* ac;

am=0.15*am;
System.out.println("Additional call charges = " +ac);

System.out.println("Additional message charges = " +am);

ta=ac+am;

System.out.println("Total Additional charges = " +ta);

}

else if(cm>=0&& tm>=0) { System.out.println("911 Call charges =$0.44" );

}

else {
tcws= cm+tm+ac+am+ta;

System.out.println("Total cost without Sales Tax ="+tcws);

tax=( 5/100.00*(tcws));

System.out.println(" 5% Sales Tax ="+tax);
taxpluscost=tcws+tax;

System.out.println("Total cost Including 5% Sales Tax ="+taxpluscost);
}
}
}

最佳答案

这意味着变量acamta尚未被赋值。您无法对没有任何值的变量求和。

对于修复,这取决于您想要获得的内容。也许您需要像其他一些情况一样执行 ac=s.nextDouble(); 来从用户读取值。或者也许您只需要执行 ac = 1.0; 或任何适当的值。当然,其他两个变量也是如此。

可能您想要一种不同的 if-else 结构,其中分配给一个 if 分支中的三个变量的值也应该用于最后的else部分?

关于java - 错误: variable might not have been initialised in my CellPhoneBill program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44098707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com