gpt4 book ai didi

java - 如何使用小程序运行(伪)main 方法?

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:11 24 4
gpt4 key购买 nike

我是一名初级/中级 Java 程序员,正在尝试编写一些“超出我的范围”的代码。该程序应该通过按下对应于不同得分值的按键来实时判断拳击/综合格斗比赛。我发现我需要一个 KeyListener,而我发现使用它的唯一方法是使用小程序。

我遇到的问题是我必须打印出来自 keyPresses 和 keyReleases 的分数的唯一提示。我希望每一秒都打印出乐谱以及时间。我制作了一个时钟函数,可以使用另一个具有 main 方法的类来打印每秒,但我不知道如何在小程序中执行此操作。

这是我到目前为止所拥有的:

import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.*;

public class KeyPressTwo
extends Applet
implements KeyListener{

private long t;
private ArrayList<Integer> keysDown = new ArrayList<Integer>();
private double controlOnlyValue = 1; //Stores the score per second for control only
private double threateningValue = 2.5; //Score for threatening with strikes, i.e. landing sig strikes, or sub attempts
private double damagingValue = 4; //Score for doing significant damage and dominating hea
private static double redTotal = 0; //Stores fighter score
private static double blueTotal = 0;
private static boolean firstRun = true;
private static boolean start = false;
private static boolean releasePressed = false; //Tells KeysReleased method when to wipe keysDown list
private static long roundBeganAt = 0; //System time when the round began 5
private static String redName;
private static String blueName;


public void init(){
this.addKeyListener(this);
//If names aren't hardcoded in, get them when the program is run
if (redName == null){
redName = JOptionPane.showInputDialog("Enter the red corner fighter's name.");
blueName = JOptionPane.showInputDialog("Enter the blue corner fighter's name.");
}

}

public void paint(){
setSize(500,500);
}


@Override
public void keyPressed(KeyEvent e) {

if(!keysDown.contains(e.getKeyCode()))
keysDown.add(e.getKeyCode());

//Starts the timer, don't print anything until started
if(keysDown.contains(KeyEvent.VK_SPACE)){
start = true;
roundBeganAt = System.currentTimeMillis();
}

//If it's been more than 1s
if(nextStep()){
//If space has been pushed
if(start){
if(keysDown.contains(KeyEvent.VK_Z) || keysDown.contains(KeyEvent.VK_NUMPAD1)){
redTotal += controlOnlyValue;
}
if(keysDown.contains(KeyEvent.VK_X) || keysDown.contains(KeyEvent.VK_NUMPAD4)){
redTotal += threateningValue;
}
if(keysDown.contains(KeyEvent.VK_C) || keysDown.contains(KeyEvent.VK_NUMPAD7)){
redTotal += damagingValue;
}
if(keysDown.contains(KeyEvent.VK_COMMA) || keysDown.contains(KeyEvent.VK_NUMPAD3)){
blueTotal += controlOnlyValue;
}
if(keysDown.contains(KeyEvent.VK_M) || keysDown.contains(KeyEvent.VK_NUMPAD6)){
blueTotal += threateningValue;
}
if(keysDown.contains(KeyEvent.VK_N) || keysDown.contains(KeyEvent.VK_NUMPAD9)){
blueTotal += damagingValue;
}
System.out.print("\n" +redName +": " +redTotal +" \t" +blueName +": " +blueTotal +"\t\t" +time());
releasePressed = true;
}
}
}

//Prints time since start (e.g. 2:05)
private static String time() {
String minutes = "";
String seconds = "";
int sRaw; //Gets time directly from system, will go above 60
int s; //Gets time from sRaw, (0 - 59)

sRaw = (int)((System.currentTimeMillis() - roundBeganAt))/1000;
s = sRaw%60;

minutes = Integer.toString(sRaw/60);
if(s < 10)
seconds = "0" +Integer.toString(s);
else seconds = Integer.toString(s);


return minutes +":" +seconds;
}

//Returns true if it's been more than1s since the last time it ran
public boolean nextStep() {
if(firstRun){
t = System.currentTimeMillis();
firstRun = false;
return true;
}
if(System.currentTimeMillis() > t + 1000){
t = System.currentTimeMillis();
return true;
}else
return false;
}


public void printList(){
for(int i : keysDown)
System.out.print(i +" ");
System.out.println();
}

@Override
public void keyReleased(KeyEvent e) {

if(releasePressed){
keysDown.clear();
releasePressed = false;
}

}

@Override
public void keyTyped(KeyEvent e) {

}
}

最佳答案

也许这些内容对您有用:

Thread timerOutputThread = new Thread(new Runnable(){
public boolean running = true;
public void run(){
output();
}
private void output(){
try {
Thread.sleep(1000);
} catch(Exception ex) {
ex.printStackTrace();
}
System.out.println("PRINT THE SCORE HERE");
if(running){
output();
}
}
});
timerOutputThread.start();

将该代码粘贴到您想要启动线程计时器的任何位置,然后填写“在此处打印分数”的位置。

关于java - 如何使用小程序运行(伪)main 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15419270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com