個人ACM啓程之路 1.1.1

看着同窗搞ACM心生羨慕而後結網實幹,結果發現沒那麼簡單 java

杭電第一題 spa

第一次代碼(錯誤) code

import java.util.*;

public class Main {
	 public static void main(String[] args){
		 Scanner s = new Scanner(System.in);
			 int a,b;
		 while (true){
			 a=s.nextInt();
			 b=s.nextInt();
			 System.out.println(a+b);
		 }	 
	}
}

hasNextInt

public boolean hasNextInt()

若是經過使用 nextInt() 方法,此掃描器輸入信息中的下一個標記能夠解釋爲默認基數中的一個 int 值,則返回 true。掃描器不執行任何輸入。 io

返回: 當且僅當此掃描器的下一個標記是有效的 int 值時才返回 true class

拋出: IllegalStateException- 若是此掃描器已關閉 import

正確代碼 方法

import java.util.*;

public class Main{
	 public static void main(String[] args){
		 Scanner s = new Scanner(System.in);
			 int a,b;
		 while (s.hasNextInt()){
			 a=s.nextInt();
			 b=s.nextInt();
			 System.out.println(a+b);
		 }	 
	}
}
相關文章
相關標籤/搜索