你能检查一下是否正确 [英] Could you check if right

查看:54
本文介绍了你能检查一下是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是作业:
编写一个执行以下操作的程序:

声明一组七个名为MONDAY,TUESDAY,...,SUNDAY的整数常量.将MONDAY设置为1,将TUESDAY设置为2,依此类推.然后,创建一个名为day_of_week的整数变量,并使用某个整数值对其进行初始化.如果day_of_week的值为1,则程序应显示Today is Monday.如果值为7,则程序应显示Today is Sunday,依此类推.如果该值不在1-7范围内,则程序应显示消息无效数据".使用开关构造和定义的常量.

这是我能检查的吗?

THis is the assignment:
Write a program that does the following:

Declare a set of seven integer constants named MONDAY, TUESDAY, ... , SUNDAY. Set MONDAY to 1, TUESDAY to 2, and so forth. Then, create an integer variable named day_of_week and initialize it with some integer value. If the value of day_of_week is 1, the program should display Today is Monday. If the value is 7, the program should display Today is Sunday, and so forth. If the value is not in the range 1-7, the program should display the message Invalid Data. Use a switch construct and the defined constants.

This is what I did could you check?

public class DaysOfWeek

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

        int Monday=1;
        int tuesday=2;
        int wednesday=3;
        int thursday=4;
        int friday=5;
        int saturday=6;
        int sunday=7;
        int day_of_week = 1;


         switch(day_of_week)

         {
          case 1:
            System.out.println("today is monday");
            break;

          case 2:
            System.out.println("today is tuesday");
            break;

          case 3:
            System.out.println("today is wednesday");
            break;

          case 4:
            System.out.println("today is thursday");
            break;

          case 5:
            System.out.println("today is friday");
            break;

          case 6:
            System.out.println("today is saturday");
            break;

          case 7:
            System.out.println("today is sunday");
            break;

          default:
            System.out.println("Invalid Data");
            break;
         }
    }
}

推荐答案

好吧,如果必须定义一个常量,则应使用"final"关键字或"enum",然后使用google找出答案使用int扩展了关于它们的信息,这使它成为一个变量",该变量不是恒定的.仅当您的老师挑剔时,这才是一个问题.

除此之外,代码看起来还不错.

您绝对应该使用调试器对其进行编译和执行,以查看其行为.

;)
Well, if you have to define a constant you should use the "final" keyword or an "enum", use google to find out extended information on both of them, using an int makes it a "variable" which is not constant. This will be a problem only if your teacher is picky...

Apart of that the code looks good.

You should definitely compile and execute it using the debugger to see how it behaves.

;)


声明一个常量值然后不使用它有什么意义?您的case语句应使用星期几,而不是整数值,例如:
What is the point of declaring a constant value and then not using it? Your case statements should use the days of the week, not the integer values, something like:
        switch(day_of_week)
        {
         case Monday:
           System.out.println("today is monday");
           break;
...


您可能还想将所有的日期名称和所用的消息大写.


You may also like to capitalise all the day names and the messages you are using.


public class Test
{
    public static void main(String[] args)
    {
        String[] day_names = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
        int day = 1;
        System.out.println("Today is " + day_names[day-1]);
    }
}


如果您使用的是Java 1.5或更高版本,则应使用一个枚举.
这是一个更好的解决方案:


If you are using java 1.5 or later then you should utilize an enum.
This is a much better solution:

public class Test
{
    enum DayOfWeek
    {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday,
    }

    public static void main(String[] args)
    {
        int day = 1;
        System.out.println("Today is " + DayOfWeek.values()[day-1]);
        // or if you can omit the usage of an int type then you can say:
        //DayOfWeek day = DayOfWeek.Monday;
        //System.out.println("Today is " + day);
    }
}




OR

public class Test
{
    enum DayOfWeek
    {
        Monday(1),
        Tuesday(2),
        Wednesday(3),
        Thursday(4),
        Friday(5),
        Saturday(6),
        Sunday(7);

        public static DayOfWeek fromInt(int i)
        {
            assert i>=1 && i<=7;
            return values()[i-1];
        }

        private DayOfWeek(int customValue)
        {
            _customValue = customValue;
        }

        public int getCustomValue()
        {
            return _customValue;
        }

        // You can store any kind of custom info for each enum member, not only an integer.
        // For example some kind of textual description, or fancy name, or references to data objects.
        private int _customValue;
    }

    public static void main(String[] args)
    {
        int day = DayOfWeek.Monday.getCustomValue();
        System.out.println("Today is " + DayOfWeek.fromInt(day));

        /*
        // This is much more elegant if you don't have to use an int type:
        DayOfWeek day = DayOfWeek.Monday;
        System.out.println("Today is " + day);
        */
    }
}


这篇关于你能检查一下是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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