+ 1
user input 2 numbers , add and print here in sl ?? java
is there some problem with my system or more than one user input in java here at SL not allowed??
3 Respostas
+ 7
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
int i,j;
Scanner inp=new Scanner(System.in);
i=inp.nextInt();
j=inp.nextInt();
System.out.println(i+j);
}
}
Input should be in separate lines :
1
2
output:3
+ 3
Multiple inputs can be collected by separating them with a new line.
Example (Code Playground):
1
2
+ 1
ok i got it that in same input box we can input more than 1 value pressing enter each time
but how user will know about number of inputs??
public class Program
{
public static void main(String[] args) {
public class Program {
public static void main(String[] args) {
int i,j;
System.out.println("Enter two numbers to add:");
//this msg gets displayed with result
Scanner inp=new Scanner(System.in);
i=inp.nextInt();
j=inp.nextInt();
System.out.println(i+j);
}
}
}
}
//Input should be in separate lines :
1
2
output:3