I have an assignment to create a multiplications table using loops and the first column is supposed to go from 1-10 with an 'x' on the top left corner. This is my program:
public class s {
public static void main(String[] args) {
int a = 10;
for (int b = 0; b <= a; b++) {
for (int c = 1; c <= 1; c++) {
System.out.printf ("%3d | ", + b*c );
}
}
System.out.println ();
for (int d = 5; d < a; d++) {
System.out.printf ("-------------");
}
System.out.println ("");
for (int e = 1; e <= a; e++) {
for (int c = 0; c <= a; c++) {
System.out.printf ("%3d | ", e*c );
}
System.out.println ();
}
}
}
This prints all zeros on the first column but I want it to go x, 1, 2, 3, etc. How do I change these values?
Sorry if there are any formatting mistakes or anything, I'm just as new to Stack Overflow as I am to Java but I'm glad I found you.
Your code is already very close to working. The only issue you had is trying to include the left column inside the for loops (which are dedicated to printing the multiplication values). The general form should be:
System.out.printf(... left hand label ...);
for (col counter ...)
System.out.printf(... value based on col ... );
System.out.println();
The adjusted code is:
public class s {
public static void main(String[] args) {
int a = 10;
System.out.printf("%3s | ", "x");
for (int b = 1; b <= a; b++) {
System.out.printf("%3d | ", b);
}
System.out.println();
System.out.printf("----+");
for (int d = 0; d < a; d++) {
System.out.printf("-----+");
}
System.out.println();
for (int e = 1; e <= a; e++) {
System.out.printf("%3d | ", e);
for (int c = 1; c <= a; c++) {
System.out.printf("%3d | ", e * c);
}
System.out.println();
}
}
}
I would also encourage you to use class names that start with a capital letter (by convention class names should be capitalized).