Java 基于,我找不到我的代码哪里出错了
Java based, i cant find where i am going wrong in my code
是的,这是学校的作业!所以没有答案请只是一个工作方向:)
此代码应该获取星期几、设置日期并打印日期。它还需要将一天增加一天并在第二天增加return,将给定的一天减少一天并在前一天减少return,然后将一天增加用户给定的数量和return 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.
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()
您需要为每个方法声明一个 return 类型。如果它没有 return 任何东西,请使用 void
:public void day()
。
一旦您习惯了错误消息,它们确实可以解释问题:
第一个和第四个错误是因为你在方法内部声明了一些静态的东西,这没有意义。只需删除 static
关键字。
第二个和第三个是因为您的方法没有 return 类型 - 它必须是 public void foo()
、public String foo()
或类似的类型。
是的,这是学校的作业!所以没有答案请只是一个工作方向:)
此代码应该获取星期几、设置日期并打印日期。它还需要将一天增加一天并在第二天增加return,将给定的一天减少一天并在前一天减少return,然后将一天增加用户给定的数量和return 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.
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()
您需要为每个方法声明一个 return 类型。如果它没有 return 任何东西,请使用 void
:public void day()
。
一旦您习惯了错误消息,它们确实可以解释问题:
第一个和第四个错误是因为你在方法内部声明了一些静态的东西,这没有意义。只需删除 static
关键字。
第二个和第三个是因为您的方法没有 return 类型 - 它必须是 public void foo()
、public String foo()
或类似的类型。