我正在制作一个带有菜单的程序,并且我正在使用开关在菜单之间导航。Java开关(使用在另一种情况下计算的情况下的值)
我有这样的事情:
switch (pick) {
case 1:
// Here the program ask the user to input some data related with students (lets say
// name and dob). Student is a class and the students data is stored in 1 array of
// students. If I do:
// for (Student item: students){
// if (item != null){
// System.out.println(item);
// }
// }
// It will print the name and dob of all the students inserted because I've created
// a toString() method that returns the name and dob of the students
case 2:
// On case 2 at some point I will need to print the array created on the case
// above. If I do again:
// for (Student item: students){
// if (item != null){
// System.out.println(item);
// }
// }
// It says that students variable might have not been initialized.
问:
如果一个变量在一个情况下创建它的价值不能在另一种情况下使用? 我试图做的是首先进入情况1并输入值,然后,在情况2中能够使用在情况1中定义的一些值。
如果这不能完成,请点我在正确的方向。
请记住,我已经开始只学几个礼拜了。
favolas
你说“问题一:”,那里还有一个“问题二”吗? –
@java熔岩我的不好。对不起 – Favolas