Visualizzazione dei risultati da 1 a 7 su 7

Visualizzazione discussione

  1. #4
    Ciao! E grazie della risposta... dunque per ora ho fatto quanto segue, un codice che ha un metodo che calcola le medie e dovrebbe restituirmi la differenza tra esse... peró mi dá errori tipo...

    non-static variable this cannot be referenced from a static context
    return this.first_time_ave; }

    peró se non metto i getter con this.nomevariabile mi crea problemi per variabili non statiche chiamate nella main statica...

    Cosa sto sbagliando?

    codice:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.StringTokenizer;
     
    public class ReadFileee
    {
        /*interval for calculating average, 100ns*/
    
        private boolean first_time_ave=true, second_time_ave=true;
        private double Sum=0, Ave1=0;
        boolean first_time = true;
        private int k=0;
        
        public static void main(String[] args) throws IOException 
        {
            int n=3; 
            int k=0;
            double sum=0;
            
            double T1=0, T2=0;
            
            boolean first_time = true;
            
            
            
        BufferedReader br = null;
     
        
        try{
            String line;
            br = new BufferedReader(new FileReader("prova2.txt"));
            
            
            while ((line = br.readLine()) != null) 
            {
               
               StringTokenizer stringTokenizer = new StringTokenizer(line, " ");
               
               while (stringTokenizer.hasMoreElements()) 
               {
                  double x = Double.parseDouble(stringTokenizer.nextElement().toString());
                  double y = Double.parseDouble(stringTokenizer.nextElement().toString());
                  
                  if (k == 0 && getFirst_time_ave()) T1=x; //fissare il primo elemento x per il primo salto
                  else if (k == 0 && !getFirst_time_ave() && getSecond_time_ave()) T2=x;    //fissare ogni volta il primo elemento, secondo salto
                  
                  if(k<n)
                    {    
                          sum += y;
                          
                          k++;
                          System.out.println("y = " + y);    
                    }
                    
                      //Qui posso chiamare il metodo che legge somma e fa la media?
                  
                  if(k==n) {
                      
                      setSum(sum);
                      
                      if(first_time==true) {
                          Average1(getSum());
                          first_time = false;  
                      }
                         
                      
                      //double DiffInAve = 
                                  
                  //System.out.println("Difference in Averages is D =" + DiffInAve );
                                              
                  //k=0;
                      else {
                          double DiffInAve = Average2(getSum());
                          System.out.println("Difference in Averages is D =" + DiffInAve );
                              }
                          
                      }
                  
                  
                  }
                  
                    
                  
               }
              
            
    
            
    
        }
        
        catch (NumberFormatException e) 
        {
            System.out.println(e.toString());
        }
    finally 
        {
            try 
                {
                    if (br != null)
                    br.close();
                } 
            catch (IOException ex) 
                {
                    ex.printStackTrace();
                }
     
        }
        }
        
        
        
        public static void Average1(double S)
        {
            double ave1=0;
            
            
                ave1=S/3;
                setAve1(ave1);
                System.out.println("sum y = " + S);
                System.out.println("Ave.1 = " + getAve1());
                //first_time = false;
                S=0;    
                
                //return Ave1;
            }
        
        
        public static double Average2(double S)
        {
        double D=0;
            double Ave2=0, A=0, K=0;
             
                    Ave2=S/3;
                    System.out.println("sum y = " + S);
                    System.out.println("Ave.i = " + Ave2);
                    A=getAve1();
                    A=Ave2;
                    //Ave1=Ave2;
                    
                
            D=(A-Ave2);
            Ave2=0;
            S=0;
               
            K=getK();
            K=0;
            
            return D;
                    
        }
        
        /*public double Averages()
    { return this.Averages; }*/
        
        public static boolean getFirst_time_ave()
        {     //this.first_time_ave = first_time_ave;
            return this.first_time_ave; }
        
        public static boolean getSecond_time_ave()
        { return this.second_time_ave; }
        
        public static boolean getFirst_time()
        { return this.first_time; }
        
        public static double getSum()
        { return this.Sum; }
        
        public static double getT1()
        { return this.T1; }
        
        public static double getT2()
        { return this.T2; }
        
        public static double getK()
        { return this.k; }
        
        public static double getAve1()
        { return this.Ave1; }
        
        public static void setSum(double sum)
        {}    
        
        public static void setAve1(double ave1)
        {}    
        
    }
    Ultima modifica di java_beginner; 05-07-2015 a 15:56

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.