-1

I am new to java and i have a project about making a quiz with a timer, but that's not what i was going to ask since i am trying my best to make this project on my own, but this "static" error is something that i think that is hindering me from getting what i wanted from my code, i tried googling many ways to program what i wanted to do and this is the code that i ended up making, just dont mind the logic or my code, its just my code is full of static, i tried removing them all except the one from my main method, but i only get errors out of it, please help me or guide me what do i have to do to remove static. thank you

import java.util.Timer;
import java.util.TimerTask;
import java.util.Scanner;
import java.io.IOException;
public class q2 {
static int seconds = 0; 
static Scanner a=new Scanner(System.in);
static Timer timer = new Timer();
static int number = 1;
static int score = 0;
public static void mema(){
    TimerTask task = new TimerTask () 
{
    public void run() 
    {
        seconds++;
        System.out.print(seconds);


}
};

timer.schedule(task,1000,1000);
}   

public static void tanong1(String... args)throws IOException,InterruptedException
{  
System.out.println("");
System.out.printf("%72s\n","QUIZ FOR PHILIPPINE HISTORY");
System.out.println("");
System.out.printf("%64s\n","Question #1");
System.out.println("");
System.out.println("\t\t\t\t\t     WHO DISCOVERED PHILIPPINES?");
System.out.println("\n\t\t\t\t\t\tA. Fernando Magellan");
System.out.println("\t\t\t\t\t\tB. Ferdinand Megallan");
System.out.println("\t\t\t\t\t\tC. Ferdinand Magellan");
System.out.println("\t\t\t\t\t\tD. Fernando Poe");

System.out.println("\n\t\t\t\t\t\tTYPE YOU ANSWER HERE: "); 
char sagot1 = a.nextLine().charAt(0);   
        switch (sagot1)
        {
            case 'B':
            case 'b':
                score++;    
                seconds=30;
                System.out.print("You are CORRECT!");
                Thread.sleep(3000);
                break;
                default:
                System.out.print("You are WRONG");
                seconds=30;
                Thread.sleep(3000);
                break;

        }

}

public static void tanong2(String... args)throws IOException,InterruptedException
{  
System.out.println("");
System.out.printf("%72s\n","QUIZ FOR PHILIPPINE HISTORY");
System.out.println("");
System.out.printf("%64s\n","Question #2");
System.out.println("");
System.out.println("\t\t\t\t\t     WHO DISCOVERED PHILIPPINES?");
System.out.println("\n\t\t\t\t\t\tA. Fernando Magellan");
System.out.println("\t\t\t\t\t\tB. Ferdinand Megallan");
System.out.println("\t\t\t\t\t\tC. Ferdinand Magellan");
System.out.println("\t\t\t\t\t\tD. Fernando Poe");

System.out.println("\n\t\t\t\t\t\tTYPE YOU ANSWER HERE: ");
char sagot2 = a.nextLine().charAt(0);
        switch (sagot2)
        {
            case 'B':
            case 'b':
                score++;
                seconds=60;
                System.out.print("You are CORRECT!");
                break;
            default:
                System.out.print("You are WRONG");
                seconds=60;
                break;
        }
}

public static void main(String... args)throws IOException,InterruptedException
{  
mema();
while(seconds<=5){
    tanong1();
        }
while(seconds<=60||seconds>=6){
    new ProcessBuilder("cmd","/c","cls").inheritIO().start().waitFor();
    tanong2();
}


}
}   

this is the error that i am getting in case i remove the static on my seconds variable error: non-static variable seconds cannot be referenced from a static context. i just want to know what do i have to do, thank you in advanced.

