Logo
Section 1 / 3

数组和数组操作

通过构建一个健康餐食追踪器来学习 Java 数组,该追踪器可存储每日卡路里摄入量,并帮助你查看每周总摄入量。

导入

数组和数组操作

通过构建一个健康餐食追踪器来学习 Java 数组,该追踪器可存储每日卡路里摄入量,并帮助你查看每周总摄入量。

...

代码示例

1Breakfast: 400 2Lunch: 600
1int[] calories = {400, 600, 300}; // 一天各餐的卡路里列表 2String[] meals = {"Breakfast", "Lunch", "Dinner"}; // 各餐名称列表
1int[] meals = {400, 600, 300};
1int[] meals = {400, 600, 300}; 2System.out.println(Arrays.toString(meals)); 3
1import java.util.Arrays; 2 3public class MealTotals { 4 public static void main(String[] args) { 5 int[] meals = {400, 600, 300}; 6 int total = 0; 7 8 for (int meal : meals) { 9 total += meal; 10 } 11 12 System.out.println("一天总卡路里: " + total); 13 } 14}

代码示例

1请输入第1天的餐食(用逗号分隔):Breakfast,Lunch,Dinner 2请输入第1天各餐的卡路里(用逗号分隔):400,600,300 3 4第1天餐食:[Breakfast, Lunch, Dinner] 5第1天总卡路里:1300 6 7请输入第2天的餐食(用逗号分隔):Breakfast,Lunch,Dinner 8请输入第2天各餐的卡路里(用逗号分隔):500,700,250 9 10第2天餐食:[Breakfast, Lunch, Dinner] 11第2天总卡路里:1450 12 13一周总卡路里:2750 14很棒,继续保持饮食记录!
PageFlux AI 助手

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

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

No configuration found for ID: 18cc522762ec8de03182385b6900b3ec1aa6c32113dda89157c3e5a50a020338

1// 健康餐食追踪器 2 3import java.util.*; 4 5public class HealthyMealsTracker { 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 int weeklyTotal = 0; // 存储一周总卡路里 9 10 // --- 每日输入循环 --- 11 for (int day = 1; day <= 2; day++) { // 测试时使用2天 12 System.out.print("请输入第 " + day + " 天的餐食(用逗号分隔): "); 13 String[] meals = sc.nextLine().split(","); 14 15 System.out.print("请输入第 " + day + " 天各餐的卡路里(用逗号分隔): "); 16 String[] calStr = sc.nextLine().split(","); 17 int[] calories = new int[calStr.length]; 18 19 for (int i = 0; i < calStr.length; i++) { 20 calories[i] = Integer.parseInt(calStr[i].trim()); 21 } 22 23 int dayTotal = 0; 24 for (int cal : calories) { 25 dayTotal += cal; 26 } 27 28 System.out.println("第 " + day + " 天餐食: " + Arrays.toString(meals)); 29 System.out.println("第 " + day + " 天总卡路里: " + dayTotal); 30 31 weeklyTotal += dayTotal; // 将当天卡路里加入一周总计 32 } 33 34 // --- 最终输出 --- 35 System.out.println("一周总卡路里: " + weeklyTotal); 36 System.out.println("很棒,继续保持饮食记录!"); 37 } 38}
1// 健康餐食追踪器 2 3import java.util.*; 4 5public class HealthyMealsTracker { 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 int weeklyTotal = 0; // 存储一周总卡路里 9 10 // --- 每日输入循环 --- 11 for (int day = 1; day <= 2; day++) { // 测试时使用2天 12 System.out.print("请输入第 " + day + " 天的餐食(用逗号分隔): "); 13 String[] meals = sc.nextLine().split(","); 14 15 System.out.print("请输入第 " + day + " 天各餐的卡路里(用逗号分隔): "); 16 String[] calStr = sc.nextLine().split(","); 17 int[] calories = new int[calStr.length]; 18 19 for (int i = 0; i < calStr.length; i++) { 20 calories[i] = Integer.parseInt(calStr[i].trim()); 21 } 22 23 int dayTotal = 0; 24 for (int cal : calories) { 25 dayTotal += cal; 26 } 27 28 System.out.println("第 " + day + " 天餐食: " + Arrays.toString(meals)); 29 System.out.println("第 " + day + " 天总卡路里: " + dayTotal); 30 31 weeklyTotal += dayTotal; // 将当天卡路里加入一周总计 32 } 33 34 // --- 最终输出 --- 35 System.out.println("一周总卡路里: " + weeklyTotal); 36 System.out.println("很棒,继续保持饮食记录!"); 37 } 38}

