ตัวอักษร
คุณมีรายการคำกริยาที่ต้องการพิมพ์ลงกระดาษ และอยากทราบว่าหมึกในเครื่องพิมพ์มีเพียงพอสำหรับพิมพ์ตัวอักษรทั้งหมดหรือไม่
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
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");
}
}