我无法从字符串数组中检索值。
我需要让它输出星期几的名称..即星期日的数字0.我有完美的输出数字的代码,但我不能让它从我的一天中拉出来名。
import java.util.Scanner;
public class FutureDateJava {
String[] dayStrings = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday" };
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter today's day: ");
int today = input.nextInt();
System.out.println("Enter a day in the future ");
int future = input.nextInt();
int futureDay = (today + future) % 7;
System.out.print("Today is " + today +
" and the future day is " + futureDay);
}
}
答案 0 :(得分:3)
实际访问数组:
System.out.print("Today is " + dayStrings [today % 7] + " and the future day is " + dayStrings [futureDay]);
%7
是为了确保一天(例如10)不会导致越界异常。然而,这并不是最佳的,它假设星期一是第0天,但这个想法就在那里。