- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是一名初级/中级 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/
如标题所示,我正在寻找有关伪/冒号 header 字段用途的一些信息,即我想知道为什么我们有第二种类型的 header 字段... 另外 - 我知道在 http2 中使用伪/冒号 header 字段代
(伪)多线程:借助外力 利用WEB服务器本身的多线程来处理,从WEB服务器多次调用我们需要实现多线程的程序。 QUOTE: 我们知道PHP本身是不支持多线程的, 但是我们的WEB服务器是支持多线程的
您如何在 HDL (verilog) 中实现硬件随机数生成器? 需要考虑哪些选项? 这个问题是在self-answer之后格式。鼓励添加答案和更新。 最佳答案 正如摩根的回答中所指出的,这只会产生一个
我写了这个CSS: div { width: 500px; height:150px; margin-left:150px; background: lightblue; } div:
这是我要解决的问题:从数据库A读取一个字符串,将该字符串转换为Date对象,将Date对象存储到数据库B中。 例)数据库A:从数据库A读入日期字符串“ 2015-03-08 02:00:00”,转换为
我想创建 std::fscanf() 的 sibling (我知道这是一个 C 函数)。所以,我的界面是这样的: template std::size_t ts_scanf(is, format,
运行 PostgreSQL 7.x(是的,我正在升级) 问题: 如果没有返回数据,我有三到四个字段需要设置。 正在考虑这样的事情 SELECT CASE WHEN default_fie
出于某种原因,我很难在 JS 中为我的游戏执行以下代码: 假设我们要求用户在棋盘上移动一个棋子。他们可以做的位置是位置A、位置B或位置C。每个位置一次只能容纳一件。否则为无效移动。 第一个用户决定
我已经毫无问题地编写了霍夫曼树的代码,但现在我希望在文件和树中添加伪 EOF,以便我知道何时停止从文件中读取。 我完全掌握了伪 EOF 的概念。我还了解到没有 ASCII 值 > 255 的字符。 我
给定一个按钮 ::after 当被触发时,伪 :after 类需要有一个类 search-active 切换,为按钮设置背景颜色 .primary .search:after, .primary
我想让第一行的文本像第二行一样缩进 (50px)。有什么办法吗?非常感谢! body{ counter-reset: h2counter; } h1{ counter-reset: h2counter
:before 或 :after 这样的伪元素是否可以从父元素的不同属性继承值? 在我的例子中,我有一个第三方组件设置其元素运行时的背景颜色...我需要继承该颜色并将其设置为伪元素的边框颜色。 最佳答
在并行循环中请求随机数总是返回相同的伪随机数。我怎样才能避免这种情况? % workers initialization: if matlabpool('size') == 0 matlabp
假设最大IP可以包含每个“点”括号中的最大数量999,即999.999.999.999 是最大的可用值。 我已经在计算器中检查了正则表达式 ([0-9]+.){3}[0-9]。那么,为什么程序抛出运行
我对随机数生成的概念非常陌生,我需要为用c编写的工作创建自己的算法(内置的随机数生成器对我不起作用)。 有人能给我介绍一个很好的主题,这样我就可以理解这个概念了吗?到目前为止,我所发现的一切似乎都是用
假设我有一个数字序列:{n, n+1, n+2, ... n+m} 在不提前存储数字的情况下,我想创建一个函数 f(),给定序列 {1,2,3,...m} 将以随机(或至少伪)的方式吐出原始集合随机)
什么是伪 tcp channel ,如何实现? 最佳答案 伪 TCP 是一种协议(protocol),它实现了 TCP 的一些思想,以通过不可靠的、基于数据包的接口(interface)提供可靠的数据
我正在尝试展开一些嵌套循环,以牺牲内存为代价(可能)获得更好的性能。在我的场景中,我最终会得到一个包含大约 3 亿个元素(元组)的列表,我必须以(或多或少)随机顺序产生这些元素。 在这个数量级上,ra
如何在 PHP 中生成(伪)随机字母数字字符串,例如:'d79jd8c'? 最佳答案 首先创建一个包含所有可能字符的字符串: $characters = 'abcdefghijklmnopqrstu
我有一段代码可以为玩家生成迷你任务。这很简单,要获得两个不同的点(起点和终点),我有一个如下所示的算法: std::vector missions; missions.push_bac
我是一名优秀的程序员,十分优秀!