使用 java setter 和 getter 请求用户输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32659386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:35:08  来源:igfitidea点击:

Asking for user input with java setters and getters

java

提问by T. Wielgos

how would i ask the user for the radius? I'm pretty sure it will be an easy fix but i'm just not sure how to do it. Here is my code so far.

我将如何向用户询问半径?我很确定这将是一个简单的修复,但我不确定如何去做。到目前为止,这是我的代码。

import java.util.Scanner;
public class CircleDriver
{


    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);


        Circle circle1 = new Circle();
        circle1.setRadius(20);
        System.out.println("Enter the radius of a circle " + circle1.getRadius());
        System.out.println("Area = " + circle1.calculateArea());
        System.out.println("Diameter = " + circle1.calculateDiameter());
        System.out.println("Circumference = " + circle1.calculateCircumference());


    }

}

Circle class :

圈类:

public class Circle
{

    private double radius;
    private final double PI = 3.14159;

    /**
     * this method calculates the area of the given radius
     * @return
     */
    public double calculateArea() {

        double area;

        area = (PI * radius * radius);

        return area;
    }
    /**
     * this method calculates the diameter of the given radius
     * @return
     */
    public double calculateDiameter() {

        double diameter;

        diameter = (radius * 2);

        return diameter;

    }
    /**
     * this method calculates the circumference of the given radius
     * @return
     */
    public double calculateCircumference() {

        double circumference;

        circumference = (2 * PI * radius);

        return circumference;

    }

    /**
     * this method sets the radius of the object
     * @param radius
     */

    public void setRadius(double radius) {

        this.radius = radius;
    }

    /**
     * this radius returns the radius given
     * @return
     */
    public double getRadius() {

        return radius;
    }

}

回答by Andreas

You're creating a Scanner called input, but you're not using it. Instead, you're hard-coding the value 20.

您正在创建一个名为 的扫描仪input,但您并未使用它。相反,您正在对 value 进行硬编码20

Try this:

试试这个:

Scanner input = new Scanner(System.in);
Circle circle1 = new Circle();
System.out.print("Enter the radius of a circle: ");
circle1.setRadius(input.nextDouble());
System.out.println("Area = " + circle1.calculateArea());
System.out.println("Diameter = " + circle1.calculateDiameter());
System.out.println("Circumference = " + circle1.calculateCircumference());

回答by Yassin Hajaj

Try this :

试试这个 :

System.out.println("Enter the radius pls :");
try {    
    circle1.setRadius(input.nextDouble());
} catch (InputMismatchException e){
    System.out.println("ERROR : Invalid input !");
}

回答by sre parthiban

Scanner input = new Scanner(System.in);
Circle circle1 = new Circle();
System.out.println("Enter the radius of a circle ");
int radius = input.nextInt();

There are several ways in getting an input from the user including BufferedReader, Console, Scanner etc. Above is an example using Scanner class. Now use the above radius value in your further calculations.

有多种方法可以从用户那里获取输入,包括 BufferedReader、Console、Scanner 等。以上是使用 Scanner 类的示例。现在在进一步计算中使用上述半径值。