Skip to content

Instantly share code, notes, and snippets.

@shoheikawano
Created April 15, 2014 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoheikawano/10725920 to your computer and use it in GitHub Desktop.
Save shoheikawano/10725920 to your computer and use it in GitHub Desktop.
プログラミング初歩見習いのJavaメモ / char型からint型への変換 ref: http://qiita.com/shohe_i/items/b8bdd9517e293d0a1adb
public static void main (String[] args) {
int sum = 0;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)) {
while(sum == 0) {
System.out.print("3桁の数値を入力してください: ");
String line = reader.readLine();
for(int i = 0; i < line.length(); i++) {
char ch = line.charAt(i);
try {
sum += Character.getNumericValue(ch);
} catch(NumberFormatException e) {
System.out.println("数字の形式が正しくありません。");
}
}
}
} catch (IOException e) {
System.out.println(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment