回 帖 发 新 帖 刷新版面

主题:请帮忙修改一下程序

class Node1//定义节点
    {
        int node1_id=1;
        int S12=10,S13=10,F12=10,F13=10,I12=20,I13=20;
        double L12=0.5,L13=0.5,R12=0.0,R13=0.0,C12=0.0,C13=0.0,D12=1.0,D13=1.0,W12=0.672,W13=0.672,G12=0.336,G13=0.336,T1=0.0;
        int K12=10,K13=10;
        double alpha=0.8;
        int beta=2;
        
        void compute1()
        {
            I12=S12+F12;
            L12=S12/I12;
            L13=S13+F13;
            I13=S13/I13;
            
            R12=(S12-F12)/I12;
            R13=(S13-F13)/I13;
            
            
            D12=1-Math.sqrt((L12-L13)/2);
            
            
            D13=1-Math.sqrt((L12-L13)/2);
            
            C12=1-D12;
            C13=1-D13;
            
            T1=(R12+R13)*(C12+C13);
                        
            W12=1-Math.sqrt(Math.pow(0.8,K12));
            W13=1-Math.sqrt(Math.pow(0.8,K13));
            
            G12=W12*L12+(1-W12)*T1;
            G13=W13*L13+(1-W13)*T1;
        }
        
    }
public class DHT
    public static void main(String args[]) throws IOException
    {

        
        int i,node_require,node_exchange;
        String str1,str2,str3;
        BufferedReader buf;
        
        
        buf=new BufferedReader(new InputStreamReader(System.in));
        
        
        
        System.out.print("请输入交易请求节点:");
          str1=buf.readLine();                    
          node_require=Integer.parseInt(str1);
               
          System.out.print("请输入交易节点:");
          str2=buf.readLine();                    
          node_exchange=Integer.parseInt(str2);     
          
          System.out.print("请输入交易次数:");
          str3=buf.readLine();                    
          i=Integer.parseInt(str3);                
          
          
          switch(node_require='1')
          {
              case 1:
                  node_exchange='1';
                  System.out.println("不能与自身结点进行交易");
                  break;
              case 2:
                  node_exchange='2';
                  Node1 node1=new Node1();
                  Node1.compute1();
                  node1.L12=new node1(n1.L12);
                  node1.G12=new node1(n1.G12);
                  System.out.println("L12"+L12);
                  System.out.println("G12"+G12);
                  break;
                                    
          }
          
    }

在编译的时候报错,提示non-static variable this cannot be referenced from a static context,请教各位高手如何修改,谢谢

回复列表 (共1个回复)

沙发

在静态的代码块中不能引用非静态的变量,说得蛮清楚了~
你要么把这些全都加上static:如下
static int node1_id=1;
static int S12=10,S13=10,F12=10,F13=10,I12=20,I13=20;
static double L12=0.5,L13=0.5,R12=0.0,R13=0.0,C12=0.0,C13=0.0,D12=1.0,D13=1.0,W12=0.672,W13=0.672,G12=0.336,G13=0.336,T1=0.0;
static int K12=10,K13=10;
static double alpha=0.8;
static int beta=2;
static void compute1()
{

要么就不要把程序都写在main方法里面

我来回复

您尚未登录,请登录后再回复。点此登录或注册