2017-11-06 7 views
-1
import java.util.Scanner; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
/** 
* 
* @author Leigh 
*/ 
public class circle { 

    private static String answer; 

    public static void main(String[] args) { 
     //Opening statement 
     System.out.println("Welcome to the Round Object Calculator\n" 
       + "This program will calculate the area of a circle\n" 
       + "  or the volume of a sphere.\n" 
       + "The calculations will be based on the user input radius."); 
     // CREATE SCANNER 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter C for circle or S for sphere: "); 
     String a = input.next(); 
     //s or c loop 
     if (a.equals("C")) { 
      System.out.print("Thank you. What is the radius of the circle (in inches): "); 
      Double c = input.nextDouble(); 
      System.out.printf("The area of a circle with a radius of " + "is " + (3.14 * c * c) + " cubic inches"); 
     } else if (a.equals("S")) { 
      System.out.print("Thank you. What is the radius of the sphere (in inches): "); 
      Double s = input.nextDouble(); 

      System.out.printf("The volume of a sphere with a radius of" + s + "is " + (4 * 22 * s * s * s)/(3 * 7) + " cubic inches"); 

     } 
     //yes or no loop 
     System.out.print("Do you want to calculate another round object (Y/N)? "); 
     String y = input.next(); 
     if (y.equals("Y")) { 
      if (a.equals("C")) { 
       System.out.print("Thank you. What is the radius of the circle (in inches): "); 
       Double c = input.nextDouble(); 
       System.out.printf("The area of a circle with a radius of " + "is " + (3.14 * c * c) + " cubic inches"); 
      } else if (a.equals("S")) { 
       System.out.print("Thank you. What is the radius of the sphere (in inches): "); 
       Double s = input.nextDouble(); 

       System.out.printf("The volume of a sphere with a radius of" + s + "is " + (4 * 22 * s * s * s)/(3 * 7) + " cubic inches"); 
      } 
     } 
     //default statement for N 
     System.out.println("Thnk you"); 
    } 
} 

私はJavaにとって非常に新しいので、このセクションで球と円の面積を計算するのに問題があります。私はループを発行しようとしているので、入力がYの場合、上記の質問は、ユーザが質問にNを入力するまで繰り返されます。 Nを入れていただいた場合は、ありがとうございます。プログラムを終了してください。この目標を達成するために大いに感謝してください。 また、最も近い小数点以下の数字に問題があります。どんな助けも大いにありがたくなるでしょう。入れ子ループ中にはい、いいえ、入れ子になったループ、新しいJavaへ

+0

構造およびループを容易に見ることができる。適切なエディタ/ IDEがコードを自動フォーマットします。 – user2864740

+0

私がJavaに慣れていないと言っているように、私はNetbeansでどのようにフォーマットするかを考え出したので、誰かが喜んで喜ぶだろう。私のコードを見て、私を助けてください。 – LeighB

+0

さて、それは良く見えます。次のステップは、ループをループにするコードを置くことです。この場合、["do..while"](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html): 'do {/ *ユーザーに入力と表示出力を要求*/}(/ *ユーザは/ *を続けると言いました) 'おそらく便利です。 – user2864740

答えて

1

あなたがやったように計算コードブロックを2回繰り返す必要はありません。これがループの目的です。一度だけコードを書いてから、ループを繰り返し実行する必要がありますので、do ... whileループを使用してケースを実行してください。

import java.util.Scanner; 

    /* 
    * To change this license header, choose License Headers in Project Properties. 
    * To change this template file, choose Tools | Templates 
    * and open the template in the editor. 
    */ 

    /** 
    * 
    * @author Leigh 
    */ 
    public class circle { 
     private static String answer; 

     public static void main(String[] args) { 

      String y = ""; 

      do { 
       System.out.println("Welcome to the Round Object Calculator\n" 
         + "This program will calculate the area of a circle\n" + "  or the volume of a sphere.\n" 
         + "The calculations will be based on the user input radius."); 
       // CREATE SCANNER 
       Scanner input = new Scanner(System.in); 
       System.out.print("Enter C for circle or S for sphere: "); 
       String a = input.next(); 
       // s or c 
       if (a.equals("C")) { 
        System.out.print("Thank you. What is the radius of the circle (in inches): "); 
        Double c = input.nextDouble(); 
        System.out.printf("The area of a circle with a radius of " + "is " + (3.14 * c * c) + " cubic inches"); 
       } else if (a.equals("S")) { 
        System.out.print("Thank you. What is the radius of the sphere (in inches): "); 
        Double s = input.nextDouble(); 

        System.out.printf("The volume of a sphere with a radius of" + s + "is " + (4 * 22 * s * s * s)/(3 * 7) 
          + " cubic inches"); 

       } 
       // yes or no 
       System.out.print("Do you want to calculate another round object (Y/N)? "); 
       y = input.next(); 
      } while (y.equalsIgnoreCase("Y")); 

      System.out.println("Thnk you"); 
     } 
    } 
+0

ここで、丸め数値にはどのような問題がありますか? – Saurabh

+0

ありがとうございました。数字を計算するために、2つではなく小数点以下6〜8桁の数字を持つ問題があります。私はこれにsystem.out.printfを使用するように言われましたが、私はコンポーネントがないと思いますか? – LeighB

+0

私の答えが役に立つと分かったら、それを選択した回答としてマークしてください。 – Saurabh

関連する問題