0

我正在尝试从另一个类中写入变量的内容。

设置变量的代码,请注意有一个打印语句让我知道该变量目前持有多少卡路里,这就像它应该至少不是 0 的方式一样

        edit


 import java.util.*; //importing utilities use the Scanner

public class food extends products{
    //variable declaration
private String[] foodName;
private String[] drinkName;
private String[] NoNo;

public int calories;
public int protz;
public int carbz;
public int fatz;

Scanner input = new Scanner(System.in);//importing scanner and creating an object for it
Interface inte = new Interface(); //creating an object interface
caloriesmain m = new caloriesmain(); // creating a main class object to enable the exit to main menu



    public void setFood(){
        this.name = "Food"; //overriding name variable inherited from class products to "Food"
        this.description = "NB!The amount of nutrition is based on the raw weight of product with skin where applicable";//overriding description variable inherited from class products to a descriptive message
       System.out.println(name); // printing the header of the Menu

          //Storing items in to the array    
        String[] foodNA = {"1. Chicken", "2.Eggs", "3. Turkey", "4. Pork", "5. Beef", "6. Fish", "7, Rice"}; //storing food names
        Integer[] nutriCalories = {230, 84, 194, 149, 150, 82, 111};//storing calories for each food name
        Integer[] nutriProtein = {43, 6, 29, 28, 25, 18, 3};//Storing protein amount
        Integer[] nutriCarb = {0, 1, 0, 0, 0, 0, 23};//storing carbohydrate amount
        Integer[] nutriFat = {5, 6, 8, 3, 4, 1, 1};//storing fat amount
        this.foodName = foodNA;  
        //converting arrays to arraylists( reason for conversion: could not find a way to index a regular array)
        List<Integer> intList = Arrays.asList(nutriCalories);
        List<Integer> intList2 = Arrays.asList(nutriProtein);
        List<Integer> intList3 = Arrays.asList(nutriCarb);
        List<Integer> intList4 = Arrays.asList(nutriFat);


        System.out.println(description); //displaying an informative message    
       System.out.println(Arrays.toString(getFood()));//displaying all of the stored food names
       System.out.println("8. Display Current intake");
        System.out.println("9. Next");

        int option = input.nextInt();//calling scanner to store an integer entered by the user in option variable


        if (option == 1){                   //if user chooses option number 1
            int cals = intList.get(0);               //stores the amount of calories that is on index 0 in the arraylist
            int prots = intList2.get(0);             //stores the amount of protein that is on index 0 in the arraylist
            int carbs = intList3.get(0);            //stores the amount of carbohydrates that is on index 0 in the arraylist
            int fats = intList4.get(0);              //stores the amount of fat that is on index 0 in the arraylist

            System.out.println("+" + cals);//print out how many calories did this product add to the total amount
            calories +=cals;//adds calories to the current amount
            protz +=prots;//adds protein to the current amount
            carbz +=carbs;//adds carbohydrates to the current amount
            fatz +=fats; //adds fat to the current amount
            System.out.println("total: " + calories);//displays the running total of the calories after each choice
            setFood();  //displays the list again if user needs to add more than one product        
            }

       else if (option == 2){                   //if user chooses option number 1
            int cals = intList.get(1);               //stores the amount of calories that is on index 0 in the arraylist
            int prots = intList2.get(1);             //stores the amount of protein that is on index 0 in the arraylist
            int carbs = intList3.get(1);            //stores the amount of carbohydrates that is on index 0 in the arraylist
            int fats = intList4.get(1);              //stores the amount of fat that is on index 0 in the arraylist

            System.out.println("+" + cals);//print out how many calories did this product add to the total amount
            calories +=cals;//adds calories to the current amount
            protz +=prots;//adds protein to the current amount
            carbz +=carbs;//adds carbohydrates to the current amount
            fatz +=fats; //adds fat to the current amount
            System.out.println("total: " + calories);//displays the running total of the calories after each choice
            setFood();  //displays the list again if user needs to add more than one product        
            }
         else if (option == 3){                   //if user chooses option number 1
            int cals = intList.get(2);               //stores the amount of calories that is on index 0 in the arraylist
            int prots = intList2.get(2);             //stores the amount of protein that is on index 0 in the arraylist
            int carbs = intList3.get(2);            //stores the amount of carbohydrates that is on index 0 in the arraylist
            int fats = intList4.get(2);              //stores the amount of fat that is on index 0 in the arraylist

            System.out.println("+" + cals);//print out how many calories did this product add to the total amount
            calories +=cals;//adds calories to the current amount
            protz +=prots;//adds protein to the current amount
            carbz +=carbs;//adds carbohydrates to the current amount
            fatz +=fats; //adds fat to the current amount
            System.out.println("total: " + calories);//displays the running total of the calories after each choice
            setFood();  //displays the list again if user needs to add more than one product        
            }
         else if (option == 4){                   //if user chooses option number 1
            int cals = intList.get(3);               //stores the amount of calories that is on index 0 in the arraylist
            int prots = intList2.get(3);             //stores the amount of protein that is on index 0 in the arraylist
            int carbs = intList3.get(3);            //stores the amount of carbohydrates that is on index 0 in the arraylist
            int fats = intList4.get(3);              //stores the amount of fat that is on index 0 in the arraylist

            System.out.println("+" + cals);//print out how many calories did this product add to the total amount
            calories +=cals;//adds calories to the current amount
            protz +=prots;//adds protein to the current amount
            carbz +=carbs;//adds carbohydrates to the current amount
            fatz +=fats; //adds fat to the current amount
            System.out.println("total: " + calories);//displays the running total of the calories after each choice
            setFood();  //displays the list again if user needs to add more than one product        
            }
         else if (option == 5){                   //if user chooses option number 1
            int cals = intList.get(4);               //stores the amount of calories that is on index 0 in the arraylist
            int prots = intList2.get(4);             //stores the amount of protein that is on index 0 in the arraylist
            int carbs = intList3.get(4);            //stores the amount of carbohydrates that is on index 0 in the arraylist
            int fats = intList4.get(4);              //stores the amount of fat that is on index 0 in the arraylist

            System.out.println("+" + cals);//print out how many calories did this product add to the total amount
            calories +=cals;//adds calories to the current amount
            protz +=prots;//adds protein to the current amount
            carbz +=carbs;//adds carbohydrates to the current amount
            fatz +=fats; //adds fat to the current amount
            System.out.println("total: " + calories);//displays the running total of the calories after each choice
            setFood();  //displays the list again if user needs to add more than one product        
            } 

         else if (option == 6){                   //if user chooses option number 1
            int cals = intList.get(5);               //stores the amount of calories that is on index 0 in the arraylist
            int prots = intList2.get(5);             //stores the amount of protein that is on index 0 in the arraylist
            int carbs = intList3.get(5);            //stores the amount of carbohydrates that is on index 0 in the arraylist
            int fats = intList4.get(5);              //stores the amount of fat that is on index 0 in the arraylist

            System.out.println("+" + cals);//print out how many calories did this product add to the total amount
            calories +=cals;//adds calories to the current amount
            protz +=prots;//adds protein to the current amount
            carbz +=carbs;//adds carbohydrates to the current amount
            fatz +=fats; //adds fat to the current amount
            System.out.println("total: " + calories);//displays the running total of the calories after each choice
            setFood();  //displays the list again if user needs to add more than one product        
            }

          else if (option == 7){                   //if user chooses option number 1
            int cals = intList.get(6);               //stores the amount of calories that is on index 0 in the arraylist
            int prots = intList2.get(6);             //stores the amount of protein that is on index 0 in the arraylist
            int carbs = intList3.get(6);            //stores the amount of carbohydrates that is on index 0 in the arraylist
            int fats = intList4.get(6);              //stores the amount of fat that is on index 0 in the arraylist

            System.out.println("+" + cals);//print out how many calories did this product add to the total amount
            calories +=cals;//adds calories to the current amount
            protz +=prots;//adds protein to the current amount
            carbz +=carbs;//adds carbohydrates to the current amount
            fatz +=fats; //adds fat to the current amount
            System.out.println("total: " + calories);//displays the running total of the calories after each choice
            setFood();  //displays the list again if user needs to add more than one product        
            }

        else if(option == 8){
            System.out.println("Current intake \n Calories:" + calories + "\nProtein: " + protz + "\nCarbohydrates: " + carbz + "\nFats :" +fatz);
            setFood(); 
        }
        else if(option==9){// if user chooses 9 then the next method is called

                setDrink();
        }
    }

