Java 二进制运算符'<=''+=';
我不明白为什么我的二进制操作数不适用于if语句。例如,我得到一个错误,二进制运算符“的操作数类型不正确,因为prices是数组(double[]),而5.00是单双精度,所以检查“pricesJava 二进制运算符'<=''+=';,java,Java,我不明白为什么我的二进制操作数不适用于if语句。例如,我得到一个错误,二进制运算符“的操作数类型不正确,因为prices是数组(double[]),而5.00是单双精度,所以检查“pricesprices是一个数组。Java如何通过询问20个不同的double是否小于9来说明您的意思?它们都小于9吗?它们的总和是否小于9?他们中是否有一个人少于9人 这不是一个好的比较。您的操作数不正确。在大多数地方,您可能是想编写prices[i]而不是prices。如何更正此错误?我在IDE中重新编写了上面的
prices
是一个数组。Java如何通过询问20个不同的double是否小于9来说明您的意思?它们都小于9吗?它们的总和是否小于9?他们中是否有一个人少于9人
这不是一个好的比较。您的操作数不正确。在大多数地方,您可能是想编写
prices[i]
而不是prices
。如何更正此错误?我在IDE中重新编写了上面的代码,以便您可以得到准确、可运行的答案。这基本上是Harsh之前建议的,还有一些小改动。另外,对于将来的问题,请在发布时在漂亮的打印机或IDE/编辑器中格式化您的代码,因为这样可以更容易地进行可视化调试。total+=prices代码>?这是什么意思?价格是一个数组,总数是一个双精度数组。
public class Prices2
{
public static void main (String[] args)
{
double[] prices = {2.45, 7.85, 1.35, 1.55, 4.05, 9.55, 4.55, 1.45, 7.85, 1.25, 5.55, 10.95, 8.55,
2.85, 11.05, 1.95, 5.05, 8.15, 10.55, 1.05};
double average;
double total = 0.0;
int i;
for (i = 0; i < 20; i++)
{
total += prices;
}
System.out.println("Sum of all prices: "+total+"\n" );
for (i = 0; i < 20; i++)
{
if (prices <= 5.00){
System.out.println(prices + " is less than $5.00");
}
}
average = total / (20);
System.out.println("\nThe average price is " + average + "\n");
for (i = 0; i < 20; i++)
{
if (prices >= average) {
System.out.println( " Values above average: " + prices);
}
}
}
}
public class Prices {
public static void main(String[] args) {
// Set up a list of TEST prices to check for various conditions
double[] prices = { 2.45, 7.85, 1.35, 1.55, 4.05, 9.55, 4.55, 1.45, 7.85, 1.25, 5.55, 10.95, 8.55,
2.85, 11.05, 1.95, 5.05, 8.15, 10.55, 1.05 };
int numberOfPrices = prices.length;
// GOAL: Calculate Sum of all numbers
// For each price (i=0, 1, 2... 19), add THAT price to the current value of total
double total = 0.0;
for (int i = 0; i < numberOfPrices; i++) {
total += prices[i];
}
System.out.println("Sum of all prices: " + total + "\n");
// GOAL: Check for for those less than 5
// For each price (i=0, 1, 2... 19), see if THAT price is less than 5.00
for (int i = 0; i < numberOfPrices; i++) {
if (prices[i] <= 5.00) {
System.out.println(prices[i] + " is less than $5.00");
}
}
// Calculate the average price (without hard-coding)
double average;
// average = total / (20);
average = total / numberOfPrices;
System.out.println("\nThe average price is " + average + "\n");
// GOAL: Check for Above Average numbers
// For each price (i=0, 1, 2... 19), see if THAT price is above the average we computed before
for (int i = 0; i < numberOfPrices; i++) {
if (prices[i] >= average) {
System.out.println(" Values above average: " + prices[i]);
}
}
}
}