输出每磅肥料的价格和每平方施肥的成本

时间:2016-12-12 09:21:15

标签: java

每年夏天,约翰和杰西卡都会在后院种植蔬菜,并从当地的苗圃购买种子和肥料。托儿所携带不同尺寸的不同类型的蔬菜肥料。购买特定的肥料时,他们想知道每磅肥料的价格和每平方英尺施肥的成本。以下程序提示用户输入肥料袋的大小 - 以磅为单位,袋子的成本和面积 - 以袋子可以覆盖的平方英尺为单位。程序应输出所需的结果

这是我到目前为止所做的:

import java.util.*;


public class Ch3_PrExercise4
{

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

    double cost;
    double area;
    double bagSize;

    System.out.print("Enter the amount of fertilizer in pounds, in one bag: ");
    bagSize = console.nextDouble();
    System.out.println();
    System.out.println();

    System.out.print("Enter the cost of the, " + bagSize + " pound fertilizer bag: ");
    cost = console.nextDouble();
    System.out.println();

    System.out.print("Enter the area, in square feet, that can be fertilized by one bag: ");
    area = console.nextDouble();
    System.out.println();

    System.out.printf("The cost of the fertilizer per pound is: $%.2f%n", bagSize/cost);
    System.out.println();
    System.out.printf("The cost of fertilizing per square foot is: $%.4f%n", area/cost);


}
}

PS:我是Java的新手。我试图解决这个问题。任何回复将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

我认为最后几行应该使用cost/bagSize(因为你想要每磅美元,所以你应该有美元在顶部,磅在底部)和cost/area(因为你想要每个区域的美元) ,所以你应该有顶部的美元和底部的区域):

    System.out.printf("The cost of the fertilizer per pound is: $%.2f%n", cost/bagSize);
    System.out.println();
    System.out.printf("The cost of fertilizing per square foot is: $%.4f%n", cost/area);
相关问题