我已经编写了一个问题的代码,但它有很多if else循环。任何人都可以帮助我摆脱一些if else条件。谢谢 [英] i have written code for one problem but it have lots of if else loop. can anybody help me to get the rid of some if else condition. thank you

查看:57
本文介绍了我已经编写了一个问题的代码,但它有很多if else循环。任何人都可以帮助我摆脱一些if else条件。谢谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:输入3个数字作为输入(可以是1位或2位数字)。写一个程序,找到组合这3个数字可以形成的最小日期。这应该验证日历的所有逻辑约束(例如,我们不能在2月30日)





我的代码是:





problem is : Take 3 numbers as input (can be 1 or 2 digit numbers).Write a program to find the minimum date which can be formed by combining these 3 digits. This should validate for ALL logical constraints of a calendar (e.g. we cannot have 30th Feb)


my code is :


public class ThreeNum {
    public static void main(String[] args) {
        int d = 0, m = 0, y = 0;
        int num[] = new int[3];

        input = /* accept input from user */

        for(int i = 0; i < 3; i++) {
            System.out.print("Enter Number: ");
            num[i] = input.nextInt();
        }

  sort num  /* use of any sorting function to sorting array. */

        if (num[0] > 12) {
            System.out.print("Date is invalid. Try again");
            System.exit(0);
        } else if (num[2] <= 12) {
            y = num[0]; m = num[1]; d = num[2];
        } else if (num[2] <= 31) {
            if (num[1] <= 12) {
                y = num[0]; m = num[1]; d = num[2];
            } else {
                m = num[0]; y = num[1]; d = num[2];
            }
        } else {
            if (num[1] <= 31) {
                y = num[2]; d = num[1]; m = num[0];
            } else {
                System.out.print("Date is invalid. Try again");
                System.exit(0);
            }
        }

        if (m == 4 || m == 6 || m == 9 || m == 11) {
            if (d <= 30) {
                System.out.print("Corrent Date is " + d + "/" + m + "/" + y);
                System.exit(0);
            } else if (y <= 12) {
                if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)

{
                    m = m + y; y = m - y; m = m - y;
                    System.out.print("Corrent Date is " + d + "/" + m + "/" + y);
                    System.exit(0);
                }
            } else {
                d = d + y; y = d - y; d = d - y;
                System.out.print("Corrent Date is " + d + "/" + m + "/" + y);
                System.exit(0);
            }
        } else if (m == 2) {
            if (d <= 29) {
                if((y % 4) == 0) {
                    System.out.print("Corrent Date is " + d + "/" + m + "/" + y);
                    System.exit(0);
                } else if (y <= 28) {
                    d = d + y; y = d - y; d = d - y;
                    System.out.print("Corrent Date is " + d + "/" + m + "/" + y);
                    System.exit(0);
                } else {
                    System.out.print("Date is invalid. Try again");
                    System.exit(0);
                }
            } else if (d <= 31 & y <= 28) {
                d = d + y; y = d - y; d = d - y;
                System.out.print("Corrent Date is " + d + "/" + m + "/" + y);
                System.exit(0);
            } else {
                System.out.print("Date is invalid. Try again");
                System.exit(0);
            }
        } else {
            System.out.print("Corrent Date is " + d + "/" + m + "/" + y);
            System.exit(0);
        }
    }
}

推荐答案

这不仅仅是你的if..else丰富的问题 - 假设排序数字会给你正确的订单,你犯了一个根本性的错误。



例如,如果我下周给你的代码约会:4 2013年9月,您的系统会将其排序为

It's not just your if..else profusion that's the problem - you have made a fundamental mistake in assuming that sorting numbers will give you the right order.

For example, if I give your code a date next week: 4th Sept 2013, your system will sort it as
13
9
4

这将使您的首次测试失败,因为你假设最高的数字是月份 - 为什么?



我这样做的方式有点偷偷摸摸,可能不是你的导师想要的,但是...

我根本不会对数字进行排序。相反,我会将它们传递给一个方法,该方法生成独特的组合(只有六个,所以它很容易)作为一个字符串:

which will fail your first test, because you are assuming that the highest number is the month - why?

The way I would do it is a little sneaky, and probably not what your tutor wants, but...
I wouldn't sort the numbers at all. Instead, I would pass them to a method which generated the unique combinations (there are only six of them, so it's pretty easy) as a string:

static string[] GetCombination(int[] list)
    {
    string[] op = new string[6];
    int opIndex = 0;
    for (int i = 0; i < 3; i++)
        {
        for (int j = 0; j < 3; j++)
            {
            if (i != j)
                {
                for (int k = 0; k < 3; k++)
                    {
                    if (k != i && k != j)
                        {
                        op[opIndex++] = string.Format("{0:00}{1:00}{2:00}", list[i], list[j], list[k]);
                        }
                    }
                }
            }
        }
    return op;
    }

然后,我会使用一个循环使用DateTime.TryParseExact将每个转换为DateTime。一个简单的日期类型为您提供最低的有效日期,如果有的话。

Then, I would use a loop to convert each of these to a DateTime using DateTime.TryParseExact. A simple sort of the dates gives you the lowest valid date, if there are any.


这篇关于我已经编写了一个问题的代码,但它有很多if else循环。任何人都可以帮助我摆脱一些if else条件。谢谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