是的,这是学校的作业!所以没有答案请只是一个工作方向:)
此代码应该是获取星期几,设置日期和打印日期。此外,它需要将一天增加一个并在第二天返回,将给定的一天减少一个并返回前一天,然后按用户给定的数量增加一天,并返回x天的天数。
import java.util.Scanner;
public class MyDay
{
private int tempDays;
private String day;
public String toString()
{
return (day);
}
//Creates the method setDay, Uses switch statements to determine the day.
public void setDay(int tempDay)
{
String day;
switch (tempDay){
case 1: day= "Sun";
break;
case 2: day= "Mon";
break;
case 3: day= "Tues";
break;
case 4: day= "Wednes";
break;
case 5: day= "Thurs";
break;
case 6: day= "Fri";
break;
case 7: day= "Sat";
break;
}
return (day);
}
//Returns the day as a string.
public Day setName(String day)
{
day = tempDay;
return day;
}
//Creates the method printDay, returns the string day.
public void printDay()
{
return (day);
}
//Creates the method nextDay, Determines the next day.
public void nextDay()
{
nextDay=(tempDay+1)%7;
tempDay(nextDay);
printDay();
}
//Creates the method lastDay, Determines the previous day.
public void lastDay()
{
lastDay=(tempDay-1)%7;
tempDay(lastDay);
printDay();
}
//Creates the method getDay, Determines the next day it will be based on the users input.
public void getDay()
{
static Scanner console= new Scanner(System.in);
int x=0;
int y=0;
System.out.println("The next number you will be prompted to enter will return\n"+
"what day it will be in that many days.");
System.out.println("Enter a number using numeric keys only.");
x= console.nextInt();
y = day +(x);
Day = y %7;
tempDay(getDay);
printDay();
}
//Allows Day to be minpulated.
public day()
{
tempDay(0);
}
//converts Day into a string.
public day (int tempDay)
{
tempDay(day);
}
public static void main(String[] args)
{
//Import Scanner as imput device.
static Scanner console= new Scanner(System.in);
System.out.println("Please enter the day you wish to set.");
System.out.println("Enter 1 for Sunday\n"+"Enter 2 for Monday\n"+"Enter 3 for Tuesday\n"+
"Enter 4 for Wednesday\n"+"Enter 5 for Thursday\n"+"Enter 6 for Friday\n"+
"Enter 7 for Saturday");
tempDay= console.nextInt();
Day myDay = new Day(tempDay);
System.out.println();
System.out.println("Today is: ");
myDay.printDay();
System.out.println("Tomorrow is: ");
myDay.nextDay();
System.out.println("Yesterday was: ");
myDay.lastDay();
System.out.println("In "+ x +"day(s), it will be: ");
myDay.getDay();
}
}
我收到的错误消息是:
----jGRASP exec: javac -g MyDay.java
MyDay.java:69: error: illegal start of expression
static Scanner console= new Scanner(System.in);
^
MyDay.java:83: error: invalid method declaration; return type required
public day()
^
MyDay.java:89: error: invalid method declaration; return type required
public day (int tempDay)
^
MyDay.java:98: error: illegal start of expression
static Scanner console= new Scanner(System.in);
^
4 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
答案 0 :(得分:1)
MyDay.java:69: error: illegal start of expression
static Scanner console= new Scanner(System.in);
在Java中,局部变量不能是静态的。只需删除static
。
MyDay.java:83: error: invalid method declaration; return type required
public day()
您需要为每个方法声明一个返回类型。如果它没有返回任何内容,请使用void
:public void day()
。
答案 1 :(得分:1)
一旦您习惯了这些问题,错误信息就可以解释这些问题:
第一个和第四个错误是因为你在方法中声明了一些静态的东西,这是没有意义的。只需删除static
关键字。
第二个和第三个是因为您的方法没有返回类型 - 它必须是public void foo()
,public String foo()
或类似的东西。