简体   繁体   English

方法不从方法中的扫描仪返回 int 值

[英]Method don't return int value from scanner in method

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.问题是方法 choiceList() 不返回 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. IDE 显示 - java: 找不到符号如果我在 /public static void main/ 方法中声明此方法,它会显示 - void 无法返回,但它是 java main 方法,无法更改。 So how can i return int value from method in main method?那么如何从 main 方法中的方法返回 int 值呢?

This is a bit confusing, tried to look up everywhere... Thats not all code, but the part that doesn't work.. Thanks in advance!这有点令人困惑,试图到处查找......这不是所有代码,而是不起作用的部分......提前致谢!

Right now you're ignoring the return value of choiceList .现在您忽略了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

You have also a mismatch in your brackets as your choiceList method seems part of the main method.您的括号中也有不匹配,因为您的choiceList方法似乎是main方法的一部分。 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:请参阅我对您的代码的以下更正,其中还包括其他人的建议,即您应该将choiceList方法的返回值存储在一个新变量中,您将在 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;
}

The problem is that value returned by choiceList method is not assigned to variable.问题是 choiceList 方法返回的值没有分配给变量。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM