0
Arrays
/*I don't quite understand array. This is my first time seeing them in my life, can you please explain to me what is the error here?*/ Your calendar program should output all the days of week, but it has errors. Change the code so that the program prints the days. public class Main { public static void main(String[] args) { int[] days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; for (int i = 0; i < 7; i++) { System.out.println(days[i]); } } }
7 ответов
+ 1
You have to declare arrays by curly braces int[] days = { .. }; //replace with [ ]
edit :
Array days should be string type as well.
+ 1
Oh thanks guys, i’ve tried before replacing the curly braces, but didnt think about the array name.
thanks a lot!
+ 1
public class Main {
public static void main(String[] args) {
String[] days = {"Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
for (int i = 0; i < 7; i++) {
System.out.println(days[i]);
}
}
}
+ 1
public class Main {
public static void main(String[] args) {
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
for (int i = 0; i < 7; i++) {
System.out.println(days[i]);
}
}
}
Good Luck
0
As Jayakrishna🇮🇳 writes, you should use curly braces. What's more, there are strings in the array, not numbers. Therefore, you should change the data type from int to String:
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
0
what does the for loop do. I'm doing the same problem why is there and why are we printing it.
0
The loop is there to iterate through the days of the week. Remember for loops determine how many times a specific piece of code will run. Thus:
for (x = 0; x < 7; x++) {
System.out.println(days[2]);
}
will print Wednesday seven times. That should point you in the right direction.