Everything points to the following loop is not correct:while (true) {
m = sc.nextDouble();
if (m == -1) {
break;
}
x = sc.nextDouble();
j = sc.nextDouble();
v = sc.nextDouble();
s = sc.nextDouble();
d = sc.nextDouble();
}
Why? Basically two reasons:You can only get out of it when m==-1Which means you're already overwriting the value of m.For each row of values (m,x,jYou don't. Absolutely nothing.. Upon reaching the next row the previous results are overwritten.These two conditions together give rise to the following: When you leave the loop you will begin to calculate the end of the range... in this case m It will be always. -1 and the rest of the values will correspond only to those of the last row.What? m Whatever. -1 has implications in the following line:if(m<x && m<j && m<v && m<s && m<d) menor="MARTES";
To see it clearer:if(-1<x && -1<j && -1<v && -1<s && -1<d) menor="MARTES";
I mean, presumably. -1 will be less than the rest of the values.Another weird look I see is that you're driving int instead of double. The variables should therefore be of type int to avoid problems in comparisons:int m , x = 0 , j = 0, v = 0, s = 0, d = 0;
m = sc.nextInt(); // Y sucesivos...
To simplify things you should consider using an array to store values:int[] valores = new int[6];
So the 0 index will store the value of Tuesday, the 1 for Wednesday, ... Well, filling in the array could be done like this:while (true) {
int valor = sc.nextInt();
if (valor == -1) {
break;
}
valores[0] = valor; // Valor del martes
for( int = 1; i<valores.length; i++) // Resto de la semana
valores[i] = sc.nextInt();
}
And to know the larger and smaller elements:int minIdx = 0;
int maxIdx = 0;
for( int i=1; i<valores.length; i++ )
{
if( valores[minIdx] > valores[i] ) minIdx = i;
if( valores[maxIdx] < valores[i] ) maxIdx = i;
}
Now you've got it. minIdx the index with less value and maxIdx the index that has the highest value. With this data extracting the texts could be trivial:String[] nombres = {"MARTES", "MIERCOLES", "JUEVES", "VIERNES", "SABADO", "DOMINGO" };
System.out.println(nombres[maxIdx] + " " + nombres[minIdx] + " " + media);
Solution without arraysCapture loopwhile (true) {
int valor = sc.nextInt();
if (valor == -1) {
break;
}
m = valor; // Valor del martes
x = sc.nextInt();
j = sc.nextInt();
v = sc.nextInt();
s = sc.nextInt();
d = sc.nextInt();
}
Class of utilities not to repeat codeclass Utils
{
public static String GetDiaSemana(int valor, int m, int x, int j, int v, int s, int d)
{
if( valor == m )
return "MARTES";
else if( valor == x )
return "MIERCOLES";
else if( valor == j )
return "JUEVES";
else if( valor == v )
return "VIERNES";
else if( valor == s )
return "SABADO";
return "DOMINGO";
}
}
Detection of the elder and the minorint minIdx = 0;
int maxIdx = 0;
int valorMin = min(min(min(min(min(m,x),j),v),s),d);
int valorMax = max(max(max(max(max(m,x),j),v),s),d);
String menor = Utils.GetDiaSemana(valorMin,m,x,j,v,s,d);
String mayor = Utils.GetDiaSemana(valorMax,m,x,j,v,s,d);
System.out.println(mayor + " " + menor + " " + media);