繁体   English   中英

有人可以解释为什么我不能从方法 showMenu 中取回选择变量

[英]Could someone please explain why I can't get the selection variable back from the method showMenu

   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 返回到变量选择。

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

如果要使用selection的更新值,则必须执行以下操作:

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

或等效代码:

int selection = showMenu(0);

暂无
暂无

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

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