【问题标题】:Could someone please explain why I can't get the selection variable back from the method showMenu有人可以解释为什么我不能从方法 showMenu 中取回选择变量
【发布时间】:2022-06-29 13:53:09
【问题描述】:
   int selection = 0;
    showMenu(selection);

   

    


 }
public static int showMenu(int selection) {



    Scanner key = new Scanner(System.in);
    System.out.println("METER CONVERSION\n1) Convert to Kilometers\n2) Convert to Inches\n3) Convert to Feet\n4) Quit the Program");
    System.out.println("Please make a selection: ");
    selection = key.nextInt();
    return selection;

}

我需要知道如何将 showMenu 中的值返回给变量选择。

【问题讨论】:

    标签: java methods


    【解决方案1】:

    selection 的更新值由showMenu() 函数返回。另外请记住,函数参数中的selection变量和函数调用前定义的同名变量是两个不同的变量。

    如果你想使用selection的更新值,你必须这样做:

    int selection = 0;
    selection = showMenu(selection);
    

    或等效代码:

    int selection = showMenu(0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多