我一直收到这些错误代码:
SoftwareSales.java:20:找不到符号
symbol:变量Sales
location:class SoftwareSales
Sales = 99;
^
SoftwareSales.java:30:找不到符号
symbol:变量Sales
location:class SoftwareSales
Total = Quantity * Sales;
^
SoftwareSales.java:32:找不到符号
symbol:variable Disount
location:class SoftwareSales
Total = Total - Disount;
^
SoftwareSales.java:37:可能会失去精确度
发现:浮动
必需:int
Rate = .20f;
^
SoftwareSales.java:39:可能会失去精确度
发现:浮动
必需:int
Rate = .30f;
^
SoftwareSales.java:41:可能会失去精确度
发现:浮动
必需:int
Rate = .40f;
^
SoftwareSales.java:43:可能会失去精确度
发现:浮动
必需:int
Rate = .50f;
^
7个错误
到目前为止,这是我的代码:
import java.util.Scanner;
public class SoftwareSales
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Sales = 99;
int Quantity;
int Total;
int Rate;
double Discount;
System.out.print ("Enter amount of packages purchased: ");
Quantity = keyboard.nextInt();
Total = Quantity * Sales;
Discount = Total * Rate;
Total = Total - Disount;
if(!(Total < 10))
System.out.println("No discount." + "Your total is: " + Total);
else if(Total >= 19)
Rate = .20f;
else if(Total >= 49)
Rate = .30f;
else if(Total >= 99)
Rate = .40f;
else
Rate = .50f;
System.out.println("Your discount is " + Discount +
". Your total is: " + Total);
}
}
非常感谢任何帮助。谢谢你的时间。
答案 0 :(得分:3)
您需要声明变量Sales
。例如:
int Sales = 99;
精度损失错误是因为您要将float
值分配给int
个变量。虽然这有时被视为警告,但它通常表示错误,需要声明正确的变量类型或使用显式强制转换。您可能应该将变量声明为float
。或者,以整数分值进行所有计算,避免潜在的舍入误差。
并不是你问过这个问题,但是Java命名约定是变量为camel case并以小写字母开头:sales
而不是Sales
(和totalSales
代替TotalSales
,如果你有这样一个变量名。)
答案 1 :(得分:2)
您可能希望将变量设为小写,以避免与类名混淆。在java类中,名称总是大写,这可能会在代码中引入混淆。
您遗失int sales = 99;
它无法解析符号,因为您从未声明过它。
Rate
应为双倍,并且您要将值转换为浮动,您可以不使用.f
答案 2 :(得分:2)
Sales
Disount
Rate
应为float
答案 3 :(得分:0)
你需要学习我朋友的变量类型。销售很可能是一个int
int Sales
费率是双倍的
double Rate
整个代码应该是
import java.util.Scanner;
public class SoftwareSales
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int sales = 99;
int quantity;
double total;
double rate = 0; // set rate to what you need.
double discount;
System.out.print ("Enter amount of packages purchased: ");
quantity = keyboard.nextInt();
total = quantity * sales;
discount = total * rate;
total = total - discount;
if(!(total < 10))
System.out.println("No discount." + "Your total is: " + total);
else if(total >= 19)
rate = .20f;
else if(total >= 49)
rate = .30f;
else if(total >= 99)
rate = .40f;
else
rate = .50f;
System.out.println("Your discount is " + discount +
". Your total is: " + total);
}
}