Bonjour pour un exercice j'ai du crée un jeu assez simple et y ajouter un temps limité avec Timer et TimerTask cependant je tombe sur cette erreur " non-static variable this cannot be referenced from a static context " et je ne sais pas comment la résoudre.
Quelqu'un pourrait t-il m'aider à corriger ce problème ? Merci d'avance !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
           import java.util.Scanner;
                import java.util.Random;
                import java.util.Timer;
                import java.util.TimerTask;
                public class NewMain {
 
                    /**
                     * @param args the command line arguments
                     */
                    public  void main(String[] args) {
                Timer temps = new Timer("Printer");
                Mytask t = new Mytask();
                temps.schedule(t,0,2000);
 
                int a = 0;        
 
 
                final String oui = "oui";
                final String non = "non";
                System.out.print("Bienvenue dans Pick'n'lock\n");
                System.out.print("Êtes vous prêt ? ");
                System.out.print("oui/non ?\n");
                Scanner daniet = new Scanner(System.in);
                String c = daniet.nextLine();
 
 
 
 
 
 
 
                switch(c){
                    case oui :
                    System.out.print("Hello World\n");
                    Random rand = new Random();
                    int combi1 = rand.nextInt(100+1);
                    int combi2 = rand.nextInt(100+1);
                    int combi3 = rand.nextInt(100+1);
                    System.out.println(""+combi1);
                    System.out.println(""+combi2);
                    System.out.println(""+combi3);
                    int ok1=0;
                    int ok2=0;
                    int ok3=0;
 
                    Scanner trouve = new Scanner(System.in);
                    do{
                        System.out.print("Entrez premier nombre\n");
                    int trouve1 = trouve.nextInt();
 
                    if (trouve1 < combi1){
                    System.out.print("C'est plus grand\n");
                    }else if (trouve1 > combi1) {
                    System.out.print("C'est plus petit\n");
                    } else {
                    System.out.print("Nombre 1 trouvé !\n");
                    ok1++;
 
                    }
                    }while(ok1==0);
                    do{
                    System.out.print("Entrez deuxième nombre\n");
                        int trouve2 = trouve.nextInt();
                         if (trouve2 < combi2){
                    System.out.print("C'est plus grand\n");
                    }else if (trouve2 > combi2) {
                    System.out.print("C'est plus petit\n");
                    } else {
                    System.out.print("Nombre 2 trouvé !\n");
                    ok2++;
                    }
                    }while (ok2==0);
 
                    do{
                    System.out.print("Entrez troisième nombre\n");
                        int trouve3 = trouve.nextInt();
                         if (trouve3 < combi3){
                    System.out.print("C'est plus grand\n");
                    }else if (trouve3 > combi3) {
                    System.out.print("C'est plus petit\n");
                    } else {
                    System.out.print("Nombre 3 trouvé !\n");
                    ok3++;
                    }
                    }while (ok3==0);
                   System.out.print("Prenez les documents secrets et filez vite la police arrive !");
 
 
 
 
                    break;
                    case non:
                    System.out.print("Good Bye World");
                    break;
 
 
                }
                    }
                   class Mytask extends TimerTask {
                       private  int times = 0;
                       @Override
                       public void run(){
                           times++;
                           if (times <= 5){
                               System.out.print("La police arrive");
                           }else{
                               System.out.print("Trop tard !");
                               this.cancel();
                           }
                       }
 
                   }
                }