指定された文字列のすべての数字を合計する必要があります。指定された文字列の数字を集計する
出力は100の代わりに、4。私は何をすべきである
"アラは3個のリンゴと1個のオレンジを得た":I入力がときに、私はこの
public static void main(String[] args) {
Scanner odczyt = new Scanner(System.in);
System.out.println("Input string");
String ciag = odczyt.nextLine();
int suma = 0;
for (int i = 0; i < ciag.length(); i++) {
if (Character.isDigit(ciag.charAt(i))) {
suma += ciag.charAt(i);
}
}
System.out.println(suma);
}
を持っているためそれは数字だけを数えるために変更?
「Alaが13のリンゴと1つのオレンジを手に入れた」場合は、14または5が必要ですか?言い換えれば、数字を個別に処理する必要がありますか? –
はい、すべての桁を独自の番号に追加する必要があります。 –