-1
public enum Quarter
{
   FIRST,
   SECOND,
   THIRD,
   FOURTH,
}

  import java.util.*;



public class Quarterly
{

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

我想知道如果我在这里适当地声明了我的扫描仪类,我在教科书中看到了这一点,但我不确定它是否有效?还有另一种方法可以写这个吗?

    public Quarterly(Scanner scanner)
    {
        this._scanner = scanner;
    }

这个方法编译不正确有什么问题呢?它显示三个错误

该方法的标题显示编译错误“错误:找不到符号”

这两个错误都显示在我的所有四个案例陈述中

Error #1 : "an enum switch case label must be the unqualified name of the numeric constant.
Error #2: " Duplicate case label.

``

    private string GetMessage(Quarter quarter, int departmentNumber)// This shows an error that says " Error: Cannot Find Symbol"
    {
        // This case Statement is showing Two compiling errors that in every case just cannot find a way to fix them. 

        switch(quarter)
        {
            // Both These Errors are showing in all of my four case statement
            // Error #1 : "an enum switch case label must be the unqualified name of the numeric constant"
            // Error #2: " Duplicate case label.
            case Quarter.FIRST:
            return "Enter The First Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ ";
            case Quarter.SECOND:
            return "Enter a The Second Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ ";
            case Quarter.THIRD:
            return "Enter a The Third Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ ";
            case Quarter.FORTH:
            return "Enter a The Forth Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ ";

        }
    }

这是一种方法,它将把输入分配给我的数组并进行总结,任何人都可以查看这个方法,看看他们是否看到我编译它时可能出现的任何错误。

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

            for (int row = 0; row < ROW ; row++)
            {

                for (int col = 0; col < COL; col++)
                {

                    // This will input value into my array
                    sales[row][col] = _scanner.nextDouble();
                    num++;

                    // This will Total my array
                    for (int i = 0; i < sales.length; i++);
                    {
                        total += sales[row][col];
                    }
                }

                return total;
            }

    }
}

这将是我的主要内容。这是我从我的班级中调用我所有的方法。

public static void main(String[] args)
 {
     double test1;
     double test2;
     double test3;
     double test4;

 Scanner  keyboard = new Scanner(System.in);
 Quarterly helper = new Quarterly(keyboard);

 test1 = helper.GetTotalForDepartments(Quarter.FIRST);
 System.out.println(" Test 1 = " + test1);

 test2 = helper.GetTotalForDepartments(Quarter.SECOND);
 System.out.println(" Test 2 = " + test2);

 test3 = helper.GetTotalForDepartments(Quarter.THIRD);
 System.out.println(" Test 3 = " + test3);

 test4 = helper.GetTotalForDepartments(Quarter.FOURTH);
 System.out.println(" Test 4 = " + test4 " /n ");

}

4

3 回答 3

5

您不能在 switch 语句中写入类型,也不能。编译器已经知道您在谈论哪种类型。代替

case Quarter.FIRST:

case FIRST:
于 2013-11-11T03:56:24.593 回答
0

除了彼得已经说过的,

string不是有效的类类型,我认为您的意思是String.

您的GetMessage方法应该有一个 default case return。我是老派,相信所有方法的一个入口和一个出口点,这意味着,一个return声明。这使该方法更具可读性,因为您不会意外错过该隐藏return语句,因此我很想做类似...

private String GetMessage(Quarter quarter, int departmentNumber)// This shows an error that says " Error: Cannot Find Symbol"
{
    String result = null;

    switch (quarter) {
        case FIRST:
            result = "Enter The First Quarterly Sales For Department [ " + departmentNumber + " ]. . . . . $ ";
        case SECOND:
            result = "Enter a The Second Quarterly Sales For Department [ " + departmentNumber + " ]. . . . . $ ";
        case THIRD:
            result = "Enter a The Third Quarterly Sales For Department [ " + departmentNumber + " ]. . . . . $ ";
        case FORTH:
            result = "Enter a The Forth Quarterly Sales For Department [ " + departmentNumber + " ]. . . . . $ ";

    }
    return result;
}

同样,您在外循环GetTotalForDepartments中有一个语句。编译器无法判断此语句是否会被调用。相反,您应该考虑将它移到循环之外(因为我很确定您不希望它在它所在的位置),例如......returnforfor

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

    for (int row = 0; row < ROW; row++) {

        for (int col = 0; col < COL; col++) {

            // This will input value into my array
            sales[row][col] = _scanner.nextDouble();
            num++;

            // This will Total my array
            for (int i = 0; i < sales.length; i++);
            {
                total += sales[row][col];
            }
        }

    }
    return total;
}

您可能还想通读Java 编程语言的代码约定,因为按照约定,方法名称以小写字母开头;)

于 2013-11-11T04:02:01.430 回答
0

应该这样...

private String GetMessage(int quarter, int departmentNumber)
    {
        // This case Statement is showing Two compiling errors that in every
        // case just cannot find a way to fix them.
        String msg = null;
        switch (quarter) {
        // Both These Errors are showing in all of my four case statement
        // Error #1 :
        // "an enum switch case label must be the unqualified name of the numeric constant"
        // Error #2: " Duplicate case label.
        case FIRST:
            msg = "Enter The First Quarterly Sales For Department [ " + departmentNumber + " ]. . . . . $ ";
            break;
        case SECOND:
            msg = "Enter a The Second Quarterly Sales For Department [ " + departmentNumber + " ]. . . . . $ ";
            break;
        case THIRD:
            msg = "Enter a The Third Quarterly Sales For Department [ " + departmentNumber + " ]. . . . . $ ";
            break;
        case FORTH:
            msg = "Enter a The Forth Quarterly Sales For Department [ " + departmentNumber + " ]. . . . . $ ";
            break;
        default:
            msg = "no choice.";
        }
        return msg;
    }
于 2013-11-11T04:06:54.147 回答