0

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;
    }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45
  • 2
    "Also, I need to have two methods in the program and use the boolean method but I got stuck on it." - where exactly? – Gurkirat Singh Guliani Jan 30 '22 at 00:04
  • `a || b && c || d` is same as `a || (b && c) || d`. Also are you sure you need `||`? Note that `||` represents *sum* or ranges, while `&&` represents *intersection*. – Pshemo Jan 30 '22 at 00:09
  • Does this answer your question? [Nested functions in Java](https://stackoverflow.com/questions/7367714/nested-functions-in-java) – Alexander Ivanchenko Jan 30 '22 at 02:29

2 Answers2

2

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;
    }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45
  • Thanks man! Also, to substitute the print true that shows and use a if statement answering if (true) System.out.print ("Both are between 18 and 65") and for false Neither are between 18 and 65") – Jorge de Paula Jr Jan 30 '22 at 00:50
0

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;
}
Sercan
  • 4,739
  • 3
  • 17
  • 36