【发布时间】:2020-06-23 15:30:04
【问题描述】:
我目前正在参加培训课程,这给我带来了很多问题。 代码如下。 我遇到的问题是培训课程需要基于 java.time.LocalDate 导入的特定解决方案。
我已经尝试了多种不同的方法来解决这个问题,但是内置编译器一直在爆炸。 这是我第一次尝试学习 Java,但绝对没有帮助。 需要在指定的3点添加代码。
感觉好像没必要在这方面寻求帮助,但我在这里看不到任何解决方案。
import java.time.LocalDate;
public class ChallengeThree {
public static String dayOfWeek(String date) {
/**
* Returns a String storing the day of the week in all capital letters of the
* given date String
* Complete the implementation of the DateUtil class and use it in this function
* Arguments
* date - a String storing a local date, such as "2000-01-01"
* Examples
* dayOfWeek("2000-01-01") returns "SATURDAY"
*/
// ====================================
// Do not change the code before this
// CODE1: Write code to return the day of the week of the String date
// using the DateUtil class at the bottom of this file
// ====================================
// Do not change the code after this
}
public static void main(String[] args) {
String theDayOfWeek = dayOfWeek("2000-01-01");
String expected = "SATURDAY";
// Expected output is
// true
System.out.println(theDayOfWeek == expected);
}
}
class DateUtil {
LocalDate theDate;
public DateUtil(String date) {
/**
* Initialize the theDate field using the String date argument
* Arguments
* date - a String storing a local date, such as "2000-01-01"
*/
// ====================================
// Do not change the code before this
// CODE2: Write code to initialize the date field of the class
// ====================================
// Do not change the code after this
}
public String dayOfWeek() {
/**
* Return a String the day of the week represented by theDate
*/
// ====================================
// Do not change the code before this
// CODE3: Write code to return the String day of the week of theDate
// ====================================
// Do not change the code after this
}
}
【问题讨论】:
-
请发布代码而不是屏幕截图:如果人们需要测试或调试您的代码,复制/粘贴它比从屏幕截图转录更容易。
-
我无法正确格式化代码以进行发布
-
您的问题是您尝试定义
String[] date,但date已定义为String(来自方法的参数)。只需使用不同的变量名称,例如dateParts(并明显重命名其他引用) -
如果要保留格式,请在代码前后使用```。请参阅help。
-
好吧,那么我认为你应该通过将 Arvind 的答案分成两部分来调整它,以便尊重你的模板:在 DateUtil 的构造函数中将 LocalDate.parse(date) 分配给
theDate,并且让getDayOfWeek在theDate上拨打getDayOfWeek。