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... @@
6 odpowiedzi
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.
+ 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.
0
I understanded. Thanks everyone so much.
- 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"!?
- 1
Shadow I just realize this... I apreciate for your help!!