Reputation: 27
I would like help with my java program. I'm trying to compare two different inputs from the user and check if these inputs are between the minimum and maximum age. Also, I need to have two methods in the program and use the boolean method but I got stuck on it. Can someone help me to understand it better and fix it? What I have so far is:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
String nome, nomes;
int ida, idade;
final int MIN_AGE = 18;
final int MAX_AGE = 65;
Scanner diSexta = new Scanner (System.in);
System.out.println("Please enter first person's name:");
nome = diSexta.nextLine();
System.out.print("Please enter " + nome + "'s age:");
ida = diSexta.nextInt();
diSexta.nextLine();
System.out.println("Please enter second person's name:");
nomes = diSexta.nextLine();
System.out.print("Please enter " + nomes + "'s age:");
idade = diSexta.nextInt();
public static boolean estado() {
if (ida >= MIN_AGE || ida <= MAX_AGE && idade >= MIN_AGE || idade <= MAX_AGE) {
return true;
} else {
return false;
}
}
Upvotes: 0
Views: 93
Reputation: 5091
Logic error in if
statement: All conditions must be evaluated with &&
. Because the method returns true when ida
variable is greater than MIN_AGE
and MAX_AGE
. The same applies to the control of the idade
variable.
public static boolean estado()
{
if((ida >= MIN_AGE && ida <= MAX_AGE) && (idade >= MIN_AGE && idade <= MAX_AGE))
return true;
return false;
}
Upvotes: 0
Reputation: 17510
Your current code does not compile. You need to make MIN_AGE
and MAX_AGE
constants. You also need to move estado()
method outside main()
method. And then you need to review your estado()
implementation. It seems you are looking for something along the following lines:
import java.util.*;
public class Main {
static final int MIN_AGE = 18;
static final int MAX_AGE = 65;
public static void main(String[] args) {
String nome, nomes;
int ida, idade;
Scanner diSexta = new Scanner(System.in);
System.out.println("Please enter first person's name:");
nome = diSexta.nextLine();
System.out.print("Please enter " + nome + "'s age:");
ida = diSexta.nextInt();
diSexta.nextLine();
System.out.println("Please enter second person's name:");
nomes = diSexta.nextLine();
System.out.print("Please enter " + nomes + "'s age:");
idade = diSexta.nextInt();
System.out.print(estado(ida, idade));
}
public static boolean estado(int ida, int idade) {
return ida >= MIN_AGE && ida <= MAX_AGE && idade >= MIN_AGE && idade <= MAX_AGE;
}
}
Upvotes: 2