I have this code:
String[] sweetFlevors = { "Caramel", "Cinnamon", "Wetermelon", "Backed Beans" };
String[] savoryFlavors = { "Sea Salt", "Potato Chip", "Carrot", "Barbque" };
int[] numberRows = { 1, 2, 3, 4 };
int[] numberCol = { 1, 2 };
for (int col = 0; col < numberCol.length; col++) {
System.out.println(" " + numberCol[0] + " " + numberCol[1]);
for (String sweet: sweetFlevors) {
for (String savory: savoryFlavors) {
for (int row = 0; row < numberRows.length; row++) {
System.out.print(numberRows[row] + ". ");
System.out.println(sweet + " and " + savory);
}
}
}
}
and my Output is something like this:
1 2
1. Caramel and Sea Salt
2. Caramel and Sea Salt
3. Caramel and Sea Salt
2. Wetermelon and Carrot
3. Backed Beans and Barbque
4. Backed Beans and Barbque
1 2 // How to delete this line?
1. Caramel and Sea Salt
2. Caramel and Sea Salt
I dont know how I can delete the second line column? and why I get it?
thanks you for help me :)
You're printing the column header twice because you have put it inside the for
loop for (int col = 0; col < numberCol.length; col++)
. You have to do it like this:
String[] sweetFlevors = { "Caramel", "Cinnamon", "Wetermelon", "Backed Beans" };
String[] savoryFlavors = { "Sea Salt", "Potato Chip", "Carrot", "Barbque" };
int[] numberRows = { 1, 2, 3, 4 };
int[] numberCol = { 1, 2 };
System.out.println(" " + numberCol[0] + " " + numberCol[1]);
for (int col = 0; col < numberCol.length; col++) {
for (String sweet: sweetFlevors) {
for (String savory: savoryFlavors) {
for (int row = 0; row < numberRows.length; row++) {
System.out.print(numberRows[row] + ". ");
System.out.println(sweet + " and " + savory);
}
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments