这是一个相当基本的计划。当我尝试打印summaryOutput
方法和billOutput
方法时,我收到的错误是参数无法作为变量解析。
public class PizzaDriver{
public static void main(String[] args) {
PizzaOrder order = new PizzaOrder();
PizzaOutput output = new PizzaOutput();
PizzaInput input = new PizzaInput();
System.out.println(output.menuOutput());
input.readInput(order);
System.out.println(summaryOutput1 = output.summaryOutput (numCheese, numPepperoni, numSausage, numVegetarian));
System.out.println(output.billOutput(String billOutput));
}
}
公共类PizzaOutput {
public String menuOutput()
{
String menuOutput1 = "Item \t Price \nCheese \t $2.40 \n Sausage \t $3.00 \nPepperoni \t $3.00 \nVegertarian \t $3.00";
return menuOutput1;
}
public void summaryOutput(int numCheese,int numPepperoni,int numSausage,int numVegetarian)
{
System.out.println("Cheese: " + numCheese);
System.out.println("Pepperoni: " + numPepperoni);
System.out.println("Sausage: " + numSausage);
System.out.println("Vegetarian: " + numVegetarian);
}
public void billOutput(double subTotal, double tax, double carryOut, double totalBill)
{
System.out.println("Subtotal : " +subTotal);
System.out.println("Tax : " + tax);
System.out.println("carryOut : " +carryOut);
System.out.println("Total Bill: " + totalBill);
}
import java.util.Scanner;
公共类PizzaInput { 扫描仪键盘=新扫描仪(System.in);
public int numCheese, numPepperoni, numSausage, numVegetarian;
public void readInput(PizzaOrder order)
{
System.out.print("How many Cheese Pizzas would you like?");
int numCheese = keyboard.nextInt();
order.setCheese(numCheese);
System.out.print("How many Pepperoni pizzas would you like?");
int numPepeperoni = keyboard.nextInt();
order.setPepperoni(numPepperoni);
System.out.print("How many Sausage pizzas would you like?");
int numSausage = keyboard.nextInt();
order.setSausage(numSausage);
System.out.print("How many Vegetarian pizzas would you like?");
int numVegetarian = keyboard.nextInt();
order.setVegetarian(numVegetarian);
}
}
公共类PizzaOrder { 私人决赛双人CHEESE_PRICE = 2.40;
private final double PEPPERONI_PRICE = 3.00;
private final double SAUSAGE_PRICE = 3.00;
private final double VEGETARIAN_PRICE = 3.50;
private final double SALES_TAX = .025;
private final double CARRY_OUT = .10;
public int numCheese, numPepperoni, numSausage, numVegetarian;
public int getCheese()
{
return numCheese;
}
public void setCheese(int numCheese)
{
this.numCheese=numCheese;
}
public int getPepperoni()
{
return numPepperoni;
}
public void setPepperoni(int numPepperoni)
{
this.numPepperoni=numPepperoni;
}
public int getSausage()
{
return numSausage;
}
public void setSausage(int numSausage)
{
this.numSausage= numSausage;
}
public int getVegetarian()
{
return numVegetarian;
}
public void setVegetarian(int numVegetarian)
{
this.numVegetarian = numVegetarian;
}
public double calculateSubTotal()
{
double cheeseTotal= numCheese * CHEESE_PRICE;
double pepperoniTotal = numPepperoni * PEPPERONI_PRICE;
double sausageTotal = numSausage * SAUSAGE_PRICE;
double vegetarianTotal = numVegetarian * VEGETARIAN_PRICE;
double subTotal = cheeseTotal + pepperoniTotal + sausageTotal + vegetarianTotal;
double tax = (subTotal) * SALES_TAX;
double totalBill = (tax + subTotal) * CARRY_OUT;
return totalBill;
}
}
答案 0 :(得分:0)
当你说下面的话
output.summaryOutput (numCheese, numPepperoni, numSausage, numVegetarian)
它会尝试在当前方法/类中搜索相同的变量名称(numCheese
,numPepperoni
,numSausage
,numVegetarian
),这些名称未定义,因此它无法弄清楚。
请定义这些或者正确引用
也没有summaryOutput1
和@pooja建议的定义
System.out.println(output.billOutput(String billOutput));//where is the param billOutput?
应该像
System.out.println(output.billOutput(billOutput));
答案 1 :(得分:0)
删除" String
"在调用方法而不是定义方法时,从以下行开始。此外,billOutput
已经两次。避免名称冲突。
System.out.println(output.billOutput(String billOutput));
答案 2 :(得分:0)
浏览以下代码集并确定您必须在应用程序中执行的转换器
public class PizzaOutput {
private int numCheese;
private int numPepperoni;
private int numSausage;
private int numVegetarian;
public PizzaOutput(){}
public PizzaOutput(int numSausage, int numCheese, int numPepperoni, int numVegetarian) {
this.numSausage = numSausage;
this.numCheese = numCheese;
this.numPepperoni = numPepperoni;
this.numVegetarian = numVegetarian;
}
public int getNumCheese() {
return numCheese;
}
public void setNumCheese(int numCheese) {
this.numCheese = numCheese;
}
public int getNumPepperoni() {
return numPepperoni;
}
public void setNumPepperoni(int numPepperoni) {
this.numPepperoni = numPepperoni;
}
public int getNumSausage() {
return numSausage;
}
public void setNumSausage(int numSausage) {
this.numSausage = numSausage;
}
public int getNumVegetarian() {
return numVegetarian;
}
public void setNumVegetarian(int numVegetarian) {
this.numVegetarian = numVegetarian;
}
public String summaryOutput(int numSausage, int numCheese, int numPepperoni, int numVegetarian){
setNumSausage(numSausage);
setNumCheese(numCheese);
setNumPepperoni(numPepperoni);
setNumVegetarian(numVegetarian);
return "Sausage : "+ getNumSausage()+ " Cheese : "+getNumCheese()+ " Pepeeroni : "+ getNumPepperoni()+ " Vegetarian : "+getNumVegetarian();
}
public String billOutPut(String bOutput){
// Enter here your code to get the bill
return "Bill Output"; // return the bill as a String
}
}
- >注意实例变量声明
- >你没有在两个" summaryOutput"中声明退货声明。和" billOutPut"方法。因此,您必须将return语句声明为单个语句
- >根据你的代码结构调用这两个方法足以获得输出而不需要在SOP中调用这两个方法