+ 1

What process or method in java i use to define my inputted integer that it is negative or positive?

example .if i input 2 negative integers it will terminate at ask to input again another integers .. and if i input 2 positive integer it will multifly both numbers

15th Oct 2017, 11:11 AM
SingleName
SingleName - avatar
10 Réponses
+ 12
import java.util.Scanner; public class main { public static void main (String[] args) { Scanner reader = new Scanner(System.in); While (true) { System.out.println("Enter number 1:"); int n1 = reader.nextInt(); System.out.println("Enter number 2:"); int n2 = reader.nextInt(); if (n1 >= 0 && n2 >= 0) { int p = n1 * n2; System.out.println(p); break; } } } } This should do the job. Hope it helps. =)
15th Oct 2017, 11:36 AM
Mayur N Mallya
Mayur N Mallya - avatar
+ 12
Use the code I've mentioned above, the while loop will keep looping and asking for both positive input. The loop will only break if both inputs are positive.
15th Oct 2017, 11:41 AM
Mayur N Mallya
Mayur N Mallya - avatar
+ 12
then the it will loop and ask you for number again
15th Oct 2017, 11:45 AM
Mayur N Mallya
Mayur N Mallya - avatar
+ 12
No problem, glad to help. 😁
15th Oct 2017, 11:49 AM
Mayur N Mallya
Mayur N Mallya - avatar
+ 2
hi if you provide 2 negative integers that becomes positive right.
15th Oct 2017, 11:31 AM
Nanda Balakrishnan
+ 1
i want the program if i nput negative integers the program will ask again to input another integer ... thats my problem now .. what are the process or method i use?
15th Oct 2017, 11:38 AM
SingleName
SingleName - avatar
+ 1
tnx shadow i try it to run in my compiler ..
15th Oct 2017, 11:40 AM
SingleName
SingleName - avatar
+ 1
if i input negative integer?
15th Oct 2017, 11:43 AM
SingleName
SingleName - avatar
+ 1
much*
15th Oct 2017, 11:46 AM
SingleName
SingleName - avatar
0
wow thnk you so match shdow .. i will study your code
15th Oct 2017, 11:46 AM
SingleName
SingleName - avatar