Reputation: 11
I have to make a table in Java using loops. It needs to print out 1-78 in one column, then 15-75 every five times (e.g. 15, 20, 55, ... 75) 78 times(as the loop needs to print out a total of 78 values; so the loop needs to iterate 6 times) in the second column, then print out 15-30 every three times (e.g. 15, 18, 21, 24 , ...30)78 times (as the loop needs to print out a total of 78 values; so the loop needs to iterate 13 times), in the third column and then the number four in the fourth column. This is what I have so far:
import java.util.Scanner;
public class Sandbox {
public static void main(String[] args) { //main method
int angles = 10;
double cosines =4;
int meters =12;
for (int i =1; i<=78;i++){
System.out.println(i);
}
for (int j = 1; j <=6; j++) {
for (angles =10 ;angles<75;) {
angles +=5;
for (meters=12; meters <30;) {
meters +=3;
int num1= 4;
System.out.print( ")" +angles + "|"+ meters) +4);
}
}
}
This outputs the correct values but prints them in a way such that it will be 1-78 then meters = 30 then the values of 15-75 78 times.
Upvotes: 1
Views: 16882
Reputation: 48640
You can calculate the value given the index, max, factor, and offset for the type of data. With this information, you can generate each row, no matter which index you are at.
public class TablePrinter {
public static interface RowPrinter {
String print(int rowIndex, int dataIndex, Object[][] data);
}
public static void main(String[] args) {
// { label, max, factor, offset }
Object[][] data = {
{ "angles", 75, 5, 10 },
{ "meters", 30, 3, 12 }
};
printTable(data, new RowPrinter() {
@Override
public String print(int rowIndex, int dataIndex, Object[][] data) {
int max = (int) data[dataIndex][1];
int factor = (int) data[dataIndex][2];
int offset = (int) data[dataIndex][3];
return String.format(" | %8d", calculate(rowIndex, max, factor, offset));
}
});
}
public static void printTable(Object[][] data, RowPrinter rowPrinter) {
// Print headers
System.out.printf("%8s", "index");
for (int i = 0; i < data.length; i++) {
System.out.printf(" | %8s", data[i][0]);
}
System.out.println();
// Print data rows
for (int i = 0; i < 75; i++) {
System.out.printf("%8d", i + 1);
for (int j = 0; j < data.length; j++) {
System.out.printf(rowPrinter.print(i, j, data));
}
System.out.println();
}
}
public static int calculate(int index, int max, int factor, int offset) {
return ((index * factor) % (max - offset + factor)) + offset;
}
}
index | angles | meters
1 | 10 | 12
2 | 15 | 15
3 | 20 | 18
4 | 25 | 21
5 | 30 | 24
6 | 35 | 27
7 | 40 | 30
8 | 45 | 12
9 | 50 | 15
10 | 55 | 18
11 | 60 | 21
12 | 65 | 24
13 | 70 | 27
14 | 75 | 30
15 | 10 | 12
16 | 15 | 15
17 | 20 | 18
18 | 25 | 21
19 | 30 | 24
20 | 35 | 27
21 | 40 | 30
22 | 45 | 12
23 | 50 | 15
24 | 55 | 18
25 | 60 | 21
26 | 65 | 24
27 | 70 | 27
28 | 75 | 30
29 | 10 | 12
30 | 15 | 15
31 | 20 | 18
32 | 25 | 21
33 | 30 | 24
34 | 35 | 27
35 | 40 | 30
36 | 45 | 12
37 | 50 | 15
38 | 55 | 18
39 | 60 | 21
40 | 65 | 24
41 | 70 | 27
42 | 75 | 30
43 | 10 | 12
44 | 15 | 15
45 | 20 | 18
46 | 25 | 21
47 | 30 | 24
48 | 35 | 27
49 | 40 | 30
50 | 45 | 12
51 | 50 | 15
52 | 55 | 18
53 | 60 | 21
54 | 65 | 24
55 | 70 | 27
56 | 75 | 30
57 | 10 | 12
58 | 15 | 15
59 | 20 | 18
60 | 25 | 21
61 | 30 | 24
62 | 35 | 27
63 | 40 | 30
64 | 45 | 12
65 | 50 | 15
66 | 55 | 18
67 | 60 | 21
68 | 65 | 24
69 | 70 | 27
70 | 75 | 30
71 | 10 | 12
72 | 15 | 15
73 | 20 | 18
74 | 25 | 21
75 | 30 | 24
Upvotes: 1
Reputation: 86306
A possible solution is to build the entire output in a 78-by-4 (or 4-by-78) array first, then printing it all in two nested loops. If that is easier for you or feels more natural. This way you could reuse the idea and much of the code in your original program.
public static void main(String[] args) {
int[][] table = new int[4][78];
// generate table
int angles = 10;
double cosines = 4;
int meters = 12;
int line = 0;
for (int i = 1; i <= 78; i++) {
table[0][line] = i;
line++;
}
line = 0;
for (int j = 1; j <= 6; j++)
for (angles = 10; angles < 75;) {
angles += 5;
table[1][line] = angles;
line++;
}
line = 0;
for (int j = 1; j <= 13; j++)
for (meters = 12; meters < 30;) {
meters += 3;
table[2][line] = meters;
table[3][line] = 4;
line++;
}
// print table
for (line = 0; line < 78; line++) {
System.out.print(table[0][line]);
for (int column = 1; column < 4; column++) {
System.out.print("|" + table[column][line]);
}
System.out.println();
}
}
Upvotes: 1
Reputation: 4673
What you need is to build the input inside of the overall loop, assuming you want a table like
1 | 15 20 25 30 ... 75 | 15 18 21 ... 30 | 4
2 | 15 20 25 30 ... 75 | 15 18 21 ... 30 | 4
....
In order to build this up, you'll need nested loops and output. It can be quite simple as long as you make the loops simple.
for (int i = 1; i <= 78; i++)
{
System.out.print(i + " | ");
for (int j = 15; j <=75; j += 5)
System.out.print(j + " ");
System.out.print("| ");
for (int k = 15; k <= 30; k += 3)
System.out.print(k + " ");
System.out.print("| 4 |");
System.out.println("");
}
As Kira Yakuza mention in the comments, you would be better off dividing this into methods.
Note that your question is somewhat unclear on the sample output, and this is how I interpreted what you wrote. If you desire a different output, you should edit your question to clarify.
Upvotes: 1
Reputation: 7403
This I think does what you want:
public static void main(String[] args) { //main method
int angles = 15;
double cosines =4;
int meters =15;
for (int i =1; i<=78;i++){
System.out.print(i);
System.out.print('|');
System.out.print(angles);
angles+=5;
if (angles>75){
angles=15;
}
System.out.print('|');
System.out.print(meters);
meters+=3;
if (meters>30){
meters=15;
}
System.out.println("|4|");
}
}
You're confused about all the numbers of loops, the thing is you need to generate 78 rows, to you need to loop 78 times, full stop. Then for each loop you print your variables, increment and reset them as needed, and loop back.
Upvotes: 1