Logo
Section 1 / 2

While 循环用于追踪步数

通过构建一个每日步数追踪器来学习 Java 中的 while 循环,该程序会持续询问步数,直到用户完成输入。

导入

While 循环用于追踪步数

通过构建一个每日步数追踪器来学习 Java 中的 while 循环,该程序会持续询问步数,直到用户完成输入。

...
1public class LoopDemo { 2 public static void main(String[] args) { 3 int count = 1; 4 5 while (count <= 5) { 6 System.out.println("This is loop number " + count); 7 count = count + 1; 8 } 9 } 10}

No configuration found for ID: 1bbce99969691ac70f96cd81321f5c11639a6d53cde55a20293a9087571baa55

Code Example

1Enter your steps (or type 'done' to finish): 3000 2Enter your steps (or type 'done' to finish): 4500 3Enter your steps (or type 'done' to finish): 2200 4Enter your steps (or type 'done' to finish): done 5 6Total steps: 9700 7Great job staying active!
PageFlux AI 助手

👋 你好!我是 PageFlux AI 助手,专门帮助您理解课程内容。有什么问题尽管问我吧!

💡 选中文本后点击"AI 询问",或 Ctrl+Enter 发送消息

No configuration found for ID: 4f080d79ea4f5633093f8e54078fccce2ce56c3eebb0b24ea549e0de08bd5ae8

1import java.util.Scanner; 2 3public class StepTracker { 4 public static void main(String[] args) { 5 Scanner scanner = new Scanner(System.in); 6 7 int totalSteps = 0; 8 9 System.out.print("Enter your steps (or type 'done' to finish): "); 10 String userInput = scanner.nextLine(); 11 12 while (!userInput.equals("done")) { 13 if (userInput.matches("\\d+")) { 14 int steps = Integer.parseInt(userInput); 15 totalSteps += steps; 16 } else { 17 System.out.println("Invalid input. Please enter a number or 'done'."); 18 } 19 20 System.out.print("Enter your steps (or type 'done' to finish): "); 21 userInput = scanner.nextLine(); 22 } 23 24 System.out.println("Total steps: " + totalSteps); 25 System.out.println("Great job staying active!"); 26 27 scanner.close(); 28 } 29}
1import java.util.Scanner; 2 3public class StepTracker { 4 public static void main(String[] args) { 5 Scanner scanner = new Scanner(System.in); 6 7 int totalSteps = 0; 8 9 System.out.print("Enter your steps (or type 'done' to finish): "); 10 String userInput = scanner.nextLine(); 11 12 while (!userInput.equals("done")) { 13 if (userInput.matches("\\d+")) { 14 int steps = Integer.parseInt(userInput); 15 totalSteps += steps; 16 } else { 17 System.out.println("Invalid input. Please enter a number or 'done'."); 18 } 19 20 System.out.print("Enter your steps (or type 'done' to finish): "); 21 userInput = scanner.nextLine(); 22 } 23 24 System.out.println("Total steps: " + totalSteps); 25 System.out.println("Great job staying active!"); 26 27 scanner.close(); 28 } 29}

Code Example

1Enter your daily step goal: 5000 2Enter steps taken (or type 'done' to finish): 1200 3Enter steps taken (or type 'done' to finish): 2500 4Enter steps taken (or type 'done' to finish): done 5 6Total steps taken: 3700 7Steps left to reach your goal: 1300 8Keep moving and stay active!

No configuration found for ID: 74ea3a5fd02ad672770d901ed1f5a32eb2e8df85b245aad5b99fbc3a8da93f6a

1import java.util.Scanner; 2 3public class StepGoalTracker { 4 public static void main(String[] args) { 5 Scanner scanner = new Scanner(System.in); 6 7 // --- Goal input with validation --- 8 System.out.print("Enter your daily step goal: "); 9 String goalInput = scanner.nextLine(); 10 11 while (!goalInput.matches("\\d+") || Integer.parseInt(goalInput) <= 0) { 12 System.out.println("Invalid input. Please enter a positive number."); 13 goalInput = scanner.nextLine(); 14 } 15 int goal = Integer.parseInt(goalInput); 16 17 int totalSteps = 0; 18 19 // --- Loop for step input --- 20 System.out.print("Enter steps taken (or type 'done' to finish): "); 21 String userInput = scanner.nextLine(); 22 23 while (!userInput.equals("done")) { 24 if (userInput.matches("\\d+")) { 25 int steps = Integer.parseInt(userInput); 26 if (steps > 0) { // prevent negative or zero values 27 totalSteps += steps; 28 } else { 29 System.out.println("Please enter a positive number."); 30 } 31 } else { 32 System.out.println("Invalid input. Please enter a number or 'done'."); 33 } 34 35 System.out.print("Enter steps taken (or type 'done' to finish): "); 36 userInput = scanner.nextLine(); 37 } 38 39 // --- Final Output --- 40 System.out.println("Total steps taken: " + totalSteps); 41 System.out.println("Steps left to reach your goal: " + (goal - totalSteps)); 42 System.out.println("Keep moving and stay active!"); 43 44 scanner.close(); 45 } 46}
1import java.util.Scanner; 2 3public class StepGoalTracker { 4 public static void main(String[] args) { 5 Scanner scanner = new Scanner(System.in); 6 7 // --- Goal input with validation --- 8 System.out.print("Enter your daily step goal: "); 9 String goalInput = scanner.nextLine(); 10 11 while (!goalInput.matches("\\d+") || Integer.parseInt(goalInput) <= 0) { 12 System.out.println("Invalid input. Please enter a positive number."); 13 goalInput = scanner.nextLine(); 14 } 15 int goal = Integer.parseInt(goalInput); 16 17 int totalSteps = 0; 18 19 // --- Loop for step input --- 20 System.out.print("Enter steps taken (or type 'done' to finish): "); 21 String userInput = scanner.nextLine(); 22 23 while (!userInput.equals("done")) { 24 if (userInput.matches("\\d+")) { 25 int steps = Integer.parseInt(userInput); 26 if (steps > 0) { // prevent negative or zero values 27 totalSteps += steps; 28 } else { 29 System.out.println("Please enter a positive number."); 30 } 31 } else { 32 System.out.println("Invalid input. Please enter a number or 'done'."); 33 } 34 35 System.out.print("Enter steps taken (or type 'done' to finish): "); 36 userInput = scanner.nextLine(); 37 } 38 39 // --- Final Output --- 40 System.out.println("Total steps taken: " + totalSteps); 41 System.out.println("Steps left to reach your goal: " + (goal - totalSteps)); 42 System.out.println("Keep moving and stay active!"); 43 44 scanner.close(); 45 } 46}

逻辑判断:If/Else

for 循环与数组