dave
  • 7
  • 4
  • 4
    "just dont mind the logic or my code" - that strongly suggests you should put more effort into providing a [mcve] that doesn't include the bits you want us to ignore. (Once you've created that, please put more effort into formatting it readably.) – Jon Skeet Jan 15 '18 at 16:59
  • 4
    You can't just add and remove `static` all over the place without understanding what it means. Please see [What does the 'static' keyword do in a class?](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class). Once you understand that maybe you would be able to ask a more specific question or solve the problem on your own. – takendarkk Jan 15 '18 at 17:01
  • 1
    "Ferdinand Megallan" should be correct? His name was Magellan (answer "c"), not Megallan. – Tom Jan 15 '18 at 17:03
  • @JonSkeet thank you for the tip, this is my first post and i am still getting the hang of it. – dave Jan 15 '18 at 17:47
  • @csmckelvey thank you, I will read that right now. – dave Jan 15 '18 at 17:48
  • @Tom uhm yeah, thanks. – dave Jan 15 '18 at 17:49

1 Answers1

1

To remove the statics I moved you whole code into a inner class and executing it in this way.

Code:

import java.util.Timer;
import java.util.TimerTask;
import java.util.Scanner;
import java.io.IOException;

public class q2 {

    public static void main(String... args) {
    new q2().new quiz();
    }

    private class quiz {

    private int seconds = 0;

    private Scanner a = new Scanner(System.in);

    private Timer timer = new Timer();

    private int number = 1;

    private int score = 0;

    quiz() {
        mema();
        while (seconds <= 5) {
        tanong1();
        }
        while (seconds <= 60 || seconds >= 6) {
        try {
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start()
                .waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        tanong2();
        }
    }

    public void mema() {
        TimerTask task = new TimerTask() {

        public void run() {
            seconds++;
            System.out.print(seconds);

        }
        };

        timer.schedule(task, 1000, 1000);
    }

    public void tanong1(String... args) {
        System.out.println("");
        System.out.printf("%72s\n", "QUIZ FOR PHILIPPINE HISTORY");
        System.out.println("");
        System.out.printf("%64s\n", "Question #1");
        System.out.println("");
        System.out.println("\t\t\t\t\t     WHO DISCOVERED PHILIPPINES?");
        System.out.println("\n\t\t\t\t\t\tA. Fernando Magellan");
        System.out.println("\t\t\t\t\t\tB. Ferdinand Megallan");
        System.out.println("\t\t\t\t\t\tC. Ferdinand Magellan");
        System.out.println("\t\t\t\t\t\tD. Fernando Poe");

        System.out.println("\n\t\t\t\t\t\tTYPE YOU ANSWER HERE: ");
        char sagot1 = a.nextLine().charAt(0);
        switch (sagot1) {
        case 'B':
        case 'b':
        score++;
        seconds = 30;
        System.out.print("You are CORRECT!");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        break;
        default:
        System.out.print("You are WRONG");
        seconds = 30;
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        break;

        }

    }

    public void tanong2(String... args) {
        System.out.println("");
        System.out.printf("%72s\n", "QUIZ FOR PHILIPPINE HISTORY");
        System.out.println("");
        System.out.printf("%64s\n", "Question #2");
        System.out.println("");
        System.out.println("\t\t\t\t\t     WHO DISCOVERED PHILIPPINES?");
        System.out.println("\n\t\t\t\t\t\tA. Fernando Magellan");
        System.out.println("\t\t\t\t\t\tB. Ferdinand Megallan");
        System.out.println("\t\t\t\t\t\tC. Ferdinand Magellan");
        System.out.println("\t\t\t\t\t\tD. Fernando Poe");

        System.out.println("\n\t\t\t\t\t\tTYPE YOU ANSWER HERE: ");
        char sagot2 = a.nextLine().charAt(0);
        switch (sagot2) {
        case 'B':
        case 'b':
        score++;
        seconds = 60;
        System.out.print("You are CORRECT!");
        break;
        default:
        System.out.print("You are WRONG");
        seconds = 60;
        break;
        }
    }
    }
}
ProfBits
  • 199
  • 1
  • 9
  • 1
    OP stated that they did this "_i tried removing them all except the one from my main method, but i only get errors out of it_". You need to explain specifically how to avoid those errors once you remove static from everything since that is where they got stuck. – takendarkk Jan 15 '18 at 17:28