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へ
構造およびループを容易に見ることができる。適切なエディタ/ IDEがコードを自動フォーマットします。 – user2864740
私がJavaに慣れていないと言っているように、私はNetbeansでどのようにフォーマットするかを考え出したので、誰かが喜んで喜ぶだろう。私のコードを見て、私を助けてください。 – LeighB
さて、それは良く見えます。次のステップは、ループをループにするコードを置くことです。この場合、["do..while"](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html): 'do {/ *ユーザーに入力と表示出力を要求*/}(/ *ユーザは/ *を続けると言いました) 'おそらく便利です。 – user2864740