演習の解答例

問題 1

String[] rows = loadStrings("excercise1.txt");
int lines = rows.length;
int words = 0;
int chars = 0;
for (int i = 0; i < lines; ++i) {
  int spaces = 0;
  for (int j = 0; j < rows[i].length(); ++j) {
    if (rows[i].charAt(j) == ' ') {
      spaces += 1;
    } else {
      chars += 1;
    }
  }
  if (spaces >= 1) {
    words += spaces + 1;
  }
}
println("行数: " + lines);
println("単語数: " + words);
println("文字数: " + chars);

問題 2

String[] rows = loadStrings("excercise2.txt");
int[] values = new int[rows.length];
for (int i = 0; i < rows.length; ++i) {
  values[i] = int(rows[i]);
}

int odd = 0;
int even = 0;
for (int i = 0; i < values.length; ++i) {
  if (values[i] % 2 == 0) {
    even += values[i];
  } else {
    odd += values[i];
  }
}
println("偶数の和: " + even);
println("奇数の和: " + odd);

問題 3

size(400, 400);
Table table = loadTable("excercise3.csv", "header");
for (int i = 0; i < table.getRowCount(); ++i) {
  int x = table.getInt(i, "x");
  int y = table.getInt(i, "y");
  int r = table.getInt(i, "r");
  ellipse(x, y, 2 * r, 2 * r);
}

問題 4

JSONObject json = loadJSONObject("excercise4.json");
JSONObject glossary = json.getJSONObject("glossary");
JSONObject glossDiv = glossary.getJSONObject("GlossDiv");
JSONObject glossList = glossDiv.getJSONObject("GlossList");
JSONObject glossEntry = glossList.getJSONObject("GlossEntry");
JSONObject glossDef = glossEntry.getJSONObject("GlossDef");
JSONArray glossSeeAlso = glossDef.getJSONArray("GlossSeeAlso");
for (int i = 0; i < glossSeeAlso.size(); ++i) {
  println(glossSeeAlso.getString(i));
}

results matching ""

    No results matching ""