我在覆盖从TimerTask类运行的方法时遇到问题.
这是我的代码:
public class PGameCore
{
Toolkit toolKit;
Timer timer;
public PGameCore(int clockIntervalInSeconds)
{
timer = new Timer();
timer.schedule(new CoreTimer(), 1000 * clockIntervalInSeconds);
}
class CoreTimer extends TimerTask
{
public void Run()
{
System.out.println("BEEEP :)");
toolKit.beep();
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题在于:
class CoreTimer extends TimerTask
Run Code Online (Sandbox Code Playgroud)
我正在使用Netbeans.它说:"PGameCore.CoreTimer不是抽象的,不会覆盖TimerTask中的抽象方法run()."
以下代码编译并执行,没有任何错误或警告.但给出了意想不到的结果.
代码:
import java.util.LinkedList;
import java.util.Scanner;
//defining a custom data-type (class)
class LLObj{
static int NodeInt;
static char NodeChar;
LLObj(int x, char y){
NodeInt = x;
NodeChar = y;
}
}
class LLexp{
static LinkedList<LLObj> list = new LinkedList<>();
public static void main(String[] args){
list.addLast(new LLObj(5,'c'));
System.out.println(list.get(0).NodeInt);
System.out.println(list.get(0).NodeChar);
list.addLast(new LLObj(7,'h'));
System.out.println(list.get(0).NodeInt);
System.out.println(list.get(0).NodeChar);
System.out.println(list.get(1).NodeInt);
System.out.println(list.get(1).NodeChar);
}
}
Run Code Online (Sandbox Code Playgroud)
预期产出 -
5
c
5
c
7
h
获得的输出 -
5
c
7
h
7
h
int[] big = {1,2,3,5,11,12,13,25,26};
Run Code Online (Sandbox Code Playgroud)
doSomething将连续的元素组合在一起如何将"大"分成这个:
{{1,2,3},{5},{11,12,13},{25,26}}
Run Code Online (Sandbox Code Playgroud)
我已经开始使用这段代码:
public List<Integer> getR(){
Integer[] big = {1,2,3,5,11,12,13,25,26};
List<Integer> a = new ArrayList<Integer>();
for(int i=0;i<big.length;i++){
if(big[i]==big[i+1]-1){
continue;
}else{
//...
}
//...
}
//...
}
Run Code Online (Sandbox Code Playgroud) 为我正在制作的项目创建了这个程序,但我似乎无法弄清楚为什么它从未在结果中给出任何1.我在这里踢自己因为它可能很简单.
为了清楚起见:程序必须滚动两个单独的模具36k次并显示结果.
import java.util.Random; //Going to need this
public class Dicerolling { //Start Class
public static void main( String[] args)
{ //Start of Main
Random randomNumbers = new Random(); // Generates random numbers
int[] array = new int[ 13 ]; // Declares the array
int dice1 = 0;
int dice2;
int total;
//Roll the die 36,000 times
for ( int roll = 1; roll <=36000; roll++ )
dice1 = 1 + randomNumbers.nextInt ( 6 );
dice2 = 1 + randomNumbers.nextInt …Run Code Online (Sandbox Code Playgroud)