How do I avoid the line breaks when I enter a number?

3

When I choose any option, it asks me to enter a number, but that's when that number goes down and does not follow. For example:

Introduzca un numero:  '1

I want that number to appear there and what happens is:

Introduzca un numero: (no lo hace acá, salta una línea)
1

This is my code:

package prueba;
import java.util.Scanner;
public class Prueba 
{
    public static void main(String[] args)
    {
     int n, num1, num2, resultado;
     Scanner leer=new Scanner(System.in);
     System.out.println("1. Sumar");
     System.out.println("2. Restar");
     System.out.println("3. Multiplicar");
     System.out.println("4. Dividir ");
     System.out.println("Elige una opcion del menu: ");
     n=leer.nextInt();

     switch (n)
             {
         case 1:
             System.out.println("ingresa un numero: ");
             num1=leer.nextInt();
             System.out.println("ingresa otro numero: ");
             num2=leer.nextInt();
             System.out.println("El resultado es: "+(num1+num2));
             break;

         case 2:
             System.out.println("ingresa un numero: ");
             num1=leer.nextInt();
             System.out.println("ingresa otro numero: ");
             num2=leer.nextInt();
             System.out.println("El resultado es: "+(num1-num2));
             break;

         case 3:
             System.out.println("ingresa un numero: ");
             num1=leer.nextInt();
             System.out.println("ingresa otro numero: ");
             num2=leer.nextInt();
             System.out.println("El resultado es: "+(num1*num2));
             break;

         case 4:
             System.out.println("ingresa un numero: ");
             num1=leer.nextInt();
             System.out.println("ingresa otro numero: ");
             num2=leer.nextInt();
             System.out.println("El resultado es: "+(num1/num2));
             break;    

         default:
             System.out.println("Opcion no valida");
             break;
             }

    }
}
    
asked by Mauro 07.12.2016 в 00:53
source

2 answers

5

Use System.out.print instead of System.out.println for example:

   ...
   ...
   System.out.print("ingresa un numero: ");
   num1= leer.nextInt();
   System.out.print("ingresa otro numero: ");
   num2=leer.nextInt();
   System.out.print("El resultado es: "+(num1+num2));
   ...
   ...

the difference between System.out.print and < strong> System.out.println is that the last one inserts a jump at the end line (move the cursor to a new line) and System.out.print do not do it, for that reason you can continue writing on the same line.

Here is a good article: Using print and println

    
answered by 07.12.2016 в 01:03
4

You should use print instead of println :

System.out.println("ingresa un numero: ");

should be:

System.out.print("ingresa un numero: ");

so that the number does not appear on the next line.

Actually the difference between both is that print will write the text you have specified without a line break, therefore the rest of the text that you type by console will be written on the same line.

On the contrary, println introduces the text with a line break at the end and that is why the numbers are written to you on the next line.

    
answered by 07.12.2016 в 01:03