gpt4 book ai didi

java - token “Invalid Character” 出现语法错误,删除此 token

转载 作者:行者123 更新时间:2023-12-02 04:15:33 25 4
gpt4 key购买 nike

我的 Eclipse 有问题,我使用的是 MAC 操作系统。

我不知道为什么它显示错误:“ token “无效字符”上的语法错误,删除此 token ”。谁能告诉我为什么?我在某处进行了谷歌搜索,但找不到如何解决这个问题。

enter image description here

这是我的代码:

package _Java_tuan1;

import java.util.Scanner;

public class Date {
private String month;
private int day, year;

public Date(){
month = "January";
day = 1;
year = 1000;
}

public Date(int monthInt, int day, int year){
setDate(monthInt, day, year);
}

public Date(String monthString, int day, int year){
setDate(monthString, day, year);
}

public Date(int year){
setDate(1, 1, year);
}

public Date(Date aDate){
if (aDate == null){
System.out.println("Fatal Error.");
System.exit(0);
}
month = aDate.month;
day = aDate.day;
year = aDate.year;
}

public void setDate(int monthInt, int day, int year){
if(dateOK(monthInt, day, year)){
this.month = monthString(monthInt);
this.day = day;
this.year = year;
}else{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(String monthString, int day, int year){
if(dateOK(monthString, day, year)){
this.month = monthString;
this.day = day;
this.year = year;
}
else{
System.out.println("Fatal Error");
System.exit(0);
}
}

public void setDate(int year){
setDate(1, 1, year);
}

public void setYear(int year){
if( (year < 1000) || (year > 9999)){
System.out.println("Fatal Error");
System.exit(0);
}
else{
this.year = year;
}
}

public void setMonth(int monthNumber){
if((monthNumber <= 0) || (monthNumber > 12)){
System.out.println("Fatal Error");
System.exit(0);
}
else{
month = monthString(monthNumber);
}
}

public void setDay(int day){
if((day <= 0) || (day > 31)){
System.out.println("Fatal Error");
System.exit(0);
}
else{
this.day = day;
}
}

public int getMonth(){
if(month.equals("January")){
return 1;
}
else if(month.equals("February")){
return 2;
}
else if(month.equals("March")){
return 3;
}
else if(month.equals("April")){
return 4;
}
else if(month.equals("May")){
return 5;
}
else if(month.equals("June")){
return 6;
}
else if(month.equals("July")){
return 7;
}
else if(month.equals("August")){
return 8;
}
else if(month.equals("Septemper")){
return 9;
}
else if(month.equals("October")){
return 10;
}
else if(month.equals("November")){
return 11;
}
else if(month.equals("December")){
return 12;
}
else{
System.out.println("Fatal Error");
System.exit(0);
return 0;
}
}

public int getDay(){
return day;
}

public int getYear(){
return year;
}

public String toString(){
return (month +" "+day+", "+year);
}

public boolean equals(Date otherDate){
 return ((month.equals(otherDate.month)) && (day == otherDate.day) && (year == otherDate.year));
}

public boolean precedes(Date otherDay){
return ( (year < otherDay.year) || (year == otherDay.year) && getMonth() < otherDay.getMonth() ||
(year == otherDay.year && month.equals(otherDay.month) && day < otherDay.day));
}

public void readInput(){
boolean tryAgain = true;
Scanner keyboard = new Scanner(System.in);
while(tryAgain){
System.out.println("Enter month, day, and year.");
System.out.println("Do not use a comma.");
String monthInput = keyboard.next();
int dayInput = keyboard.nextInt();
int yearInput = keyboard.nextInt();
if (dateOK(monthInput, dayInput, yearInput) ){
setDate(monthInput, dayInput, yearInput);
tryAgain = false;
}
else
System.out.println("Illegal date. Reenter input.");
}
}

private boolean dateOK(int monthInt, int dayInt, int yearInt){
return ( (monthInt >= 1) && (monthInt <= 12) && (dayInt >= 1) && (dayInt <= 31) &&
(yearInt >= 1000) && (yearInt <= 9999) );
}

private boolean dateOK(String monthString, int dayInt, int yearInt){
return (monthOK(monthString) && (dayInt >= 1) && (dayInt <= 31) && (yearInt >= 1000) && (yearInt <= 9999));
}

private boolean monthOK(String month){
return (month.equals("January")
|| month.equals("February")
|| month.equals("March")
|| month.equals("April")
|| month.equals("May")
|| month.equals("May")
|| month.equals("May")
|| month.equals("June")
|| month.equals("July")
|| month.equals("August")
|| month.equals("Septemper")
|| month.equals("Octobor")
|| month.equals("November")
|| month.equals("December"));
}

private String monthString(int monthNumber){
switch(monthNumber){
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "Septemper";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default:
System.out.println("Fatal Error");
System.exit(0);
return "Error";
}
}

}

最佳答案

您是否粘贴了 Windows 中的代码?删除最后几个字符并重新输入它们 - 我认为它会消失

关于java - token “Invalid Character” 出现语法错误,删除此 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com