    public String[] getFood(){

        return foodName;

    }


    public void setDrink(){
        this.name = "Drinks";
       System.out.println(name);


        String[] drinkNA = {"1. Coffee", "2.Tea", "3. Ionised Drinks", "4. Soda", "5. Cola", "6. Diet Coke", "7. Milk"};
        Integer[] nutriCalories = {3, 3, 80, 11, 12, 0, 42};
        Integer[] nutriProtein = {0, 0, 0, 0, 0, 0, 4};
        Integer[] nutriCarb = {0, 1, 1, 3, 3, 0, 5};
        Integer[] nutriFat = {0, 0, 0, 0, 0, 0, 1};
        this.drinkName = drinkNA;  
        List<Integer> intList = Arrays.asList(nutriCalories);
        List<Integer> intList2 = Arrays.asList(nutriProtein);
        List<Integer> intList3 = Arrays.asList(nutriCarb);
        List<Integer> intList4 = Arrays.asList(nutriFat);


       System.out.println(Arrays.toString(getDrink()));
       System.out.println("8. Display current Intake");
       System.out.println("9. Next");


          int option = input.nextInt();


        if (option == 1){
            int cals = intList.get(0);
            int prots = intList2.get(0);            
            int carbs = intList3.get(0);          
            int fats = intList4.get(0);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setDrink();

            }

        else if (option == 2){
            int cals = intList.get(1);
            int prots = intList2.get(1);            
            int carbs = intList3.get(1);          
            int fats = intList4.get(1);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setDrink();           
            }


        else if (option == 3){
            int cals = intList.get(2);
            int prots = intList2.get(2);            
            int carbs = intList3.get(2);          
            int fats = intList4.get(2);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setDrink();           
            }
        else if (option == 4){
            int cals = intList.get(3);
            int prots = intList2.get(3);            
            int carbs = intList3.get(3);          
            int fats = intList4.get(3);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setDrink();           
            }

            else if (option == 5){
            int cals = intList.get(4);
            int prots = intList2.get(4);            
            int carbs = intList3.get(4);          
            int fats = intList4.get(4);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setDrink();           
            }
                else if (option == 6){
            int cals = intList.get(5);
            int prots = intList2.get(5);            
            int carbs = intList3.get(5);          
            int fats = intList4.get(5);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setDrink();           
            }
                    else if (option == 7){
            int cals = intList.get(6);
            int prots = intList2.get(6);            
            int carbs = intList3.get(6);          
            int fats = intList4.get(6);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setDrink();           
            }





          else if(option == 8){
            System.out.println("Current intake \n Calories:" + calories);           
            setDrink(); 
        }

        else if(option==9){

                setNono();
        }


    }

public String[] getDrink(){

        return drinkName;

    }





