是的,这是学校的作业!所以没有答案,请只是一个工作方向:)
此代码应该获取星期几、设置日期并打印日期。它还需要将日期增加 1 并返回第二天,将给定日期减少 1 并返回前一天,然后将日期增加用户给定的数量并返回 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.
最佳答案
一旦您习惯了错误消息,它们确实可以解释问题:
第一个和第四个错误是因为您在方法内声明了一些静态内容,这是没有意义的。只需删除 static
关键字即可。
第二个和第三个是因为你的方法没有返回类型 - 它必须是 public void foo()
、public String foo()
或其他类型像这样。
关于基于Java,我找不到代码中出错的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731921/