简体   繁体   English

无法从另一个类打印变量?

[英]can't print variable from another class?

I am trying to write contents of a variable from another class. 我正在尝试从另一个类编写变量的内容。

code where the variable is set, note that there is a print statement that lets me know how many calories the variable is holding at the moment, this works just the way it should it's not 0 at least 设置变量的代码,请注意,有一条打印语句使我知道该变量当前持有多少卡路里,它的工作方式至少应不为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;

 }




}

the variable that i am trying to write is "calories" it is set from an array, and here is the class where i am trying to write the variable to a text file: 我要写入的变量是“卡路里”,它是从数组中设置的,这是我要在其中将变量写入文本文件的类:

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();
    }
}

This might be a very wrong way of doing this, i am very new to java, but in my head it should just print it, but it prints 0. 这可能是一种非常错误的方式,我对Java还是很陌生,但是在我看来,它应该只打印它,但是它打印0。

How does the saveLoad class know what the food is? saveLoad类如何知道食物是什么?

What you're trying to do will require a constructor 您尝试做的事情将需要一个构造函数

An example of this is: 例如:

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();
    }
}

In the constructor i'm solely guessing the parameters of the food class. 在构造函数中,我只是在猜测food类的参数。

To access this class all you need to do in your main code is create a new instance of it: 要访问此类,您需要在主代码中创建一个新的实例:

SaveLoad save= new SaveLoad(calories);

Then you can call the save function simply by calling this in your main program: 然后,您只需在主程序中调用以下函数即可调用save函数:

save.save();

Alternatively, skip the instantiation of the FO variable: 或者,跳过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();
    }
}

To set different calories instantiate the saveLoad class, and set the calories: 要设置不同的卡路里,请实例化saveLoad类,并设置卡路里:

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

However, whilst this will solve your issue, you need to think about the hierarchy of your class. 但是,尽管这可以解决您的问题,但您需要考虑类的层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM