【问题标题】:How to find the best pairs in for loops in java如何在java中的for循环中找到最佳对
【发布时间】:2017-02-16 07:15:17
【问题描述】:
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)输入 7,为高度(s)输入 2,newDiameter 应该是 5,newheight 应该是 4,但我的代码只打印出 6 和 3。如何改进它?

【问题讨论】:

  • 您是否在调试器中单步执行了代码?如果没有,请立即执行。
  • 在 for 循环中。尝试将 newD 和 newH 的值更改为“0.0”
  • 试过了还是不知道如何存储最小表面积的最佳直径和高度

标签: java eclipse for-loop


【解决方案1】:

问题出在这里:if (newVolume &gt; v &amp;&amp; newSurface &lt; s)

您正在尝试找到一个小于之前最佳曲面的曲面,因此请改用此条件:if (newVolume &gt;= v &amp;&amp; newSurface &lt; bestSurface)

如果输入的hd 已经是最佳值,则应将best* 变量初始化为“原始”值,例如bestVolume = v.

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-04-01
  • 2021-10-20
  • 2012-09-20
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
相关资源
最近更新 更多