 public void setNono(){
        this.name = "Restricted products";
        this.description = "Alcoholic drinks are measured 'per shot'(30ml),. rest of the food is measured in average serving sizes";

       System.out.println(name);


        String[] Nono = {"1. Drugs", "2.Whiskey", "3. Vodka", "4. Fries", "5. Hamburger", "6. Pizza"};
        Integer[] nutriCalories = {0, 70, 85, 192, 300, 530};
        Integer[] nutriProtein = {0, 0, 0, 3, 15, 21};
        Integer[] nutriCarb = {0, 0, 0, 25, 35, 55};
        Integer[] nutriFat = {0, 0, 0, 10, 11, 25};
        this.NoNo = Nono;  
        List<Integer> intList = Arrays.asList(nutriCalories);
        List<Integer> intList2 = Arrays.asList(nutriProtein);
        List<Integer> intList3 = Arrays.asList(nutriCarb);
        List<Integer> intList4 = Arrays.asList(nutriFat);

       System.out.println(description);      
       System.out.println(Arrays.toString(getNono()));
       System.out.println("7. Display current Intake");
       System.out.println("8. Exit");


          int option = input.nextInt();


        if (option == 1){
            int cals = intList.get(0);
            int prots = intList2.get(0);            
            int carbs = intList3.get(0);          
            int fats = intList4.get(0);              

            System.out.println("Consuming Drugs can harm you and others around you);
            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setDrink();

            }

        else if (option == 2){
            int cals = intList.get(1);
            int prots = intList2.get(1);            
            int carbs = intList3.get(1);          
            int fats = intList4.get(1);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setNono();           
            }


        else if (option == 3){
            int cals = intList.get(2);
            int prots = intList2.get(2);            
            int carbs = intList3.get(2);          
            int fats = intList4.get(2);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setNono();           
            }
        else if (option == 4){
            int cals = intList.get(3);
            int prots = intList2.get(3);            
            int carbs = intList3.get(3);          
            int fats = intList4.get(3);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setNono();           
            }

            else if (option == 5){
            int cals = intList.get(4);
            int prots = intList2.get(4);            
            int carbs = intList3.get(4);          
            int fats = intList4.get(4);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setNono();           
            }
                else if (option == 6){
            int cals = intList.get(5);
            int prots = intList2.get(5);            
            int carbs = intList3.get(5);          
            int fats = intList4.get(5);              


            System.out.println("+" + cals);
            calories +=cals;
            protz +=prots;
            carbz +=carbs;
            fatz +=fats; 
            System.out.println("total: " + calories);
           setNono();           
            }






         else if(option == 7){

           ;

             printIntake();
              setNono();

         }




        else if(option==8){



                m.main(Nono); //exit to main menu
        }


    }

 public void printIntake(){
     System.out.println( calories);     

 }



 public String[] getNono(){
     return NoNo;

 }




}

我要写入的变量是“卡路里”,它是从数组中设置的,这是我尝试将变量写入文本文件的类:

import java.io.*;
import java.util.*;

public class saveLoad extends caloriesmain{

public food FO = new food();


