Allow operator entry by the user

0

I want to replace the variables izquierda and derecha with user entries.

How could I do it?

public class Test {
/**
 * @param args the command line arguments
 */

    public static void main(String[] args) {

        float calculos = 0.1f;
        boolean control = true;
        float x = 0;

        while (control){
            float margenDeError = 0.001f; // Margen de error

            for (int i = 0 ; i < 2 ; i++){ // check
                float izquierda = 2 + x; // Parte de la ecuacion
                float derecha = x * x;  // Parte de la ecuacion

                float maxP = Math.max(Math.abs(izquierda), Math.abs(derecha));
                float minP = Math.min(Math.abs(izquierda), Math.abs(derecha));
                control = (maxP - minP > margenDeError); 
                System.out.println(x);

                if (control && i == 0){
                    x = -x;
                }else if (!control){
                    System.out.println(x);
                    break;
                }else{
                    x = Math.abs(x);
                    x += calculos;
                }
            }
        }
    }
}
    
asked by Esteban Druetta 13.12.2017 в 18:24
source

1 answer

1

You can use Scanner:

Scanner teclado = new Scanner(System.in);
teclado.useLocale(Locale.US); // o teclado.useLocale(Locale.FRANCE); para Europa
System.out.println("Ingresa el operando izquierdo: ");
float izquierdo = keyboard.nextFloat();
System.out.println("Ingresa el operando derecho: ");
float derecho = keyboard.nextFloat();

Code integrated in your program:

import java.util.Scanner;

class Test {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        float calculos = 0.1f;
        boolean control = true;
        float x = 0;
        float izquierda, derecha;
        Scanner teclado = new Scanner(System.in);
        teclado.useLocale(Locale.US);

        while (control) {

            float margenDeError = 0.001f; //Margen de error

            for (int i = 0; i < 2; i++) { //check
                System.out.print("Ingresa el operando izquierdo: ");
                izquierda = teclado.nextFloat();
                System.out.print("Ingresa el operando derecho: ");
                derecha = teclado.nextFloat();

                float maxP = Math.max(Math.abs(izquierda), Math.abs(derecha));
                float minP = Math.min(Math.abs(izquierda), Math.abs(derecha));
                control = (maxP - minP > margenDeError);
                System.out.printf("%g\n", x);
                if (control && i == 0) {
                    x = -x;
                } else if (!control) {
                    System.out.println(x);
                    break;
                } else {
                    x = Math.abs(x);
                    x += calculos;
                }
            }
        }
    }
}
    
answered by 13.12.2017 в 19:15