我正在学习Java。对于这项任务,我正在尝试编写一个程序,提示收银员输入每个价格和数量,然后是Y代表宠物,N代表另一个项目。使用-1的价格作为哨兵。折扣等于其他物品成本的20%,但不是宠物。它必须是一个或多个宠物和至少五个其他项目来获得折扣。我很难为我的主类调用静态方法。任何帮助将不胜感激。谢谢!
public class DT_PetDiscounter
{
public static double discount(double[] prices, boolean[] isPet, int nItems)
{
double total = 0;
double discount = 0;
for (int i = 0; i < nItems; i++)
{
if (isPet[i] == false)
{
total = total + prices[i];
}
}
return discount = 0.2 * total;
}
}
import java.util.Scanner;
/**
* Main class to be used for input and output
*/
public class DT_DiscountTester
{
public static void main(String args[])
{
double[] prices = new double[100];
boolean[] isPet = new boolean[100];
int i = 0, nPets = 0, nItems = 0;
String pets;
int sentinel = 0;
Scanner scannerObject = new Scanner(System.in);
while (sentinel != -1)
{
System.out.println("Enter the price, or -1 to quit: ");
sentinel = scannerObject.nextInt();
if (sentinel == -1)
break;
prices[i] = sentinel;
System.out.println("Is it a pet? y/n");
pets = scannerObject.next();
if(pets.equals("y"))
{
isPet[i] = true;
nPets++;
}
else
{
isPet[i] = false;
nItems++;
}
i++;
}
if(nItems >= 5 && nPets >= 1)
discount(prices,isPet,nPets+nItems);
System.out.println("The total after the discount is " + discount);
}
}
答案 0 :(得分:0)
使用:
DT_PetDiscounter.discount(...);
答案 1 :(得分:0)
更改
if (nItems >= 5 && nPets >= 1)
discount(prices,isPet,nPets+nItems);
到
if (nItems >= 5 && nPets >= 1)
DT_PetDiscounter.discount(prices,isPet,nPets+nItems);
... 请,请阅读Java Style,以及:
的重要性我希望你的讲师/老师向你解释这些事情。如果没有,这是一个很好的起点:
答案 2 :(得分:0)
请使用以下代码段,并确保您的问题得到解决。
double discVal = 0;
if(nItems >= 5 && nPets >= 1)
discVal=DT_PetDiscounter.discount(prices,isPet,nPets+nItems);
System.out.println("The total after the discount is " + discVal);