0

CALCULATING TROUBLE

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int houses = input.nextInt(); int per= 2/houses*100; //your code goes here System.out.println(per); } } My code's ouput is 0. I don't get it. Could some one give me a explanation, please... @@

4th Apr 2020, 9:48 AM
Le Hoang Tuan
Le Hoang Tuan - avatar
5 Answers
0
As we already said, you have to round the value up. round() rounds either up or down, depending on the number, so instead, you have to utilize Math.ceil(). Also, you should print an integer, because if you print a floating point value, the decimal point will be printed as well, so that the solutions don't match. Putting this together, try replacing Math.round( per ) with ( int )Math.ceil( per ) and you should be good to go.
5th Apr 2020, 8:49 AM
Shadow
Shadow - avatar
+ 1
If you divide an integer by another integer, the result is again an integer, so all decimal places are ignored, that's why you get zero when "houses" is bigger than two. If you expect a floating point value, at least one of the operands should be a floating point number itself, e.g. int per = 2.0 / houses * 100; Also, note that conversion to int will effectively round the value down, while the task requires you to round the value up.
4th Apr 2020, 9:56 AM
Shadow
Shadow - avatar
0
I understanded. Thanks everyone so much.
4th Apr 2020, 10:46 AM
Le Hoang Tuan
Le Hoang Tuan - avatar
- 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); float houses = input.nextInt(); float per= 2/houses*100; //your code goes here System.out.println(Math.round(per)); } } My code can't through the texting 3 :(((((( I have no idea. Could someone help me to find "a torch"!?
5th Apr 2020, 7:29 AM
Le Hoang Tuan
Le Hoang Tuan - avatar
- 1
Shadow I just realize this... I apreciate for your help!!
5th Apr 2020, 8:55 AM
Le Hoang Tuan
Le Hoang Tuan - avatar