Calculate circle area

Asked

Viewed 7,785 times

2

I’m having trouble writing this exercise in Language Java, using the IDE Eclipse:

Develop a program to calculate the area of the circle.

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

How should I write "commands" in the Java language? I started the course this month, which means I can only use the basics.

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

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();
  • Hi Carol when you say that "wheel" does not present any error in the ecplise console or simply stops working ?

  • Viktor Hugo - I click on RUN ,he asks me to type the Ray,I type and then nothing occurs. he does not calculate the area

  • 1

    You’re asking for two values, the raio and the area, soon the program is waiting for the 2

  • Isac, then I have to besides typing the value of Ray type the Pi which in my case is : 3.1416?

  • I reversed the changes because your code is misspelled in the question.

  • Valdeir Psr - excuse my "stupid" what is wrong? because I want to edit to get straight kk

  • There was nothing wrong, as Iscac said... after you typed the lightning he asked for you to enter the area value.

Show 2 more comments

4 answers

7

  1. You don’t need the import java.io.IOException;.

  2. Don’t use the close in the Scanner.

  3. You should not read the area of Scanner. You must calculate it from the radius.

  4. The standard library already includes Math.PI, and so you don’t need to redefine that on the outside.

  5. You are writing the area twice on System.out.print. You only have to do this once.

  6. You’ll probably want to use System.out.println instead of System.out.print. It makes it easier to combine this with other things you do later.

Your code goes 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.

5


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

The body of the method I 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): inserir a descrição da imagem aqui

It is possible instead of declaring final double PI = 3.1416;, not declare any variable and use:

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

I kept thinking about the formatting of the number at the end, and I would like to make the following suggestion:

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

System.out.printf prints the formatted text, in which % indicates where to include the value of the variable indicated after the comma. %,.2f means format the variable value with a comma as 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 running code. With this change, the result is:

inserir a descrição da imagem aqui

  • i use Eclipse ,will the codes run the same in Netbeans?

  • It should, since Eclipse and Netbeans are just a development environment, but the language remains the same. The way of organizing larger applications, with various classes etc, will be a little different, but the code that runs in one must run in the other without the need for changes.

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 back to the problem, after you type the radius value the cursor jumps the line requesting the area value too so it gives the impression that nothing happens. Just remove this line area = scan.nextFloat();

  • I’ll try here then and see if it’s right

2

There are some points you should review:

1 - The import java.io.IOException; is not required in that 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 the close(); in Scanner
4 - As you have set the variables as double, it is recommended that you use scan.nextDouble() instead of scan.nextFloat()
5 - Finally, take a look at the Math classes in Java, 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 think is 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);

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.