Question
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);
}
}
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);
}
}
Answers
I posted some ideas on this for another student. See
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.
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.
Hi Mr.Steve thank you for helping. I looked on the web and I looked at your responses to the other student. I'm still getting these errors on my code here:
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.
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.
Does the java compiler flag errors? It should say what they are.
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 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?
I think that the semicolon (;) should be deleted in your line:
if (a == b) System.out.println(a*b);
if (a == b) System.out.println(a*b);
If you are using a decent java IDE, it should flag and highlight things like mismatched braces and wrong types.
Related Questions
please, someone answer my question i posted already
I answered your question within 15 minutes. It...
Based on the question topic posted by Britney below titled:In your opinion
(your answer to this rem...
Ming posted this message:
I am interested in learning more about your company’s intensive langu...
Ask a New Question
Menu
Question successfully posted! Ask another question.
×
Ask a New Question...