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 ");
}
答案 0 :(得分:5)
您不能在switch语句中写入类型,也不能。编译器已经知道你在说什么类型。取代
case Quarter.FIRST:
与
case FIRST:
答案 1 :(得分:0)
除了彼得已经说过的话,
string
不是有效的类类型,我认为您的意思是String
。
您的GetMessage
方法应该有默认案例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
在外部return
循环中有一个for
语句。编译器无法判断是否将调用此语句。相反,你应该考虑将它移到for
循环之外(因为我很确定你不希望它在那里),例如......
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;
}
您可能还想阅读Code Conventions for the Java Programming Language,因为按照惯例,方法名称以小写字母开头;)
答案 2 :(得分: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;
}