![](/img/trans.png)
[英]Can someone please explain to me why I get a different output when I run the same command from Java
[英]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.