我坚持这个问题〜:
方法Integer.parseInt将数字字符串转换为它表示的int数.
public static int parseInt (String s)
throws NumberFormatException
Run Code Online (Sandbox Code Playgroud)
解释如何捕获parseInt抛出的异常,并用代码示例说明您的答案.
到目前为止,我的想法是:
try
{
System.out.println("incorrect");
}
catch (NumberFormatException n)
{
System.out.println("WRONG! This is not a number");
}
Run Code Online (Sandbox Code Playgroud)
我觉得这个答案是错误的,因为上面的代码只会在main方法中使用,我不知道如何在下面捕获异常:
public static int parseInt (String s)
throws NumberFormatException.
Run Code Online (Sandbox Code Playgroud)
我通过java API查看了parseInt方法,并通过各种研究查看并尝试了代码,但是由于我缺乏理解,我仍然在努力做到这一点.
如果有人能帮助我,我将不胜感激,提前谢谢.
我是初学者,通过回答过去的考试试卷中的问题来学习java和修改我的考试,还有一个问题我被困在了.
考虑下面的代码片段,它读取输入命令然后处理它.
String cmd = scanner.next();
if (cmd == "forward" )
robot.forward(1);
else if (cmd == "turn" )
robot.turn();
else
System.out.println("Unknown command: " + cmd);
Run Code Online (Sandbox Code Playgroud)
在测试程序时,扫描程序将字符串"forward"读入cmd,但程序输出"Unknown command:forward".
a)详细解释为什么会发生这种情况.
b)应该对代码进行哪些更改以更正此错误.
如果有人可以帮我回答问题a)和b)我将不胜感激.
ps我明白这不是一个只寻找答案的网站(#noeasywayout)所以我会尽量不要在这里贪婪.对于给您带来的任何不便,我深表歉意.