這是我迄今爲止所做的程序。我應該要求出納員輸入價格,如果是寵物,我應該要求y或n。然後,如果有5個或更多項目,則應該有方法計算折扣。除了折扣方法的退貨數據外,我擁有的程序正在運行。關於Java和數組在搜索方法和返回數據
的錯誤是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;
}
}
ty這麼多嘗試過,它的工作...誰讓我做程序的人告訴我所需要的方法是公共靜態無效折扣...所以這是給出 – user1797373