Click here to Skip to main content
15,896,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using that as a code for my menu, however when a second number is higher than the first number it inputs = 0, how do I fix it?
For example first number: 2, second number:10 and results is 0 why?

What I have tried:

import java.util.Scanner;
public class Main2
{
	public static void main(String[] args) {
	    int choice,firstnum,secondnum;
        Scanner sc = new Scanner(System.in);

	 
	   while(true) {
    		System.out.println("==================================");
    		System.out.println("  S E L E C T  O P E R A T I O N");
    		System.out.println("==================================");
    		System.out.println(" [1] --> Computation of Power");
                System.out.println("----------------------------------");
    		System.out.println("Enter choice:");
    		choice = sc.nextInt();
    		System.out.println("Enter first number:");
    		firstnum = sc.nextInt();
    		System.out.println("Enter second number:");
    		secondnum = sc.nextInt();
    		
    		}
    		else if(choice == 1){
    		   System.out.println("Result: "+(firstnum/secondnum));
    		 
    		}
Posted
Updated 4-Oct-20 1:24am
v3
Comments
Greg Utas 4-Oct-20 6:45am    
If you don't post any code, with an example of the input that is failing, how can anyone possibly help you?
Arnold Christian Tabada 4-Oct-20 6:59am    
Oh my bad, I am sorry.
Patrice T 4-Oct-20 6:47am    
Remember 1 thing: basically we don't know what you talk about, we don't see your screen, we can't read your mind.
Arnold Christian Tabada 4-Oct-20 6:59am    
My bad, I am sorry.

In integer arithmetic 2/10 = 0.

But it is still far from clear what you are trying to do.
 
Share this answer
 
Without seeing the rest of your code, I'm guessing that firstnum and secondnum are defined as integers. So when secondnum > firstnum, the result is truncated to 0.
 
Share this answer
 
Comments
Arnold Christian Tabada 4-Oct-20 7:11am    
I am sorry, I am new to this thing. If I've been a pain in the ass, I am sorry.
Greg Utas 4-Oct-20 7:35am    
In the expanded code that you've posted, the "while(true)" ends at the "}" before the "else if", so I can't even see how it would compile successfully.
Quote:
System.out.println("Result: "+(firstnum/secondnum));

That's because you are dividing two integers where numerator is less than denominator. With integers divisibility, you will get back quotients only.

5/7 => 0 as quotient and 5 as remainder.
Quote:
This is a binary operator that is used to divide the first operand(dividend) by the second operand(divisor) and give the quotient as result.


Read about mathematical operations here: http://tutorials.jenkov.com/java/math-operators-and-math-class.html#division[^]

You need to read basics in case you were not aware of this.
 
Share this answer
 
Quote:
first number: 2, second number:10 and results is 0 why?

May be the result of a division of integers is an integer, thus 2/10 is 0.
try
Java
((double)firstnum/secondnum)

May be you should read the documentation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900