圈计算器有错误

时间:2015-11-07 15:39:03

标签: java

十进制格式不起作用,但相信可以正确输入 区域计算不起作用继续给我0.0周长的工作。 当他们输入无效或选项时,我需要帮助让它跳过输入半径。请帮忙。随附的是该计划:

import java.text.DecimalFormat;
import java.util.Scanner;``

public class Calculator {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        DecimalFormat formatter = new DecimalFormat("#00.0000");

        String choice;
        int option;
        double area;
        double circumference;
        double radius; 

        area = 0;
        circumference = 0;     
        option = keyboard.nextInt();
        radius = keyboard.nextDouble();

        System.out.println("CIRCLE CALCULATOR MENU");

        System.out.println("1) Calculate the Area of a Circle");
        if (option == 1) {
            area = (radius*radius)*Math.PI;
        }

        System.out.println("2) Calculate the Circumference of a Circle");
        if  (option == 2) {
            circumference = 2*radius*Math.PI;
        }

        System.out.println("3) Quit the Program");                          
        System.out.println("");
        System.out.println("Make a selection by choosing a number: ");
        if (1 == option || 2 == option)
            System.out.println("Please enter the radius of the circle: ");
        if (1 == option) {
            System.out.println("The area of the circle with radius "+radius+" is: " + circumference + "");
        }else if (2 == option) {
            System.out.println("The circumference of the circle with radius         "+radius+" is: " + circumference + "");
        } else if (option == 3) {
            System.out.println("You have chosen to quit the program");
        } else if (option > 4) {
            System.out.println("You have made an invalid selection.");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

当您确实要打印区域值时,您正在打印周长值:

System.out.println("The area of the circle with radius " + radius + " is: " + circumference + "");

在上述声明中将circumference更改为area将解决问题。

答案 1 :(得分:0)

您已重新排列代码。它的顺序不正确:

试试这个:

while(true) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Please enter your option 1.Area 2.Circumference 3.Quit");
            int choice = sc.nextInt();
            double area = 0,circumference =0, radius=0;
            switch (choice) {
            case 1:
                System.out.print("PLease enter the radius :");
                radius = sc.nextInt();
                area =(radius*radius)*Math.PI;
                System.out.print("Area : " +area);
                continue;
            case 2:
                System.out.print("PLease enter the radius :");
                radius = sc.nextInt();
                circumference = 2*radius*Math.PI;
                System.out.print("circumference : " +circumference);
                continue;
            case 3:
                break;

            default:
                System.out.print("Enter a valid option");
            }
        }