我是java的新手,我有一个关于使用计时器进行测验的项目,但这不是我要问的问题,因为我正在努力自己创建这个项目,但是这个“静态”错误我认为这是阻碍我从我的代码中得到我想要的东西,我尝试使用google搜索我想要做的事情的许多方法,这是我最终制作的代码,只是不介意逻辑或我的代码,它只是我的代码充满了静态,我尝试删除它们除了我的主要方法之外的所有,但我只是从它得到错误,请帮助我或指导我我需要做什么来删除静态。谢谢
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();
}
}
}
这是我遇到的错误,以防我删除我的秒变量上的静态 错误:无法从静态上下文引用非静态变量秒。 我只是想知道我该做什么,谢谢你的进步。
答案 0 :(得分:1)
为了删除静态,我将整个代码移动到内部类中并以这种方式执行它。
代码:
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;
}
}
}
}