简体   繁体   English

参数不能解析为变量?

[英]Parameters cannot be resolved as a variable?

This is a fairly basic program. 这是一个相当基本的程序。 When I try to print the summaryOutput method and billOutput method, I get errors saying the parameters cannot be resolved as a variable. 当我尝试打印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));
    }
}

public class PizzaOutput { 公共类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; 导入java.util.Scanner;

public class PizzaInput { Scanner keyboard = new Scanner(System.in); 公共类PizzaInput {扫描仪键盘=新的Scanner(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);



    }

} }

public class PizzaOrder { private final double CHEESE_PRICE = 2.40; 公共类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; 

}

} }

when you say following 当你说跟随

output.summaryOutput (numCheese, numPepperoni, numSausage, numVegetarian)

it will try to search the same variable name ( numCheese , numPepperoni , numSausage , numVegetarian ) in current method/class, which are not defined and hence it is not able to figure it out. 它将尝试在当前方法/类中搜索相同的变量名( numCheesenumPepperoninumSausagenumVegetarian ),这些变量名未定义,因此无法确定。

Please define those or else refer it properly 请定义这些内容,否则请正确引用

also ther is no definition for summaryOutput1 and as @pooja suggested 也没有为summaryOutput1和@pooja建议定义

System.out.println(output.billOutput(String billOutput));//where is the param billOutput?

should be like 应该像

System.out.println(output.billOutput(billOutput));

Remove " String " from the following line as you are calling a method and not defining it. 在调用方法而不是定义方法时,请从以下行中删除“ String ”。 Also, billOutput has been twice. 此外, billOutput已经两次。 Avoid name collisions. 避免名称冲突。

System.out.println(output.billOutput(String billOutput));

Go through the following code set and identify the changers that you have to do inside of your application 仔细阅读以下代码集,并确定在应用程序内部必须执行的更改

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
    }
}

-> pay attention on the instance variables declaration ->注意实例变量的声明
-> Your had not declared the return statements inside the both "summaryOutput" and "billOutPut" methods. ->您尚未在“ summaryOutput”和“ billOutPut”方法中声明return语句。 Therefore you have to declare the return statement as a single statement 因此,您必须将return语句声明为单个语句
-> According to your code structure calling the both methods are enough to get the output and not need to call both methods inside the SOP ->根据您的代码结构,调用这两个方法足以获取输出,而无需在SOP中调用这两个方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM