I understand that the variable names can be a bit more concise and relevant and also that the 'b' variable is redundant. However if I delete the b variable and I run the code, I get a multiplication table that prints up to num * 9 (it should print up to num * 10).
At the end of the code, if I remove a++ out of the brackets and run the code, I just get the original NUM value printed 10 times.
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String args[]) {
//Write a Java program that takes a number as input and prints its multiplication table up to 10.
//Test Data:
//Input a number: 8
//Expected Output :
//8 x 1 = 8
//8 x 2 = 16
//8 x 3 = 24
//...
//8 x 10 = 80
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter a number");
int num= keyboard.nextInt(), a = 1, b=a++, table = num, i = 0;
for (i=0; i <= 9; i++)
{
System.out.println(table);
table = num * (a++);
}
}
}
Java coffee
Level 5
Please critique the code below and explain how and why I can write it better...
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
2 November, 17:02
You mix a lot of stuff. The first is pretending to use a for loop while using it like a while loop (not using its counter variable, just for the mere count, but for the body you declare an int a).
So usually you either use a for loop like:
You also could get rid of the adding 1 to the B operand if you start your loop at 1.
If you want to use a while loop and declare the counter variable (operandB) 'outside' the loop, then that'll look like:
And here you see a big don't. I've hidden incrementing operandB (using postfix notation) somewhere inside that loop. It is far better to have a dedicated statementfor that either at the beginning or the end of the loops body (better: use the for loop). The postfix notation takes care of the multiplication to happen first and then operandB is incremented.
Other things to mention:
a) If you want a variable to be initialized with a value of 2, then just assign 2. Or, if it already has assigned another value, you can reassign another one. Also you always can increment that variable. You do not need a 'helper' variable b for that. Just do a:
0
Thomas
2 November, 17:03
And
is C style (but nevertheless valid). In Java you usually write
0
Java coffee QA Automation Engineer
3 November, 01:47
Thank you for your response. You mentioned that I didn't use the for loop correctly (something about using the counter a specific way). Could you please clarify that part further?
I used the counter in the for loop. Do you mean that I didnt need to create a variable int i = 0 outside of the loop?
+1
Thomas
3 November, 08:10
I didn't mean 'wrong' in a syntactical way. What I tried to say is that a for loop has not just the purpose to repeat a block x times. It also can create sequences of numbers. So when you need a sequence, the easiest way is to use the for loop.
You need a sequence from 1 to 10? Just let the for loop create it for you and don't bother with declarations outside that loop. You see all necessary declarations in the loop header and all these variables just live as long as the loop does. Your a, b, table and i variable itself live as long as the method does till garbage collection can pick it up.
The next point is, my approach is shorter and you get with just a view what it is doint. For your code one needs to look around and check each var where it is incremented, initialized, modified and how everything plays together.
Shorter code is not always better. But if you write longer code, then it should have a purpose, e.g. better comprehensibility (like the increment operator in my while loop example above).
Back to the loop syntax. Yes, you do not need to create i outside the loop. Variables you just need inside the loop (like the counter variable i you declare and initialize in the loop header - not just initializing like you did).
Example: print the numbers from -100 to 100 in steps of 2. Output also the current iteration.
You see you can create several sequences in one loop.
If you need the count of found numbers after the loop, then you need to declare i outside the loop header as in your example.
0
Java coffee QA Automation Engineer
3 November, 13:39
Ok thanks so much. Actually now I understand better what you mean. They are good points and I will take them onboard.
I'm new to coding and also new to java, so my coding is still quite primitive. Hopefully with time I can write better code.
+1