2015-09-18 4 views
-1

Как я могу задать пользователю радиус? Я уверен, что это будет легкое решение, но я просто не знаю, как это сделать. Вот мой код до сих пор.Запрашивать ввод пользователя с помощью java-сеттеров и геттеров

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:

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; 
    } 

} 
+1

Все, что вам нужно сделать, это circle1.setRadius (input.nextDouble()); – StackFlowed

+4

У вас есть «Сканер», почему вы его не используете? – RealSkeptic

ответ

3

Попробуйте это:

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

Помог ли мой ответ? Не забывайте о действии. :) –

+1

Спасибо! Я пытался понять это как час, и теперь я наконец понял, насколько легко исправить! –

+0

Рад, что я мог бы помочь :) –

3

Вы создаете сканер под названием input, но вы его не используете. Вместо этого вы жестко кодируете значение 20.

Попробуйте это:

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()); 
0
Scanner input = new Scanner(System.in); 
Circle circle1 = new Circle(); 
System.out.println("Enter the radius of a circle "); 
int radius = input.nextInt(); 

Есть несколько способов в получении входного сигнала от пользователя, включая BufferedReader, консоль, сканер и т.д. Выше приведен пример используя класс сканера. Теперь используйте приведенное выше значение радиуса в ваших дальнейших расчетах.

Смежные вопросы