เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

ตัวอักษร

คุณมีรายการคำกริยาที่ต้องการพิมพ์ลงกระดาษ และอยากทราบว่าหมึกในเครื่องพิมพ์มีเพียงพอสำหรับพิมพ์ตัวอักษรทั้งหมดหรือไม่

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Java ระดับกลาง

ดูคอร์ส

คำแนะนำการฝึกหัด

  • หาจำนวนคำทั้งหมดใน Array conjugation
  • เลือกตัวดำเนินการเปรียบเทียบที่เหมาะสมเพื่อให้ค่าอยู่ต่ำกว่า tabLength
  • เลือกตัวดำเนินการอัปเดตที่ถูกต้องเพื่อเพิ่มค่า i ในแต่ละรอบ
  • ระบุตัวแปรที่ใช้ดึงแต่ละ word จาก conjugation

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

class CharacterCounter {
  public static void main(String[] args) {
    String[] conjugation = {"hide", "hindered", "hiding", "hidden", "hindering", "hid", "hides", "hinder"};
    // Retrieve the number of words in the array by calling the correct field
    int tabLength = conjugation.____;
    int numberOfCharacters = 0;

    // Choose the appropriate operators (logical and increment)
    for (int i = 0; i ____ tabLength; i____) {
      // Give the proper variable to get each word out of conjugation
      String word = conjugation[____];
      numberOfCharacters += word.length();
    }
    System.out.println("There are " + numberOfCharacters + " characters in the list");
  }
}
แก้ไขและรันโค้ด