這是我的代碼:import java.io.*;public class Main { public static void main(String[] args) throws Exception { BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); String s = b.readLine(); for (int i = 0; i < s.length(); i++) { if (i % 2 == 0) { int a = Integer.parseInt(String.valueOf(s.charAt(i))); System.out.print(a); } } }}這段代碼適用于一位數(shù)的整數(shù),但如果輸入是兩位數(shù),那么它就會一團(tuán)糟。我的輸入:1 3 6 5 7 輸出:1 3 6 5 7 效果很好,但是,如果輸入是:1 3 66 58 7 輸出:發(fā)生異常。如何處理這樣的兩位整數(shù)輸入。
2 回答

白豬掌柜的
TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
試著解析你用 readLine() 得到的整行:
String s = b.readLine();
int a = Integer.parseInt(s);
如果該字符串不是數(shù)字,您將收到異常。
添加回答
舉報(bào)
0/150
提交
取消