case "Runetrack":
String inputValue1 = JOptionPane
.showInputDialog("Please enter your username");
if (inputValue1.length() == 0)
JOptionPane.showMessageDialog(null, "You must enter a username.",
"Error", JOptionPane.ERROR_MESSAGE);
return;
openUrl("example.com/" + inputValue1);
break;
出于某种原因,它告诉我 openUrl("");不是“无法访问的代码”。为什么以及如何解决这个问题,谢谢。
最佳答案
查看代码逻辑,我猜测错误消息和返回
是有条件的。它们需要放在构成代码块的花括号 {}
内,并且在您的代码中,即 if
语句的代码块。
if (inputValue1.length() == 0) { // if block starts
JOptionPane.showMessageDialog(null, "You must enter a username.",
"Error", JOptionPane.ERROR_MESSAGE);
return;
} // if block ends
openUrl("example.com/" + inputValue1);
break;
您收到的错误是因为,return
语句之后的任何代码语句都不会执行,控件会返回到调用方法。由于 return
不是有条件的(在您发布的代码中),因此之后的两行代码变得无法访问,从而给出您看到的错误。一旦将其放入 if
条件 block 中,它就会正常工作。
关于java - openURL 上无法访问的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23427136/