自分用にAtCoderの解答をまとめていきます。誤った情報があったら、申し訳ございません。
覚えること
- charAtメソッド
文字列からn番目の文字列を引き出すためのStringクラスのメソッド。文字列の先頭は0文字目。
コード
ABC081A_PlacingMarbles
import java.util.Scanner;
public class ABC081A_PlacingMarbles {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
String str = sc.next();
System.out.println(cal(str));
}
}
public static int cal (String str) {
int count = 0;
for (int i = 0 ; i < str.length() ;i++ ){
if (str.charAt(i) == '1'){
count++;
}
}
return count;
}
}
参考にしたサイト