import java.util.Scanner;
public class ContainerCalculator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
//Print the Welcome message
System.out.println("Welcome to the Container Calculator!");
System.out.println("====================================");
//Set the value that users just input to height
//Variable Declaration
//Declare the diameter of a cylinder
float d = 0;
//Declare the height of a cylinder
float h = 0;
//Declare the volume of a cylinder
float v = 0;
//Declare the surface are of a cylinder
float s = 0;
//Set the value that users just input to diameter
//Ask users to input the diameter and height of a cylinder
System.out.print("Enter the diameter of a cylinder (in centimeters): ");
boolean validDiameter = false;
do {
if (scnr.hasNextInt()){
d = scnr.nextInt();
if (d <= 0) {
System.out.print("Please enter a positive integer value: ");
}
else {
validDiameter = true;
}
}
else {
System.out.print("Please enter an integer value (less than 2,147,483,648) as decimal digits: ");
scnr.nextLine();
}
} while (!validDiameter);
System.out.print("Enter the height of a cylinder (in centimeters): ");
boolean validHeight = false;
do {
if (scnr.hasNextInt()){
h = scnr.nextInt();
if (h <= 0) {
System.out.print("Please enter a positive integer value: ");
}
else {
validHeight = true;
}
}
else {
System.out.print("Please enter an integer value (less than 2,147,483,648) as decimal digits: ");
scnr.nextLine();
}
} while (!validHeight);
//Calculate the value of volume and surface area of the cylinder
v = (float) ((h*Math.PI*d*d)/4.0);
s = (float) ((Math.PI*d*d/2 + d*Math.PI*h));
//Print the value of volume
System.out.println("A can with a diameter of " + d+ " and a height of "+ h+ " has ");
//Print with only two places to the right of the decimal
System.out.printf("\ta volume of %.2f", v);
System.out.println(",");
//Print the value of surface area
//Print with only two places to the right of the decimal
System.out.printf("\tand a surface area of %.2f", s);
System.out.println(".");
double newVolume ;
double newSurface ;
double bestVolume = 0;
double bestSurface = 0;
double newD = 0;
double newH = 0;
double bestD = 0;
double bestH = 0;
for (newD = 1.0; newD <= v; newD++) {
for (newH = 1.0; newH <= v; newH++){
newVolume = (float) ((newH*Math.PI*newD*newD)/4.0);
newSurface = (float) ((Math.PI*newD*newD/2 + newD*Math.PI*newH));
if (newVolume > v && newSurface < s)
{
bestVolume = newVolume;
bestSurface = newSurface;
bestD = newD;
bestH = newH;
}
}
}
System.out.println("*** Surface Area Optimizer ***");
System.out.println("A can with a diameter of " + bestD+ " and a height of "+ bestH+ " has ");
//Print with only two places to the right of the decimal
System.out.printf("\ta volume of %.2f", bestVolume);
System.out.println(",");
//Print the value of surface area
//Print with only two places to the right of the decimal
System.out.printf("\tand a surface area of %.2f", bestSurface);
System.out.println(".");
//Print the end message
System.out.println("=============================================");
System.out.println("Thank you for using the Container Calculator.");
}
}
我想問一個關於for循環部分的問題。該部分的預期結果是打印出最小的表面積。例如,如果我在直徑(d)和高度(s)上輸入7,newDiameter應該是5,newheight應該是4,但是我的代碼只是輸出6和3.如何改進?如何在java中找到for循環中的最佳對
您是否在單步調試器中執行代碼?如果不是,請現在就這樣做。在for循環中使用 –
。嘗試將newD和newH的值更改爲「0.0」 –
我試過了,但我仍然不知道如何存儲最小的表面積的最佳直徑和高度 – Sharon