public static void main(String[] args) {
choiceList();
switch (choice) {
case 1:
// do something
break;
case 2:
// do something
break;
case 0:
break;
}
public static int choiceList() {
Scanner jauns = new Scanner(System.in);
System.out.println("1. Show the seats");
System.out.println("2. Buy a ticket");
System.out.println("0. Exit");
int choice = jauns.nextInt();
return choice;
}
}
The thing is that the method choiceList() dont return int choice. IDE shows - java: cannot find symbol If i declare this method in /public static void main/ method it shows - void cannot return, but it is java main method and cannot be changed. So how can i return int value from method in main method?
This is a bit confusing, tried to look up everywhere... Thats not all code, but the part that doesn't work.. Thanks in advance!
CodePudding user response:
Right now you're ignoring the return value of choiceList
. You could save it to a variable in order to use it:
public static void main(String[] args) {
int choice = choiceList();
switch (choice) {
// decide what to do based on the value of choice
CodePudding user response:
You have also a mismatch in your brackets as your choiceList
method seems part of the main
method. Please see my following correction of your code which also includes the other's suggestions that you should store the return value of your choiceList
method in a new variable which you would evalute in your switch:
public static void main(String[] args) {
int choice = choiceList();
switch (choice) {
case 1:
// do something
break;
case 2:
// do something
break;
case 0:
break;
}
} // end the main method
public static int choiceList() {
Scanner jauns = new Scanner(System.in);
System.out.println("1. Show the seats");
System.out.println("2. Buy a ticket");
System.out.println("0. Exit");
int choice = jauns.nextInt();
return choice;
}
CodePudding user response:
The problem is that value returned by choiceList method is not assigned to variable. You can try smth like this:
public static void main(String[] args) {
int choice = choiceList();
switch (choice) {
case 1:
// do something
break;
case 2:
// do something
break;
case 0:
break;
}
}
public static int choiceList() {
Scanner jauns = new Scanner(System.in);
System.out.println("1. Show the seats");
System.out.println("2. Buy a ticket");
System.out.println("0. Exit");
int choice = jauns.nextInt();
return choice;
}