Aqilhex
Aqilhex

Reputation: 135

combining several if statement for one input in java

im begginer in java , could anyone tell me how to combine several if in one input.

I mean something like this "

how old are you?"

when user answer this it works with several if forexample my code is :

public static void main(String[] args) {
    int age = 40;
    Scanner ageField = new Scanner (System.in);
         System.out.print("How old are you? ");
     if(ageField.nextDouble() > age ){
         System.out.print("you are over than 40 years old"); 

     }else if(ageField.nextDouble() < age ){
         System.out.print("you are less than 40");


     }else if(ageField.nextDouble() < 20 ){
         System.out.print("you are less than 20");
         }else {

             System.out.print("enter your age");
         }
    }
}

i mean the answer should based on the given value,hope you get what im saying

Upvotes: 1

Views: 1314

Answers (3)

Ivan Pronin
Ivan Pronin

Reputation: 1906

Here's actually one of possible optimization OP has asked for. One if statement that can be reused as much as needed. The program will ask for input for the number of your items in ages list:

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        List<Integer> ages = Arrays.asList("20", "40").stream().map(Integer::valueOf).collect(Collectors.toList());
        try (Scanner ageField = new Scanner(System.in))
        {
            System.out.print("How old are you? ");
            ages.forEach(e -> analyzeAge(ageField.nextInt(), e));
        }
    }

    private static void analyzeAge(int ageInput, int ageCompared)
    {
        String answer = null;
        if (ageInput > ageCompared)
        {
            answer = "You are older than " + ageCompared;
        }
        else if (ageInput < ageCompared)
        {
            answer = "You are younger than " + ageCompared;
        }
        else
        {
            answer = "You are exactly " + ageCompared + " years old";
        }
        System.out.println(answer);
    }
}

Upvotes: 1

Eddie Martinez
Eddie Martinez

Reputation: 13910

Your code will not work because you are calling nextDouble() several times. Instead, store the age variable in an int and perform your if statements against this age variable.

Scanner sc = new Scanner (System.in);
System.out.print("How old are you? ");
int age = sc.nextInt();

if(age > 40){
  System.out.print("You are more than 40");
}else if(age < 40 && age >= 30){
  System.out.print("You are less than 40");
} else if(age < 30) {
  System.out.print("You are less than 30");
}
....

Upvotes: 0

Your code is not working because you are discarding the user input ate checking the 1st conditional if...

Store the user input (which BTW should be an integer instead of a double)

ageField.nextInt()

In a variable and use that in the if else conditions... no need to call get Double several times

Upvotes: 1

Related Questions