    private Scanner x;

public void save(){
System.out.println();

             try {

      File file = new File("example.txt");
      BufferedWriter output = new BufferedWriter(new FileWriter(file));
      output.write("calories" + FO.calories);
      output.close();
    } catch ( IOException e ) {
       e.printStackTrace();
    }
}

这可能是一种非常错误的方法,我对 java 很陌生,但在我的脑海中它应该只打印它,但它打印 0。

4

1 回答 1

0

saveLoad 类如何知道食物是什么?

你想要做的将需要一个构造函数

这方面的一个例子是:

import java.io.*;
import java.util.*;

public class SaveLoad extends caloriesmain{

public food FO;
private Scanner x;

public SaveLoad(int calories){
    FO= new food(calories);
}

public void save(){
System.out.println();

             try {

      File file = new File("example.txt");
      BufferedWriter output = new BufferedWriter(new FileWriter(file));
      output.write("calories" + FO.calories);
      output.close();
    } catch ( IOException e ) {
       e.printStackTrace();
    }
}

在构造函数中,我只是在猜测食物类的参数。

要访问此类,您需要在主代码中创建一个新实例:

SaveLoad save= new SaveLoad(calories);

然后您可以通过在主程序中调用它来调用保存函数:

save.save();

或者,跳过 FO 变量的实例化:

import java.io.*;
import java.util.*;

public class SaveLoad extends caloriesmain{

private int calories;
private Scanner x;

public SaveLoad(int calories){
    this.calories=calories;
}


//because we've called the calories integer a private variable we need a set method
//it can only be seen from within this class
public void setCalories(int calories){
    this.calories=calories
}

public void save(){
System.out.println();

             try {

      File file = new File("example.txt");
      BufferedWriter output = new BufferedWriter(new FileWriter(file));
      output.write("calories" + FO.calories);
      output.close();
    } catch ( IOException e ) {
       e.printStackTrace();
    }
}

要设置不同的卡路里,请实例化 saveLoad 类,并设置卡路里:

SaveLoad save= new SaveLoad(calories);
save.setCalories(10);
save.save();

但是,虽然这将解决您的问题,但您需要考虑类的层次结构。

于 2013-05-23T00:00:49.150 回答