代码分析
InputData.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package vote; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 7 public class InputData { 8 9 private BufferedReader buf ; 10 11 public InputData() 12 { 13 this.buf = new BufferedReader(new InputStreamReader(System.in)); 14 } 15 16 public String getString(String info) 17 { 18 String str = null; 19 System.out.println(info); 20 try { 21 str = this.buf.readLine(); 22 } catch (IOException e) { 23 // TODO 自动生成的 catch 块 24 e.printStackTrace(); 25 } 26 return str; 27 28 } 29 30 public int getInt(String info,String error) 31 { 32 int temp = 0; 33 String str = null; 34 str = this.getString(info); 35 if(str.matches("\\d+")){ 36 temp = Integer.parseInt(str); 37 }else{ 38 System.out.println(error); 39 } 40 41 return temp; 42 } 43 44 45 }