Calculate circle area

2

I'm having trouble writing this exercise in Java , using Eclipse IDE :

  

Develop a program to calculate the area of the circle.

In this exercise I need to assign Pi (π) the value of 3.1416, and the area calculation is equal to Pi (π), multiplied by the radius squared!

How do I write the "commands" in the Java Language? I started the course this month, or I can only use the basics.

Note: I can enter the radius value, but when I give ENTER the program does not spin!

My code:

import java.io.IOException;
import java.util.Scanner;

public class Exercicio3 {

    public static void main(String[] args) {
        double raio;
        double area;
        final double PI = 3.1416;

       System.out.println("Escreva o valor do raio");
       Scanner scan = new Scanner(System.in);
       raio = scan.nextFloat();
       area = scan.nextFloat();


       area  = PI* (raio*raio);

       System.out.print("O valor da area é " + area);  
       System.out.print(area);
       scan.close();
    
asked by anonymous 02.03.2018 / 15:06

4 answers

5

I think your code is not running because you have one more command asking you to enter a area = scan.nextFloat(); number even though there is no System.out.print(Escreva o valor da área) . Even though the area will be calculated, will not it?

The body of the method I've modified is:

double raio;
double area;
final double PI = 3.1416;

System.out.println("Escreva o valor do raio");
Scanner scan = new Scanner(System.in);
raio = scan.nextFloat();

area  = PI* Math.pow(raio, 2);

System.out.print("O valor da area é " + area);  
scan.close();

With result (in NetBeans):

ItispossibleinsteadofdeclaringfinaldoublePI=3.1416;,declaringnovariableandusing:

area=Math.PI*Math.pow(raio,2);

I'vebeenthinkingaboutformattingthenumberattheend,andI'dliketomakethefollowingsuggestion:

System.out.printf("O valor da area é %,.2f \n", area);

System.out.printf prints the formatted text, where % indicates where to include the value of the indicated variable after the comma. %,.2f means format the value of the variable with a comma as a separator and 2 decimal places. To change to 4 decimal places, for example, just use %,.4f instead of %,.2f . The \n at the end is only to include a line at the end of the run code. With this change, the result is:

    
02.03.2018 / 15:35
7
  • You do not need import java.io.IOException; .

  • Do not use close in Scanner .

  • You should not read the Scanner area. You should calculate it from the radius.

  • The default library already includes Math.PI , so you do not need to reset that on the outside.

  • You are writing the area twice in System.out.print . You only need to do this once.

  • You'll probably want to use System.out.println instead of System.out.print . Make it easy to put it together with other things you do later.

  • Your code looks like this:

    import java.util.Scanner;
    
    class Exercicio3 {
    
        public static void main(String[] args) {
           System.out.println("Escreva o valor do raio");
           Scanner scan = new Scanner(System.in);
    
           double raio = scan.nextFloat();
           double area = Math.PI * raio * raio;
    
           System.out.println("O valor da area é " + area);
        }
    }
    

    See here working on ideone.

        
    02.03.2018 / 15:33
    2

    Looking at your code, some suggestions.

    double raio = 0;
    /* inicialize variaveis que for utilizar em cálculos */
    double area = 0;
    final double PI = 3.1416;
    

    When requesting user data:

    // Não faz sentido você solicitar a àrea pois será calculada
    area = scan.nextFloat();
    

    Now returning to the problem, after you type the value of the radius the cursor skips the line requesting the value of the area also so it gives the impression that nothing happens. Just remove this line area = scan.nextFloat();

        
    02.03.2018 / 15:24
    1

    There are a few points you should review:

    1 - import java.io.IOException; is not required in this code
     2 - You can facilitate the creation of variables by placing them on the same line and separating by "," double raio, area;
     3 - Do not use close(); in Scanner
     4 - Since you defined the variables as double, it is recommended that you use scan.nextDouble() instead of scan.nextFloat()
     5 - Lastly, take a look at Java's Math classes, so you can perform some operations in an easier way, for example, instead of area = pi * (raio * raio); use area = pi * (Math.pow(raio, 2));

    I will leave below a code that I would do and I find it to be the easiest way to solve your problem

    import java.util.Scanner;
    
    public class Exercicio3 {
    
        public static void main(String[] args) {
    
            Scanner scan = new Scanner(System.in);
    
            double raio, area;
    
            System.out.println("Escreva o valor do raio:");
            raio = scan.nextDouble();
    
            area = 3.1416 * (Math.pow(raio, 2));
    
            System.out.println("O valor da area eh:" + area);
    
        }
    }
    
        
    03.03.2018 / 00:23