Java : Int 变量总是在使用扫描器退出 try-catch 块时恢复为旧值

Java : Int variable always reverts to old value when exiting try-catch block with scanner

我完全被难住了,经过几个小时的摆弄,我似乎无法让它工作:

下面的功能基本上是一个菜单。它会要求您输入 1、2 或 3 和 return 那个 int.

如果您输入除 int 以外的任何内容,它会捕获它并再次启动菜单。 如果您输入 1-2-3 以外的整数,它会告诉您,然后再次启动菜单。

问题是,如果您进行了无效输入,它会告诉您正确的事情,重新启动菜单,但是如果您进行了正确的输入,它将 return 您之前尝试过的无效值新的,好的。

我逐行跟踪它,它将从我的 if 中命中 return,然后在 try-catch 结束时命中 return,“choixDeMenu”恢复为旧的无效值,它 return 就是这样。如果你被告知它不是一个整数,它 returns 0,但如果它是一个无效的整数,它会 return 那个无效的整数。

如果您立即进行 VALID 输入,该功能将起作用并且 return 是您的有效输入。只有当你先尝试无效的东西时,我才会遇到问题。

public static int menu()
{
  int choixDeMenu = 0;
  System.out.println ("Sélectionner le fichier à importer :");
  System.out.println ("1. Partie 1.");
  System.out.println ("2. Partie 2.");
  System.out.println ("3. Partie 3.");
  
  Scanner waitingForChoice = new Scanner(System.in);

  try
    {
      choixDeMenu = waitingForChoice.nextInt();
      if (choixDeMenu < 4 && choixDeMenu > 0)
      {
        waitingForChoice.close();
        return choixDeMenu;
      }
        else
        {
          System.out.println("Entrée invalide. Entrer 1, 2, ou 3.");
          menu();
        }  
    } 
      catch (InputMismatchException e)
      {
        System.out.println("Doit être un chiffre. Entrer 1, 2, ou 3.");
        menu();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
      
  return choixDeMenu;
}

任何时候创建这样的菜单,都应该将其包含在一个 while 循环中。因此,不是每次提示用户做出选择时都调用新方法 (menu()),而是简单地返回到块的开头并重复相同的操作 (System.out.println("1..))。

如果您只需要使用 try/catch 来执行此操作,这仍然可以在 while 循环中进行。

从那里您可以使用 switch 语句或组 if-else 来验证每个可能的选择和 return 值。如果您需要使用 try/catch,您可以在其中包含开关或 if-else。

在评论者的帮助下,以下是我对其进行修改的方法:

在 while 循环中转储所有内容,并且由于选择的有效性取决于正确的输入类型或正确范围内的正确类型的输入,我添加了一个布尔值来记录并操作while 循环使用它。

谢谢你所做的一切。

public static int menu()
{
  int choixDeMenu = 0;
  boolean validChoice = false;
  while (validChoice == false)
  {
    System.out.println ("Sélectionner le fichier à importer :");
    System.out.println ("1. Partie 1.");
    System.out.println ("2. Partie 2.");
    System.out.println ("3. Partie 3.");
    Scanner waitingForChoice = new Scanner(System.in);
    
    try
    {      
      choixDeMenu = waitingForChoice.nextInt();
      if (choixDeMenu < 4 && choixDeMenu > 0)
      {
        waitingForChoice.close();
        validChoice = true;
        return choixDeMenu;
      }
        else
        {
          validChoice = false;
          System.out.println("Entrée invalide. Entrer 1, 2, ou 3.");
        }  
    } 
      catch (InputMismatchException e)
      {
        validChoice = false;
        System.out.println("Doit être un chiffre. Entrer 1, 2, ou 3.");
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
  } 
  return choixDeMenu;
}

试试这个代码希望能有所帮助

    public static int menu()
{
  int choixDeMenu = 0;
  System.out.println ("Sélectionner le fichier à importer :");
  System.out.println ("1. Partie 1.");
  System.out.println ("2. Partie 2.");
  System.out.println ("3. Partie 3.");

  Scanner sc = new Scanner(System.in);

  int prev=0;
  int flg=0;

  while(true){
    try{
    choixDeMenu = sc.nextInt();
    if(choixDeMenu>0 && choixDeMenu<4){
      if(flg==0){
        return choixDeMenu;
      }
      else{
        return prev;
      }
    }
    else{
      System.out.println("Entrée invalide. Entrer 1, 2, ou 3.");
      prev = choixDeMenu;
      flg=1;
      continue;
    }
  }
  catch(InputMismatchException e)
      {
        System.out.println("Doit être un chiffre. Entrer 1, 2, ou 3.");
        sc.next();
        continue;
      }
  }

}