Java 益智游戏前最佳

Java 益智游戏前最佳,java,algorithm,Java,Algorithm,可能重复: 我一直在研究这个谜题,因为我是Java新手,我想学习它。所以,我一直在为Spotify解决这个难题。但是每次我提交的时候。它说的是错误的答案。我很确定这是对的,不过谁都能看出我遗漏了什么。这就是问题所在 这是我的解决办法 import java.io.*; import java.util.Arrays; public class best_before { static boolean next; static String month, da

可能重复:

我一直在研究这个谜题,因为我是Java新手,我想学习它。所以,我一直在为Spotify解决这个难题。但是每次我提交的时候。它说的是错误的答案。我很确定这是对的,不过谁都能看出我遗漏了什么。这就是问题所在

这是我的解决办法

       import java.io.*;
import java.util.Arrays;

public class best_before {  
    static boolean next;
    static String month, day, year;

    public static void main(String[] args) throws IOException{

        //Setup to grab the input
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);        
        String input = "";  
        input = br.readLine();

        //Spilt the input to grab each integer entered by user
            String bits = input;
            String[] tokens = bits.split("/");

            int a_value1 = Integer.parseInt(tokens[0]);
            int b_value1 = Integer.parseInt(tokens[1]);
            int c_value1 = Integer.parseInt(tokens[2]);

            //Sort the array in order from lowest to highest
            int[] int_array = new int[] {a_value1, b_value1, c_value1};
            Arrays.sort(int_array);

            int a_value = int_array[0];
            int b_value = int_array[1];
            int c_value = int_array[2];

            year = Integer.toString(a_value);
            month = Integer.toString(b_value);
            day = Integer.toString(c_value);

            //Check the integers entered to put them in the right order
            int check_days = Integer.parseInt(day);
            int check_month = Integer.parseInt(month);
            int check_year = Integer.parseInt(year);

            //Check to make sure none of the values are a negative integer
            if(check_days < 0){
                System.out.println(input + " is illegal");
                System.exit(0);
            }
            else if(check_month < 0){
                System.out.println(input + " is illegal");
                System.exit(0);
            }
            else if(check_year < 0){
                System.out.println(input + " is illegal");
                System.exit(0);
            }

            //Will only change the values around if the highest date in the array is bigger than 31
            if(check_days > 31){

                //Only reorganize if year if larger than month
                if(check_month > check_year){
                    month = Integer.toString(check_year);
                }
                //Otherwise just keep month at its current value
                else{
                    month = Integer.toString(check_month);
                }
                //Change date and year around since one is bigger than the other
                    year = Integer.toString(check_days);
                    day = Integer.toString(check_month);

            }
            else if(check_year == 0){
                if(check_month < check_days){
                    month = Integer.toString(check_month);
                }
                else{
                    month = Integer.toString(check_days);
                }
            }

            //Get the length so I can zero pad the numbers
            int length_year = year.length();
            int length_month = month.length();
            int length_day = day.length();

            //Doing my zero pad thing right here
            if(length_year == 1){
                year = "200" + year;
            }
            else if(length_year == 2){
                year = "20" + year;
            }
            else if(length_year == 3){
                year = "2" + year;
            }
            if(length_month == 1){
                month = "0" + month;
            }
            if(length_day == 1){
                day = "0" + day;
            }

            //A last check to make sure everything is Kosher
            int last_check = Integer.parseInt(year);
            int last_check_month = Integer.parseInt(month);
            int last_check_day = Integer.parseInt(day);

            //Checking to see if it is a leap year. Is the year Divisible by 4?
            if (last_check % 4 == 0) {
                // Is the year Divisible by 4 but not 100?
                if (last_check % 100 != 0) {
                    next = true;
                }
                // Is the year Divisible by 4 and 100 and 400?
                else if (last_check % 400 == 0) {
                    next = true;
                }
                // It is Divisible by 4 and 100 but not 400!
                else {
                    next = false;
                }
            }
            // It is not divisible by 4.
            else {
                next = false;
            }

            //Check to make sure the date is legal and valid :)
            if(last_check > 2999 || last_check < 2000)
            {
                //Date must be between 2000 and 2999 inclusive
                System.out.println(input + " is illegal");
            }
            else if(last_check_month > 12)
            {
                //Date month must be less than 12
                System.out.println(input + " is illegal");
            }
            else if(last_check_day > 31)
            {
                //Date day must be less than 31
                System.out.println(input + " is illegal");
            }
            else if(next == false && last_check_day > 28 && last_check_month == 2){
                //if next is false and the day is greater than 28 and its not a leap year something is wrong
                System.out.println(input + " is illegal");
            }
            else if(next == true && last_check_day > 29){
                //if next is true and the day is greater than 29 and it is a leap year something is wrongs
                System.out.println(input + " is illegal");
            }
            else if(last_check_month == 4 && last_check_day > 30){
                System.out.println(input + " is illegal");
            }
            else if(last_check_month == 6 && last_check_day > 30){
                System.out.println(input + " is illegal");
            }
            else if(last_check_month == 9 && last_check_day > 30){
                System.out.println(input + " is illegal");
            }
            else if(last_check_month == 11 && last_check_day > 30){
                System.out.println(input + " is illegal");
            }
            else if(last_check == 2000){
                //Check to make sure there are no other days that are zero too because you cant have two zeros
                if(last_check_day == 0 || last_check_month == 0){
                    System.out.println(input + " is illegal");
                }
                else{
                    System.out.print(year + "-" + month + "-" + day);
                }
            }
            else{
                System.out.print(year + "-" + month + "-" + day);
            }       
        }

    }
