Created
April 15, 2014 11:50
-
-
Save shoheikawano/10725920 to your computer and use it in GitHub Desktop.
プログラミング初歩見習いのJavaメモ / char型からint型への変換 ref: http://qiita.com/shohe_i/items/b8bdd9517e293d0a1adb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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