'Hi, I have a problem with the array with the data entry
I have a problem with arrays, I would like to create a table by entering the students and grades of each student in the following subjects. But I have a problem when I go to print on the console, the rows and columns are not matched, also I should find the highest and the lowest number. And try to do the average. HERE IS AN IMAGE OF THE OUTPUT: OUTPUT
public static int voti1 = 0;
public static int voti2;
public static int voti3;
public static int voti4;
public static int somma = 0;
public static int media;
public static void main(String[] args) {
String[] materie = { "Italiano", "Musica", "Geografia", "Biologia" };
for (int i = 0; i < materie.length; i++) {
System.out.printf("%34s", materie[i]);
}
String[] nomiAlunni = { "Derby Harringthon", "Gord Vendome", "Justin Vandervelde", "Tad
Smith" };
for (int j = 0; j < nomiAlunni.length; j++) {
System.out.print("\n" + nomiAlunni[j]);
}
String votiAlunno1[] = { "8", "4", "5", "6" };
String votiAlunno2[] = { "3", "6", "9", "8" };
String votiAlunno3[] = { "5", "9", "10", "7" };
String votiAlunno4[] = { "10", "9", "8", "3" };
for (int i = 0; i < votiAlunno1.length; i++) {
voti1 = Integer.parseInt(votiAlunno1[i]);
System.out.printf("%27s", votiAlunno1[i]);
}
for (int i = 0; i < votiAlunno2.length; i++) {
voti2 = Integer.parseInt(votiAlunno2[i]);
System.out.print("\t\t" + votiAlunno2[i]);
}
voti1 = Integer.parseInt(votiAlunno1[0]);
voti2 = Integer.parseInt(votiAlunno2[1]);
voti3 = Integer.parseInt(votiAlunno3[2]);
voti4 = Integer.parseInt(votiAlunno4[3]);
/*
* for(i = 1; i<votiAlunno2.length; i++) { for (int j = 1; j <
* votiAlunno2[i].length(); j++) { System.out.printf("%30s", "\t" +
* votiAlunno1[i]); }
*
*/
for (int i = 0; i < votiAlunno1.length; i++) {
voti1 = Integer.parseInt(votiAlunno1[i]);
somma = somma + voti1;
media = voti1 / somma;
}
System.out.println("La media dei voti è" + media);
}
}
enter code here[enter image description here][1]
Solution 1:[1]
I guess you are printing a table.
String[] nomiAlunni = { "Derby Harringthon", "Gord Vendome", "Justin Vandervelde", "Tad
Smith" };
for (int j = 0; j < nomiAlunni.length; j++) {
System.out.print("\n" + nomiAlunni[j]);
}
You print the nomiAlunni spanning through lines already. Note that print goes sequentially; once the stream is flushed, you cannot go back to the previous lines. Instead, try to do something like this:
for (int i = 0; i < nomiAlunni.length; i++) {
System.out.printf("%27s", nomiAlunni[i]);
for (int j = 0; j < votiAlunno[i].length; j++)
System.out.printf("\t%s", votiAlunno[i][j]);
}
Try to implement noniAlunni as a multidimensional array to save lines. You need not parseInt for printing; toString() of any object is called when printing. Try to be consistent with \t: java is intelligent enough
to optimize your output.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | luma |
