Help with an exercise in java

-2

Create a program that requests two integer values A, B by keyboard and then shows in screen the sum of all even numbers greater than A and less than B. It must be validated that the number B is greater than the number A, otherwise the numbers must be requested again when user.

In eclipse or in any other program that uses java language.

This is what I have tried so far:

public class error {

    int a;
    int b;

    public void valor1(){
       for(int i=0;i>a;i++){
           if(i>a){

           }    
       }

    }
}
    
asked by Diego Alejandro 20.10.2016 в 23:10
source

4 answers

1

Try this way:

import java.util.Scanner;
public class RangePrinter {
    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        int min = 0;
        int max = 0;

        while(min >= max)
        {
            System.out.println("Ingresa el primer numero");
            min=reader.nextInt();

            System.out.println("Ingresa el segundo numero");
            max=reader.nextInt();

            if(min >= max)
                System.out.println("El rango ingresado es incorrecto.");
        }

        int result = 0;

        for (int i=min + 1; i < max; i++)
        {
            if(i % 2 == 0)
                result += i;
        }

        System.out.println("Resultado: "+result);
   }
}
    
answered by 20.10.2016 в 23:18
1

You can try something like this

The user enters the values, verifying that numero is always greater than numero2 , if not, we repeat until that condition is fulfilled.

public static void main(String[] args) {
    int suma = 0;
    int numero = 0;
    int numero2 = 0;
    Scanner reader = new Scanner(System.in);
    do {
        System.out.println("Ingresa el primer numero");
        numero = reader.nextInt();

        System.out.println("Ingresa el segundo numero");
        numero2 = reader.nextInt();

        if (numero > numero2)
            System.out.println("El primer numero debe ser menor al segundo");
    } while (numero > numero2);

    //Se suma 1, porque el enunciado dice números pares MAYORES A (numero2) y menores que B (numero)
    for (int i = numero + 1; i < numero2; i++) {
        if (i % 2 == 0) {
            System.out.println("numero par " + i);
            suma = suma + i;
        }
    }
    System.out.println(suma);
}
    
answered by 20.10.2016 в 23:28
0

You can try the following:

public class error {
public static void main(String args[]){
    int a;
    int b;
    Scanner leerTeclado = new Scanner(System.in);
    while(true){
        System.out.print("Introduce el valor A: ");
        a=leerTeclado.nextInt();
        System.out.print("Introduce el valor B: ");
        b=leerTeclado.nextInt();
        if(b>a)
            break;
    }
    int pos=a+1;
    while(true){
        if(pos<b){
            if(pos%2==0){
                System.out.println("Valor par mayor que A y menor que B: "+pos);
            }
            pos++;
        }else{
            break;
        }
    }
}

}

    
answered by 20.10.2016 в 23:39
0

it could be something like this:

public static void main(String[] args) {
    try {
        Scanner sc = new Scanner(System.in);
        System.out.println("Ingrese a:");
        int a = sc.nextInt();
        System.out.println("Ingrese b:");
        int b = sc.nextInt();
        while(a > b){//si a > b vuelvo a pedir los datos
            System.out.println("el valor de a debe ser menor que b");
            System.out.println("Ingrese a:");
            a = sc.nextInt();
            System.out.println("Ingrese b:");
            b = sc.nextInt();
        }
        //hasta aqui todo bien entonces prosigo a sumar los pares entre a y b
        int suma = 0;
        for(int i = a + 1; i < b; i++){
            if(i % 2 == 0){
                suma = suma + i;
            }
        }
        System.out.println("La suma de los pares entre a y b es:");
        System.out.println(suma);
    } catch (Exception ex) {
        System.out.println("Error no considerado :'(");
    }     
}
    
answered by 20.10.2016 в 23:32