2017-02-16 8 views
-4
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を入力し、高さに2を入力すると、newDiameterは5になり、newheightは4になりますが、コードでは6と3が出力されます。forループで最良のペアを見つける方法

+1

デバッガでコードをステップ実行しましたか?そうでない場合は、今すぐ行ってください。 forループ内の –

+0

newDとnewHの値を "0.0"に変更しようとしました –

+0

私は試みましたが、私はまだ最小の表面積の最高の直径と高さを保存する方法がわかりません – Sharon

答えて

0

問題はここにある:if (newVolume > v && newSurface < s)

あなたは、以前の最良の表面よりも小さい表面を見つけ、その代わりに、この条件を使用しようとしている:if (newVolume >= v && newSurface < bestSurface)を。

hに入り、dは既に最適であった、best*変数は、例えば、「元の」値に初期化されなければならない場合にはbestVolume = v

+0

コメントは議論の対象外です。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/136041/discussion-on-answer-by-bjorn-vardal-how-to-find-the-best-pairs-in- for-loops-in)である。 –

関連する問題