0

/* 我想创建一个二维数组,它将接受 6 个部门的 4 个季度的输入。例如 - “输入第一部门的第一季度销售额”。我写了这段代码,但是我无法让我的案例语句在我的“GetMessage”方法中工作。*/

import java.util.*;
public class EnumQuarter
  {
     static final int quarter;
     static final int First;
     static final int Second;
     static final int Third;
     static final int Forth;


     public class Quarterly

    {



        private final int ROW = 6;
        private final int COL = 4;
        private Scanner _scanner;


        Scanner  keyboard = new Scanner(System.in);

    public Quarterly(Scanner scanner)
        {
            Scanner  keyboard = new Scanner(System.in);
            this._scanner = scanner;
        }



/* I am getting a compiling error here, it says that a constant expression is required
for every case statement for example " case EnumQuarter.First:" This gives me that 
compiling error.  Am I missing some syntax? or am I doing something completely wrong here . */




    private String GetMessage(int quarter, int departmentNumber)
        {
            switch(quarter)
            {
                case EnumQuarter.First:
                return "Enter The First Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ ";
                case EnumQuarter.Second:
                return "Enter a The Second Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ ";
                case EnumQuarter.Third:
                return "Enter a The Third Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ ";
                case EnumQuarter.Forth:
                return "Enter a The Forth Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ ";

            }


        }



/*  I have not gotten to run this class yet but since I am new to java, I wonder if I
am creating my 2-D array properly.  Please take a look at it and comment me giving me 
advise on weather I am doing it properly or if there is a better way of doing it */



    public double GetTotalForDepartments(EnumQuarter quarter)
        {
            double [][] sales = new double [ROW][COL];
            int num = 1;
            double total = 0;

                for (int row = 0; row < ROW ; row++)
                    {
                        // This for statement is taking the column from the array in order to fill with input
                        for (int col = 0; col < COL; col++)
                            {   
                                // This will receive sale numbers from the six departments
                                System.out.print(message + "[" + num + " ]. . . . . $ ");
                                sales[row][col] = scanner.nextDouble();
                                num++;
                                // This for statement will sum each department's input
                                for (int i = 0; i < sales.length; i++);
                                total += sales[row][col];
                            }
                    }

                            return total;
        }
}   
}
4

1 回答 1

0

你没有给你的常量赋值。您需要先为常量赋值,然后才能使用它们:

 static final int First = 0;
 static final int Second = 1;
 static final int Third = 2;
 static final int Forth = 3;
 static int quarter = First;

也许你想做的是创建一个enum

static enum Quarter { First, Second, Third, Forth }
static Quarter quarter = Quarter.First;

在这种情况下,四分之一由 类型的对象表示Quarter,而不是由int

于 2013-11-07T23:07:57.590 回答