这是我到目前为止所做的程序。我应该要求收银员输入价格,如果是宠物,则输入 y 或 n。如果有 5 个或更多项目,则应该有一种方法来计算折扣。除了折扣方法的返回数据之外,我拥有的程序正在运行。
错误是68:error; cannot return a value from method whose result type is void.
我很困惑为什么数据无效。如果我取出return discount;
语句,那么程序编译没有错误。
import javax.swing.JOptionPane;
public class Assignment4
{
public static void main (String[] args)
{
double[] prices = new double[1000];
boolean[] isPet = new boolean[1000];
double enterPrice = 0;
int i = 0;
String yesPet = "Y";
int nItems = 0;
do
{
String input = JOptionPane.showInputDialog("Enter the price for the item: ");
enterPrice = Integer.parseInt (input);
prices[i] = enterPrice;
String petOrNo = JOptionPane.showInputDialog("Is this item a pet? Enter Y for pet and N for not pet.");
if (petOrNo.equalsIgnoreCase(yesPet))
{
isPet[i] = true;
}
else
{
isPet[i] = false;
}
i = i+1;
nItems = nItems + 1;
} while (enterPrice != -1);
//System.out.println(nItems);
}
public static void discount(double[] prices, boolean[] isPet, int nItems)
{
boolean found = false;
double[] discount = new double[nItems];
if (nItems > 6)
{
for (int i = 0; i < nItems; i++)
{
if (isPet[i] = true)
{
found = true;
break;
}
}
if (found = true)
{
for (int x = 0; x < nItems; x++)
{
if (isPet[x] = false)
{
int n = 0;
prices[x] = discount[n];
n = n + 1;
}
}
}
}
return discount;
}
}