计算Java中每平方英寸比萨饼的价格

时间:2017-10-02 04:16:43

标签: java

我正在编写一个java程序,用一种方法计算披萨的面积,并用另一种方法计算披萨的每平方英寸的价格。我有区域方法工作,但当我尝试计算每平方英寸的价格时,我没有得到任何输出。我认为它与调用ppsi方法中的area()方法有关,但我不确定我做错了什么或如何正确调用area方法。

import java.util.Scanner;
public class Pizza {

    public static void main(String[] args){

        System.out.println("What is the size of your pizza in inches?");
        System.out.println(area() + " square inches");
        System.out.println("What is the price of your pizza?");
        System.out.println(ppsi());
    }

    public static double area(){
        Scanner keyboard = new Scanner(System.in);
        double diameter = keyboard.nextDouble();
        return (diameter / 2) * (diameter / 2) * Math.PI;

    }
    public static double ppsi(){
        Scanner keyboard = new Scanner(System.in);
        double price = keyboard.nextDouble();
        return((area()) / price);
    }

}

3 个答案:

答案 0 :(得分:1)

您在 ppsi 方法中再次呼叫区域,为什么还需要再次阅读?

我想你想要一些像下面这样的东西,我只修改了你的代码:

public static void main(String[] args){

    Scanner scanner = new Scanner(System.in);
    System.out.println("What is the size of your pizza in inches?");
    double inches=scanner.nextDouble();
    double area=area(inches);
    System.out.println(area + " square inches");
    System.out.println("What is the price of your pizza?");
    double price=scanner.nextDouble();
    System.out.println(ppsi(area, price));
}

public static double area(double diameter){
    return (diameter / 2) * (diameter / 2) * Math.PI;

}
public static double ppsi(double area, double price){
    return((area) / price);
}

答案 1 :(得分:1)

ppsi方法中,它会调用area,这将提示用户在AGAIN中输入另一个值。相反,首先提示这些值,然后将它们传递给您的方法

public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);

    System.out.println("What is the size of your pizza in inches?");
    double diameter = keyboard.nextDouble();
    System.out.println(area(diameter) + " square inches");
    double price = keyboard.nextDouble();
    System.out.println("What is the price of your pizza?");
    System.out.println(ppsi(diameter, price));
}

public static double area(double diameter){
    //Scanner keyboard = new Scanner(System.in);
    //double diameter = keyboard.nextDouble();
    return (diameter / 2) * (diameter / 2) * Math.PI;

}
public static double ppsi(double diameter, double price){
    //Scanner keyboard = new Scanner(System.in);
    //double price = keyboard.nextDouble();
    return((area(diameter)) / price);
}

这样,方法就是做一个工作,而不是两个

答案 2 :(得分:0)

打印“价格是多少......”的提示后,您的程序希望用户输入价格,然后再次输入区域。如果在该提示后输入两个数字,我希望你会看到一些输出。