代码示例

1今天吃了多少零食? 3 2请输入零食名称:Chips 3请输入零食卡路里:150 4请输入零食名称:Juice 5请输入零食卡路里:200 6请输入零食名称:Cookies 7请输入零食卡路里:100 8 9今天的零食:[Chips, Juice, Cookies] 10今天零食数量:3 11卡路里:[150, 200, 100] 12零食总卡路里:450 13记得用健康餐食来平衡零食摄入!

No configuration found for ID: 8faba0316af1d4bda429d6297ae32ada55f0c22e76ce409d3a31a4f87c0094b7

1// 每日零食追踪器 2 3import java.util.*; 4 5public class DailySnacksTracker { 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 9 // --- 目标输入 --- 10 System.out.print("今天吃了多少零食? "); 11 int numSnacks = sc.nextInt(); 12 sc.nextLine(); // 消耗剩余的换行符 13 14 ArrayList<String> snacks = new ArrayList<>(); // 存储零食名称 15 ArrayList<Integer> calories = new ArrayList<>(); // 存储零食卡路里 16 17 // --- 零食输入循环 --- 18 for (int i = 0; i < numSnacks; i++) { 19 System.out.print("请输入零食名称: "); 20 String snack = sc.nextLine(); 21 snacks.add(snack); 22 23 System.out.print("请输入零食卡路里: "); 24 int cal = sc.nextInt(); 25 sc.nextLine(); // 消耗剩余的换行符 26 calories.add(cal); 27 } 28 29 // --- 最终输出 --- 30 System.out.println("今天的零食: " + snacks); 31 System.out.println("今天零食数量: " + snacks.size()); 32 System.out.println("卡路里: " + calories); 33 34 int total = 0; 35 for (int cal : calories) { 36 total += cal; 37 } 38 39 System.out.println("零食总卡路里: " + total); 40 System.out.println("记得用健康餐食来平衡零食摄入!"); 41 } 42}
1// 每日零食追踪器 2 3import java.util.*; 4 5public class DailySnacksTracker { 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 9 // --- 目标输入 --- 10 System.out.print("今天吃了多少零食? "); 11 int numSnacks = sc.nextInt(); 12 sc.nextLine(); // 消耗剩余的换行符 13 14 ArrayList<String> snacks = new ArrayList<>(); // 存储零食名称 15 ArrayList<Integer> calories = new ArrayList<>(); // 存储零食卡路里 16 17 // --- 零食输入循环 --- 18 for (int i = 0; i < numSnacks; i++) { 19 System.out.print("请输入零食名称: "); 20 String snack = sc.nextLine(); 21 snacks.add(snack); 22 23 System.out.print("请输入零食卡路里: "); 24 int cal = sc.nextInt(); 25 sc.nextLine(); // 消耗剩余的换行符 26 calories.add(cal); 27 } 28 29 // --- 最终输出 --- 30 System.out.println("今天的零食: " + snacks); 31 System.out.println("今天零食数量: " + snacks.size()); 32 System.out.println("卡路里: " + calories); 33 34 int total = 0; 35 for (int cal : calories) { 36 total += cal; 37 } 38 39 System.out.println("零食总卡路里: " + total); 40 System.out.println("记得用健康餐食来平衡零食摄入!"); 41 } 42}

循环与重复

HashMaps 用于分类