import java.io.*;
导入java.util.array;
公共类在{
静态布尔下一步;
静态字符串月、日、年;
公共静态void main(字符串[]args)引发IOException{
//设置以获取输入
InputStreamReader isr=新的InputStreamReader(System.in);
BufferedReader br=新的BufferedReader(isr);
字符串输入=”;
输入=br.readLine();
//拆分输入以获取用户输入的每个整数
字符串位=输入;
String[]标记=位。拆分(/“”);
int a_value1=Integer.parseInt(标记[0]);
int b_value1=Integer.parseInt(令牌[1]);
int c_value1=Integer.parseInt(标记[2]);
//按从低到高的顺序对数组排序
int[]int_数组=新的int[]{a_值1,b_值1,c_值1};
数组。排序(int_数组);
int a_值=int_数组[0];
int b_值=int_数组[1];
int c_value=int_数组[2];
年份=整数.toString(a_值);
月=整数.toString(b_值);
day=整数。toString(c_值);
//检查输入的整数,将其按正确顺序排列
int check_days=Integer.parseInt(天);
int check_month=Integer.parseInt(月);
int check_year=Integer.parseInt(年);
//检查以确保所有值都不是负整数
如果(检查天数<0){
System.out.println(输入+“是非法的”);
系统出口(0);
}
否则如果(检查月数<0){
System.out.println(输入+“是非法的”);
系统出口(0);
}
否则如果(检查年份<0){
System.out.println(输入+“是非法的”);
系统出口(0);
}
//仅当数组中的最高日期大于31时,才会更改周围的值
如果(检查天数>31){
//仅当年大于月时才重新组织
如果(检查月份>检查年份){
月份=整数。toString(检查年份);
}
//否则,只需将月份保持在当前值即可
否则{
月份=整数.toString(检查月份);
}
//更改日期和年份,因为一个比另一个大
年份=整数.toString(检查天数);
天=整数.toString(检查月);
}
否则如果(检查年份==0){
如果(检查月份<检查天数){
月份=整数.toString(检查月份);
}
否则{
月=整数.toString(检查天数);
}
}
//获取长度,以便我可以将数字归零
int length_year=year.length();
int length_month=month.length();
int length_day=day.length();
//在这里做我的零垫子
如果(长度=1年){
年份=“200”+年;
}
否则如果(长度=2年){
年份=“20”+年;
}
否则如果(长度=3年){
year=“2”+年份;
}
如果(长度\月份==1){
月份=“0”+月份;
}
如果(长度=1天){
day=“0”+天;
}
//最后一次检查以确保所有东西都符合犹太标准
int last_check=Integer.parseInt(年);
int last\u check\u month=Integer.parseInt(月);
int last_check_day=Integer.parseInt(day);
//检查是否是闰年。这一年可以被4整除吗?
如果(上次检查%4==0){
//一年可以被4整除,但不能被100整除吗?
如果(上次检查%100!=0){
next=真;
}
//一年可以被4、100和400整除吗?
否则如果(上次检查%400==0){
next=真;
}
//它可以被4和100整除,但不能被400整除!
否则{
下一个=假;
}
}
//它不能被4整除。
否则{
下一个=假;
}
//检查以确保日期合法有效:)
如果(上次检查>2999 | |上次检查<2000)
{
//日期必须介于2000和2999之间(含)
System.out.println(输入+“是非法的”);
}
否则如果(上个月检查>12)
{
//日期月份必须小于12
System.out.println(输入+“是非法的”);
}
否则如果(最后检查日>31)
{
//日期必须小于31天
System.out.println(输入+“非法”);
}
否则如果(下一次==false和上一次检查日>28和上一次检查月==2){
//如果next为假,且日期大于28,且不是闰年,则有问题
System.out.println(输入+“是非法的”);
}
else if(next==true&&last\u check\u day>29){
//如果next是真的,并且这一天大于29,并且是闰年,那么就有问题了
System.out.println(输入+“是非法的”);
}
如果(上个月检查)=4