I posted this question before:
How would I put this into one program?
These are the directions:
Write a method that takes two parameters. If the first parameter is equal to second parameter. Multiply both and print result.
If the first parameter is less than the second parameter, add the two and print the result 10 times.
If the first parameter is greater than the second parameter, subtract the first parameter from the second and print the result 50 times.
I wrote the program and I just cant figure out how to print the results as a loop.
This is what I've got:
public class IfHomeworkRedo {
public static void two(int a, int b) {
if (a == b) System.out.println(a*b);
else if (a < b) {
for (int i = 0; i < 10; i++) {
System.out.println(a+b);
}
}
else {
b -= a;
}
}
public static void main (String[] args)
{
two(3, 3);
two(3, 4);
two(4, 3);
}
}
5 answers
http://www.jiskha.com/display.cgi?id=1435972699
http://www.jiskha.com/display.cgi?id=1436026108
You really need to learn how to use the net for the immense amount of help that is out there. You can find dozens of code snippets that will do exactly what you want. And the java documentation has many examples as well.
public class IfHomeworkRedo {
2.
3.{
4.
5. public static void two(int a, int b) {
6.
7. if (a == b) System.out.println(a*b);
8.
9. else if (a < b) {
10.
11. for (int i = 0; i < 10; i++) {
12.
13. System.out.println(a+b);
14.
15. }
16. }
17.
18. else if (a > b) {
19.
20. for (int i = 0; i < 50; i++) {
21.
22. System.out.println(b-a);
23.
24.
25.
26. }
27.
28. }
29.
30.
31.
32.
33. public static void main (String[] args)
34.
35. {
36.
37. two(3, 3);
38.
39. two(3, 4);
40.
41. two(4, 3);
42.
43. }
44.
45.}
Can you help look at them?
Thanks.
If it runs, but is wrong, then welcome to the wonderful world of debugging.
Just eyeballing the code, its logic appears ok. What kinds of errors are you getting?
if (a == b) System.out.println(a*b);