Java循环,嵌套循环时是或否,java新手

时间:2017-11-06 01:05:53

标签: java loops nested

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被输入有感谢声明并关闭程序。帮助您实现这一目标。 我也遇到问题,数字四舍五入到最接近的小数点。任何帮助也将非常感谢

1 个答案:

答案 0 :(得分:1)

您不必像往常一样重复计算代码块两次。这就是循环的目的。你只需要编写一次代码,然后使用循环来反复运行它,所以在你的情况下使用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");
        }
    }