Je suis assez nouveau à Java et j'ai essayé le Best Before puzzle from Spotify hier. Je reçois l'erreur "Mauvaise réponse" quand je l'envoie. Recherche d'autres solutions n'a pas aidé et je ne peux pas comprendre quelle entrée donne une mauvaise réponse. Au mieux, vous me direz simplement quel code conduit à la mauvaise réponse. Je devrais être capable de corriger le code moi-même alors.Spotify puzzle Meilleur avant
import java.util.Scanner;
public class Bestbefore {
public static void main(String[] args) {
//Process Input String
Scanner scanner = new Scanner(System.in);
String dateString = scanner.nextLine();
Integer a,b,c;
a=Integer.parseInt(dateString.substring(0,dateString.indexOf("/")));
b=Integer.parseInt(dateString.substring(dateString.indexOf("/")+1,
dateString.lastIndexOf("/")));
c=Integer.parseInt(dateString.substring(dateString.lastIndexOf("/")+1));
int[][] va={ //All possible combinations
{a,b,c},
{a,c,b},
{b,a,c},
{b,c,a},
{c,a,b},
{c,b,a}
};
int i=0;
while (!checkDate(va[i][0], va[i][1], va[i][2])
&& i<5) // get the first valid date combination
i++;
//to prevent OutofBoundsException of the while loop
if (!checkDate(va[i][0], va[i][1], va[i][2]))
i++;
if (i==6)
System.out.println(dateString+" is illegal");
else
{ //compare the rest of the va-Array and save the position of the lowest Date in i
for (int k=i+1;k<6;k++)
if (checkDate(va[k][0], va[k][1], va[k][2]))
{
if (ageOfDate(va[k][0], va[k][1], va[k][2])
< ageOfDate(va[i][0], va[i][1], va[i][2]))
i=k;
}
System.out.println(getDate(va[i][0], va[i][1], va[i][2]));
}
}
public static boolean checkDate(int day,int month, int year)
{
int[] dpm={31,28,31,30,31,31,30,31,30,31,30,31}; //Days per Month
if (year<2000)
year=year+2000;
if((year%4==0 && year%100!=0) || year%400==0)
dpm[1]=29; //Leapyear correction
if (month==0 || month>12)
return false;
else
if (day==0 || day>dpm[month-1])
return false;
else
return true;
}
public static int ageOfDate(int day,int month, int year) //to compare 2 dates
{
return ((year*10000)+(month*100)+day);
}
public static String getDate(int day,int month, int year) //for the Output
{
String smonth = String.valueOf(month);
String sday = String.valueOf(day);
if (year<2000)
year=year+2000;
if (month<10)
smonth="0"+smonth;
if (day<10)
sday="0"+sday;
return (year+"-"+smonth+"-"+sday);
}
}
Bonne prise. ....... –
Im tellement stupide ... merci beaucoup izo – geM