为什么我的代码无法打印arraylist中的内容?

时间:2019-04-25 00:46:41

标签: java arrays arraylist printing

因此,我正在开发一个健身应用程序,该应用程序将接收用户信息,并输出其燃烧的卡路里数量以及根据其BMI应遵循的饮食计划。好吧,到目前为止,我没有遇到任何麻烦,并且我根据ArrayList中的进餐计划保存了有关要吃哪种食物的所有信息。但是,我的代码无法打印出数组列表中的任何内容。我不知道该怎么办,我什至在互联网上查找东西,但有帮助。这不是重复的,请帮助我,我将非常感谢。

  import java.util.Scanner;
  import java.util.ArrayList;
  import java.io.*;
  import java.lang.Math;
  import java.nio.file.Paths;
  import java.nio.file.Files;


  public class Project5
{
  public static void main(String[] args) throws IOException
 {

    System.out.println("Welcome to the fitness app");
    String name;
    double weight;
    double age;
    int feet;
    int inches;
    double height;
    String gender;
    double bmi;
    double rlweight;
    double rhweight;
    boolean launch;
    double calories = 0.0;
    String juice;
    boolean x = true;
    double inches2;
    boolean underweight = false;
    boolean normal = false;
    boolean overweight = false;
    boolean obese = false;
    boolean xobese = false;
    String status;
    ArrayList<Food> n = new ArrayList<Food>();
    addFoodsToListfast(n);
    ArrayList<obx> obx = new ArrayList<obx>();
    addFoodsToListslow(obx);
    ArrayList<over> ov = new ArrayList<over>();
    addFoodsToListmoderate(ov);
    ArrayList<under> und = new ArrayList<under>();
    addFoodsToListfour(und);
    FileInputStream fileIn = new 
    FileInputStream("Foodstomaintainweight.txt");
    Scanner foods = new Scanner(fileIn);
    String[] foodss = 

Files.lines(Paths.get(“ Foodstomaintainweight.txt”))。toArray(String [] :: new);

    Scanner input = new Scanner(System.in);

    System.out.println("Please input your name: ");
    name = input.nextLine();

    System.out.println("How many feet are you?: ");
    feet = input.nextInt();

    System.out.println("How many inches are you?: ");
    inches = input.nextInt();
    height = ((feet * 12) + inches); 
    System.out.println("Please enter your weight: ");
    weight = input.nextInt();

    System.out.println("Please enter your age: ");
    age = input.nextInt();


    System.out.println("Please specify your gender: ");
    gender = input.next();

    System.out.println("Please tell me about your activity status is it 
    sedentary, moderate or active? ");
    status = input.next();


    if(gender.equals("Male") && status.equals("sedentary")){
        calories = (10 * weight + 6.25 * height - 5 * age + 5) * 1.2;
        //Math.round(calories);
        System.out.println("You burn " + Math.round(calories) + " 
       calories everyday.");

    }else if(gender.equals("Male") && status.equals("moderate")){
        //calories = 10 * weight + 6.25 * height - 5 * age - 161;
        calories = (10 * weight + 6.25 * height - 5 * age + 5) * 1.55;
        System.out.println("You burn " + Math.round(calories) + " 
    calories everyday.");

    }else if(gender.equals("Male") && status.equals("active")){
        calories = (10 * weight + 6.25 * height - 5 * age + 5) * 1.725;
        System.out.println("You burn " + Math.round(calories) + " 
  calories everyday.");

    }else if(gender.equals("Female") && status.equals("sedentary")){
        calories = (10 * weight + 6.25 * height - 5 * age - 161) * 1.2;
        //Math.round(calories);
        System.out.println("You burn " + Math.round(calories) + " 
  calories everyday.");

    }else if(gender.equals("Female") && status.equals("moderate")){
        //calories = 10 * weight + 6.25 * height - 5 * age - 161;
        calories = (10 * weight + 6.25 * height - 5 * age - 161) * 1.55;
        System.out.println("You burn " + Math.round(calories) + " 

每天的卡路里。”);

    }else if(gender.equals("Female") && status.equals("active")){
        calories = (10 * weight + 6.25 * height - 5 * age - 161) * 1.725;
        System.out.println("You burn " + Math.round(calories) + " 
  calories everyday.");

    }else{
        System.out.println("Invalid Gender or Activity status.");

    }





    bmi = (weight) / (height * height) * 703;


    if(age >= 18){
        launch = true;
        }else{

        System.out.println("You are not old enough to use this app");
        launch = false;
      }
     while(launch != false){
          if(bmi > 30 && bmi < 35){
              obese = true;
              System.out.println("You are obese");

              break;
            }else if(bmi > 35){
                xobese = true;
         System.out.println("You are dangerously obese");
         break;
          }else if(bmi < 25 && bmi > 18.5 ) {
              normal = true;
         System.out.println("You are normal weight");

         break;
         }else if(bmi > 25 && bmi < 30){

             overweight = true;
        System.out.println("You are overweight");  
        break;
         } else{

             underweight = true;
        System.out.println("You are underweight");
        break;
       }
    }
    String text4;
     while(normal == true){
     System.out.println("You are in good shape");
     System.out.println("I can recommend you a list of foods you could eat to maintain your current weight.");
     System.out.println("Would you like that? ");
     text4 = input.next();
        if(text4.equals("yes") || text4.equals("Yes") || text4.equals("YES")){
         System.out.println("Alright then here is the list!");
         for (int i =0; i < foodss.length; i ++){
          System.out.println(foodss[i]);
        }     
          break;
      }else{
        System.out.println("Ok thanks for using this app!!");
       break;
      }
    }

   String text;
    while(underweight == true){
    System.out.println("Looks like you are underweight");

    System.out.println("Do you want to gain weight fast or slow? ");
    text = input.next();
    if(text.equals("fast") || text.equals("FAST") || text.equals("Fast")){
        System.out.println("The amount of calories you should take everyday is " + Math.round(calories + 500) + " to gain weight"); 
        System.out.println("I am going to recommend you a 3500 calorie diet plan ");
        String output = "";
        for(int i =0; i < und.size(); i++){
        output = output + und.get(i);
        }
        System.out.println(output);
        break;
    } else{
        System.out.println("Thanks for using the app!.");
   }
   }

    String text1;
    while(overweight == true){
    System.out.println("Looks like you are overweight");
    System.out.println("Do you want to lose weight? ");
    text1 = input.next();
    if(text1.equals("yes") || text1.equals("Yes") || text1.equals("YES")){
        System.out.println("The amount of calories you should take everyday is " + Math.round(calories + 500) + " to gain weight"); 
        break;
    } 
    else{
        System.out.println("Oh well good luck with life.");
   }
   }

    String text2;
    while(obese == true){
    System.out.println("Looks like you are obese");
    System.out.println("Do you want to lose weight? ");
    text2 = input.next();
    if(text2.equals("yes") || text2.equals("Yes") || text2.equals("YES")){
        System.out.println("The amount of calories you should take everyday is " + Math.round(calories + 500) + " to gain weight"); 
        break;
    } else{
        System.out.println("Oh well good luck with life.");
   }
}
    String text3;
    while(xobese == true){
    System.out.println("Looks like you are dangerously obese");
    System.out.println("You should really consider losing weight or you will die!");
    System.out.println("But STILL do you wanna lose weight?");
    text3 = input.next();
    if(text3.equals("yes") || text3.equals("Yes") || text3.equals("YES")){
        System.out.println("The amount of calories you should take everyday is " + Math.round(calories + 500) + " to gain weight"); 
        break;
    } else{
        System.out.println("Oh well good luck with life.");
   }
   }

}

public static void addFoodsToListslow(ArrayList<obx> obx)
  {
      Food butteredtoast = new Food("1-slice Buttered Toast", Food.meal.BREAKFAST, 150);
      Food egg = new Food("1-Egg", Food.meal.BREAKFAST, 80);
      Food Almonds = new Food("27-Almonds", Food.meal.BREAKFAST, 170);
      Food banana = new Food("1-Large Banana", Food.meal.BREAKFAST, 90);
      Food Greekyogurt = new Food("1-cup Greekyogurt", Food.meal.BREAKFAST, 120);
      Food Blueberries = new Food("30-BlueBerries", Food.meal.BREAKFAST, 40);

      Food grilledchicken = new Food("8-oz Grilled Chicken", Food.meal.LUNCH, 225);
      Food Grilledvegetable = new Food("5 oz Grilled vegetable", Food.meal.LUNCH, 125);
      Food Pasta = new Food("1/2 cup Pasta", Food.meal.LUNCH, 185);

      Food Hummus = new Food("1-Dip Hummus", Food.meal.SNACK, 50);
      Food BabyCarrot = new Food("5-Baby Carrots", Food.meal.SNACK, 35);
      Food Crackers = new Food("5-Crackers", Food.meal.SNACK, 65);

      Food salmon = new Food("7-ozSalmon", Food.meal.DINNER, 225);
      Food brownrice = new Food("1-cup Brown Rice", Food.meal.DINNER, 175);
      Food greenbeans = new Food("1/2 cup Green Beans", Food.meal.DINNER, 100);

}
public static void addFoodsToListfast(ArrayList<Food> n)
  {
      Food cereal = new Food("1 serving-All-bran cereal", Food.meal.BREAKFAST, 125);
      Food milk = new Food("1cup-Milk", Food.meal.BREAKFAST, 50);
      Food banana = new Food("1-Large Banana", Food.meal.BREAKFAST, 90);

      Food cucumber = new Food("1-Large Cucumber", Food.meal.SNACK, 30);
      Food avocadodip = new Food("Avocado Dip", Food.meal.SNACK, 50);
      Food grilledcheese = new Food("Grilled Cheese with tomatoes", Food.meal.LUNCH, 300);
      Food salad = new Food("Salad", Food.meal.LUNCH, 50);

      Food Walnuts = new Food("10-Walnuts", Food.meal.SNACK, 100);

      Food grilledchicken = new Food("8oz Grilled Chicken", Food.meal.LUNCH, 225);
      Food BrusselSprouts = new Food("Brussel Sprouts", Food.meal.DINNER, 100);
      Food Quinoa = new Food("Quinoa", Food.meal.DINNER, 105);


}
public static void addFoodsToListmoderate(ArrayList<over> ov)
  {

      Food Granola = new Food("1 Bar Granola", Food.meal.BREAKFAST, 120);
      Food Greekyogurt = new Food("1 Cup Greekyogurt", Food.meal.BREAKFAST, 120);
      Food Blueberries = new Food("15-BlueBerries", Food.meal.BREAKFAST, 40);
      Food butteredtoast = new Food("1slice-Buttered Toast", Food.meal.BREAKFAST, 150);

      Food orange = new Food("1-Orange", Food.meal.SNACK, 70);

      Food chickenandveg = new Food("1-Cup Chicken and Vegetable Soup", Food.meal.LUNCH, 300);
      Food Bread = new Food("2-Slice Bread", Food.meal.LUNCH, 100);

      Food Apple = new Food("1-Apple", Food.meal.SNACK, 75);
      Food PeanutButter = new Food("Peanut Butter", Food.meal.SNACK, 75);

      Food Steak = new Food("5oz Steak", Food.meal.DINNER, 375);
      Food Mashedpotatoes = new Food("1-Cup Mashedpotatoes", Food.meal.DINNER, 150);
      Food Asparagus = new Food("Asparagus", Food.meal.DINNER, 75);


}
public static void addFoodsToListfour(ArrayList<under> und)
  {
      Food cereal = new Food("1-cup All-bran cereal", Food.meal.BREAKFAST, 125);
      Food milk = new Food("1-cup Milk", Food.meal.BREAKFAST, 50);
      Food banana = new Food("1-Large Banana", Food.meal.BREAKFAST, 90);
      Food Greekyogurt = new Food("1-Cup Greekyogurt", Food.meal.BREAKFAST, 120);
      Food Blueberries = new Food("40-BlueBerries", Food.meal.BREAKFAST, 200);

      Food egg = new Food("2-Egg", Food.meal.BREAKFAST, 160);
      Food Almonds = new Food("25-Almonds", Food.meal.BREAKFAST, 300);


      Food Bread = new Food("3-Slices Bread", Food.meal.LUNCH, 200);
      Food grilledchicken = new Food("10oz-Grilled Chicken", Food.meal.LUNCH, 225);
      Food Grilledvegetable = new Food("Grilled vegetable", Food.meal.LUNCH, 125);
      Food Pasta = new Food("1-Cup Pasta", Food.meal.LUNCH, 185);
      Food Apple = new Food("2-Apple", Food.meal.SNACK, 150);
      Food CheesePizza = new Food("2-Cheese Pizza", Food.meal.SNACK, 300);


      Food Steak = new Food("9oz-Steak", Food.meal.DINNER, 375);
      Food Mashedpotatoes = new Food("1cup-Mashedpotatoes", Food.meal.DINNER, 150);
      Food brownrice = new Food("1 Cup-Brown Rice", Food.meal.DINNER, 175);
      Food greenbeans = new Food("1Cup-Green Beans", Food.meal.DINNER, 100);


}

1 个答案:

答案 0 :(得分:1)

在应该将内容添加到列表中的方法中,例如,,您没有在其中添加stuf,而只是初始化了一堆addFoodsToListfast对象。因此,在方法内部,当您声明Food对象时,必须将其添加到列表中,如下所示:

Food