Could someone check this? I'm confused ??? :'( import java.io.*; public class act5 { public static void main(String args[])throws IOException { BufferedReader UInput = new BufferedReader(new InputStreamReader(System.in)); int num; int num2; int num3; int s; int p; int d; double q; String tempNum; String tempNum2; String tempNum3; System.out.print("Enter the first integer: "); tempNum = UInput.readLine(); num = Integer.parseInt(tempNum); System.out.print("Enter the second integer: "); tempNum2 = UInput.readLine(); num2 = Integer.parseInt(tempNum2); System.out.print("Enter the third integer: "); tempNum3 = UInput.readLine(); num3 = Integer.parseInt(tempNum3); s = num + num2 + num3; p = num * num2 * num3; d = num - num2 - num3; q = num / num2 /num3; System.out.println("The sum is " + s); System.out.println(); System.out.println("The product is " + p); System.out.println(); System.out.println("The difference is " +d); System.out.println(); System.out.println("The quotient is " +q); } } I don't get how I should do the quotient part. I need help.
Ahhh coding, my first internet love. I refuse to actually do it for you or give you a hint (personal rule) you're obvious a beginning, which is cool, still good to see some people learning different languages instead of the C family and perl. There are plenty of really good e-books out there. A few my friends have wrote, I'll give him a bell later and grab it from him, if you want.
If that's what you want. but my problem is, I can't get the quotient part of the code to work properly, the result is always = 0.0 .
why are you using strings to hold numbers? it should be int or float. Also check the variables in the quotient line.
I'm just following what our instructor told us to do. The string input is converted to a number, I think.
you fixed the problem I saw. Why is q a double, when s, p, and d are all ints? If anything, p should be a double or long int. while q should really be a float. Can you give some example input/output figures?
q(holds the value of a quotient value), when dividing numbers, you end up with those numbers(9 / 27 / 36 = 0.009259259259), the instructor said that we either use double or float regarding these kind of numbers I didn't do much with the s(sum), p(product), d(difference), because we're dealing with simple numbers. Input/output figures? the java.io figures?
double may not be able to hold decimal numbers (depends on the language, it could just be a long int). p needs to be a double because there's a strong possibility if you're using large numbers the result of the calculation could be higher than the upper boundary of the int datatype, in which case you will be given an incorrect answer. I'm assuming println does not format the output and just prints it as is.
so float could work then? how do I use the "f" tag with the numbers used then? and yes, println doesn't format the output, It seems all I needed to do is to to change "int" to "double" int num; int num2; int num3; double num; double num2; double num3 these variables receive the converted string...,it'll have to do for the time being. EDIT: Sorry for the double post. and thanks for the help. Note: I might be updating this thread when I'll have another problem.
Do you now know why you had to change the int to double? Say you have int a and int b. If you do: a / b, then it's (int / int) and Java (and any other language) in an attempt to maintain some speed decides you want a fast integer division, instead of a slow float or double division. So if you do 9 / 3, there's no reason to make it float, as the answer is int. But if you do 1 / 2, the result is also an integer (whole number). So 1 / 2 in int becomes 0, and 3 / 2 becomes 1. They're rounded.