How to make a program calculate the area of several objects separately using the values given by the user (scanner)

0

I'm new here and new to programming, I believe that the best way to establish concepts and learn is in practice.

So I tried to do some basic programming in Eclipse (I also use Intellij ) and at first it worked, I wanted to use Scanner of java to be able to insert values but in the last program I went to do I could not do it right and I'm at it for hours ...

I think it's very simple, the program consists of calculating the object area (triangle and trapezoid I've inserted, just to test) with the value given by the user by example:

  

The formula of the area of a triangle is A = (b*h)/2 being b = base and h = altura .

  The values of b and h will give the user in the console and so the program will calculate and return the value of the area (A) of agreement with the chosen geometric object.

But the problem is not in the resolution of the account, in this part everything is correct.

The problem is when it is time to choose which geometric object (triangle or trapezoid) it wants to know the area for the program to calculate and execute only the block of code of that object, ignoring the other one since it was not chosen.

My program asks for the value of the two and regardless of what I do it does not only execute one of them, it can be several mistakes that I made and as a layman in this subject I ask for help to solve, either by a totally new code or reusing my .

The following is the code below:

package course;

import java.util.Scanner;

public class UsingScanner1 {

public static void main(String[] args) {
    /*Descubra a área do que o usuário passar
     * Trapézio: A = ((B + b)/2) * h
     * B = Base maior, b = base menor, h = altura
     * 
     * Triângulo: A = (B*h)/2
     * B = Base, h = altura
     */


    System.out.println("Bem-vindo ao calculador de área!");
    System.out.println("Informe qual objeto quer calcular: ");

    Scanner input = new Scanner(System.in);

    boolean trapézio = true, triângulo = true;

    if(trapézio){
        Double B,b,h,A;
        //coletando os valores do usuário
        System.out.println("Informe o valor de B = base maior: ");
        B = input.nextDouble();
        System.out.println("Informe o valor de b = base menor: ");
        b = input.nextDouble();
        System.out.println("Informe o valor de h = altura");
        h = input.nextDouble();
        //calculando
        A = ((B + b)/2)*h;
        System.out.println("O valor dado é B: "+B+" b: "+b+" h: "+h);
        System.out.println("A área do trapézio é: "+A);
        input.close();
    }
    if(triângulo){
        Double B,h,A;
        //coletando valores do usuário
        System.out.println("Informe o valor de B = base: ");
        B = input.nextDouble();
        System.out.println("Informe o valor de h = altura: ");
        h = input.nextDouble();
        //calculando
        A = (B*h)/2;
        System.out.println("O valor dado é B: "+B+" h: "+h);
        System.out.println("A área do triângulo é: "+A);
        input.close();
    }   


}

}

    
asked by anonymous 30.11.2018 / 20:13

1 answer

1
    public static void main(String[] args) {
        /*Descubra a área do que o usuário passar
         * Trapézio: A = ((B + b)/2) * h
         * B = Base maior, b = base menor, h = altura
         *
         * Triângulo: A = (B*h)/2
         * B = Base, h = altura
         */

        boolean trapezio = false, triangulo = false;

        System.out.println("Bem-vindo ao calculador de área!");
        System.out.println("Informe qual objeto quer calcular: ");

        Scanner input = new Scanner(System.in);

        String escolhaDoUsuario = new String();
        escolhaDoUsuario = input.next();

        if(escolhaDoUsuario.equals("trapezio")) {
            trapezio = true;
        } else if(escolhaDoUsuario.equals("triangulo")) {
            triangulo = true;
        }

        if(trapezio){
            Double B,b,h,A;
            //coletando os valores do usuário
            System.out.println("Informe o valor de B = base maior: ");
            B = input.nextDouble();
            System.out.println("Informe o valor de b = base menor: ");
            b = input.nextDouble();
            System.out.println("Informe o valor de h = altura");
            h = input.nextDouble();
            //calculando
            A = ((B + b)/2)*h;
            System.out.println("O valor dado é B: "+B+" b: "+b+" h: "+h);
            System.out.println("A área do trapézio é: "+A);
            input.close();
        }
        if(triangulo){
            Double B,h,A;
            //coletando valores do usuário
            System.out.println("Informe o valor de B = base: ");
            B = input.nextDouble();
            System.out.println("Informe o valor de h = altura: ");
            h = input.nextDouble();
            //calculando
            A = (B*h)/2;
            System.out.println("O valor dado é B: "+B+" h: "+h);
            System.out.println("A área do triângulo é: "+A);
            input.close();
        }
    }

This is a very naive implementation, but the idea was just to finalize it using the logic you developed. A "version 2.0" could have several improvements:

  • What if the user types "TRIANGLE"?
  • Use methods instead of blocks of code.
  • What if the user types a geometric shape not provided by the program? What if he does not type anything?
  • Ask the user if he wants to calculate another figure instead of the program simply quit after executing once.
  • Only use a structure of if .
  • Delete Booleans.

The list could be even bigger. It is for you to study repetition structures such as while , flow structures like switch , investigate the equals() method I used, etc.

    
01.12.2018 / 01:31