我正在和我的内心课DateTest
搏斗。编译后出现此错误消息,我无法弄清楚如何修复该错误。我已经通过多行代码来计算测试类,试图打印出Date类中所有12个switch块的情况。
下面我已经包含了错误和我的代码。另外值得一提的是,我正在使用CodeChef中的在线IDE,browxy和双重检查。
这是我的代码:
public class Date {
public static int daysInMonth(int month) {
/** method uses a switch block and takes in a month number (1 for January, 2 for February, etc.) and returns the number of days in a month. **/
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
break;
case 2:
return 28;
break;
case 4:
case 6:
case 9:
case 11:
return 30;
break;
default:
System.out.println("Invalid month.");
break;
}
return month;
}
public class DateTest {
/*test class that prints out 12 lines like: "January is 31 days long.", "February...", etc.*/
int month; /* <-- compilation error had been here due to missing curly brace (error = "class expected") */
}
public static void main (String [] args) {
month = Date.daysInMonth(1);
System.out.println("January is " +month+ " days long.");
month = Date.daysInMonth(2);
System.out.println("February is " +month+ " days long.");
month = Date.daysInMonth(3);
System.out.println("March is " +month+ " days long.");
month = Date.daysInMonth(4);
System.out.println("April is " +month+ " days long.");
month = Date.daysInMonth(5);
System.out.println("May is " +month+ " days long.");
month = Date.daysInMonth(6);
System.out.println("June is " +month+ " days long.");
month = Date.daysInMonth(7);
System.out.println("July is " +month+ " days long.");
month = Date.daysInMonth(8);
System.out.println("August is " +month+ " days long.");
month = Date.daysInMonth(9);
System.out.println("September is " +month+ " days long.");
month = Date.daysInMonth(10);
System.out.println("October is " +month+ " days long.");
month = Date.daysInMonth(11);
System.out.println("November is " +month+ " days long.");
month = Date.daysInMonth(12);
System.out.println("December is " +month+ " days long.");
}
}
(编辑:这是我在这里提出的第一个问题,刚开始学习Java以及如何完全自己编程 - 我建议任何人开始使用:使用足够体面的计算机来支持基本IDE语法高亮,而不是在线环境/没有这个,因为Java / C ++标点符号可能会让你在不熟悉的时候做恶梦。)
答案 0 :(得分:1)
month
应该只是main
函数中的本地成员。只需在第一次使用时添加类型声明,您应该没问题:
public class DateTest {
public static void main (String [] args) {
int month = Date.daysInMonth(1);
// rest of code comes here
}
}
答案 1 :(得分:0)
不允许内部类具有静态方法。只允许使用静态类和Toplevel类
答案 2 :(得分:0)
就这样做:
import java.util.Scanner;
public class Date
{
public static boolean isLeapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
return true;
}
else
{
return false;
}
}
public static int daysInMonth(int month, boolean isLeapYear)
{
/**
* method uses a switch block and takes in a month number (1 for
* January, 2 for February, etc.) and returns the number of days in a
* month.
**/
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if (isLeapYear)
{
return 29;
}
else
{
return 28;
}
case 4:
case 6:
case 9:
case 11:
return 30;
default:
System.out.println("Invalid month.");
break;
}
return month;
}
/*
* test class that prints out 12 lines like: "January is 31 days long.",
* "February...", etc.
*/
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int year = in.nextInt();
boolean isLeapYear = isLeapYear(year);
int month = 0;
month = Date.daysInMonth(1, isLeapYear);
System.out.println("January is " + month + " days long.");
month = Date.daysInMonth(2, isLeapYear);
System.out.println("February is " + month + " days long.");
month = Date.daysInMonth(3, isLeapYear);
System.out.println("March is " + month + " days long.");
month = Date.daysInMonth(4, isLeapYear);
System.out.println("April is " + month + " days long.");
month = Date.daysInMonth(5, isLeapYear);
System.out.println("May is " + month + " days long.");
month = Date.daysInMonth(6, isLeapYear);
System.out.println("June is " + month + " days long.");
month = Date.daysInMonth(7, isLeapYear);
System.out.println("July is " + month + " days long.");
month = Date.daysInMonth(8, isLeapYear);
System.out.println("August is " + month + " days long.");
month = Date.daysInMonth(9, isLeapYear);
System.out.println("September is " + month + " days long.");
month = Date.daysInMonth(10, isLeapYear);
System.out.println("October is " + month + " days long.");
month = Date.daysInMonth(11, isLeapYear);
System.out.println("November is " + month + " days long.");
month = Date.daysInMonth(12, isLeapYear);
System.out.println("December is " + month + " days long.");
}
}