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 inEclipse
(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
The values ofA = (b*h)/2
beingb = base
andh = altura
.b
andh
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();
}
}
}