我正在尝试用 JavaFX 制作一款数独游戏,但我不知道如何只允许输入一个字母。答案是调用文本字段并执行以下操作:
myTextField.setOnKeyPressed(e ->
{
if (!myTextField.getText().length().isEmpty())
{
// Somehow reject the key press?
}
}
Run Code Online (Sandbox Code Playgroud)
上面的方法不适用于复制粘贴...或大量其他东西等等。使用像这样的按键侦听器似乎是一个糟糕的主意。一定有更好的东西吗?文本字段是否有一个属性只允许输入某些字符,或者只允许输入一定数量的字符?
谢谢你!
我正在尝试制作一个程序来为用户生成一个随机帐户名.用户将点击一个按钮,它会将帐户名称复制到他的剪贴板.它的GUI部分正在工作,但我想不出处理随机生成String的最佳方法.
用户名中的允许字符:AZ az _
连续数字,没有其他符号和两个相同的字符都不会出现.
必须是六个长度.
我的想法:
create an array of characters:
[ _, a, b, c, d ... etc ]
Generate a random integer between 0 and array.length - 1
and pick the letter in that slot.
Check the last character to be added into the output String,
and if it's the same as the one we just picked, pick again.
Otherwise, add it to the end of our String.
Stop if the String length is of length six. …Run Code Online (Sandbox Code Playgroud) 在StackOverflow上,我看到了响应:
A
Set只是一个Map,除了它Key就是它Value.这就是为什么既Set与Map不能有重复的,因为它违反了唯一Key原则.
但后来我看到(例如)Set根本不涉及自己Map.逸岸,Map甚至不是部分的Collections同时Set是.但对我来说,这没有意义,因为除了事实上两者都来自不同的接口之外,HashSet很可能JDK中的实现与实现非常相似HashMap.
在这方面Set和Map这方面有什么关系?
我正在寻找一个与句点字符匹配的正则表达式,仅当该句点周围的字符都不是句点时。
Fine by me... leave! FAIL
Okay.. You win. SUCCEED
Okay. SUCCEED //Note here, the period is the last char in the string.
Run Code Online (Sandbox Code Playgroud)
我在想:
[^\\.*]\\.
Run Code Online (Sandbox Code Playgroud)
但那是错误的,而且可能根本就没有朝着正确的方向发展。我希望这个问题也能帮助处于同样情况的其他人。
谢谢。
我有一个名为GUI的类来管理我的应用程序.当用户想要在我的程序中删除他的帐户时,我想要弹出警告框并要求他确认或取消他的操作,因此他不会意外删除他的帐户.
package application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.*;
/**
* An Alert Box for the GUI
* to display errors to the user.
*
*/
public class AlertBox
{
Stage window;
public boolean display(String title, String message)
{
boolean cancel = false;
window = new Stage(); // Create the stage.
window.initModality(Modality.APPLICATION_MODAL); // If window is up, make user handle it.
window.setTitle(title);
window.setMinHeight(350);
window.setMinWidth(250);
VBox root = new VBox(); // Root layout. …Run Code Online (Sandbox Code Playgroud) 如果你有一个功能,采取以下措施:
void foo(char **arr);
Run Code Online (Sandbox Code Playgroud)
你怎么能做到以下几点:
void foo(char* x[] = { "hello", "my", "friend" });
Run Code Online (Sandbox Code Playgroud)
如果这让您感到困惑,那么在Java中我们通过以下方式实现此目的:
public void foo(String[] x);
foo(new String[] { "hello", "my", "friend" });
Run Code Online (Sandbox Code Playgroud)
目前,我在C中做了以下我讨厌,因为它看起来很丑:
char* myArr[] =
{
"hello", "my", "friend"
};
foo(myArr);
Run Code Online (Sandbox Code Playgroud) 我想在我的Lua程序中有一个只读表。如果密钥被删除或密钥与新值相关联,则必须引发错误。
function readonly(table)
local meta = { } -- metatable for proxy
local proxy = { } -- this table is always empty
meta.__index = table -- refer to table for lookups
meta.__newindex = function(t, key, value)
error("You cannot make any changes to this table!")
end
setmetatable(proxy, meta)
return proxy -- user will use proxy instead
end
Run Code Online (Sandbox Code Playgroud)
效果很好。
t = { }
t["Apple"] = "Red"
t[true] = "True!"
t[51] = 29
for k,v in pairs(t) do
print(v)
end
t = …Run Code Online (Sandbox Code Playgroud) 我最近一直在编写像Lua这样的脚本语言,并且匿名内部函数的存在让我思考.用C语言实现的语言(如Lua)如何在C语言中具有内部函数,无论您做什么,都无法避免在编译期间必须事先声明函数的事实?这是否意味着在C中,实际上有一种方法可以实现内部函数,而这仅仅是实现庞大的代码库以使它们成为可能的问题?
例如
void *block = malloc(sizeof(1) * 1024); // somehow
// write bytes to this memory address to make it operate
// like an inner function?
// is that even possible?
char (*letterFunct)(int) = ((char (*letterFunct)(int))block;
// somehow trick C into thinking this block is a function?
printf("%c\n", (*letterFunct)(5)); // call it
Run Code Online (Sandbox Code Playgroud)
我缺少的关键概念是如何弥合这一差距,理解为什么某些具有高级功能的语言(类,对象,内部函数,多线程)可以用没有所有这些语言的语言实现?
以下代码将y输出为大整数,而不是15.我不明白为什么.我知道--和++操作员在*操作员面前,所以它应该工作.
下面的代码试图说什么.
/*
Create a variable, set to 15.
Create a pointer to that variable.
Increment the pointer, into undefined memory space.
Decrement the pointer back where it was,
then return the value of what is there,
and save it into the variable y.
Print y.
*/
int main()
{
int x = 15;
int *test = &x;
test++;
int y = *test--;
printf("%d\n", y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
相反,我将代码更改为以下内容:
int main()
{ …Run Code Online (Sandbox Code Playgroud) 此代码段打印该值5.我不明白为什么.
#include <stdio.h>
struct A
{
int x;
};
struct B
{
struct A a;
int y;
};
void printA(struct A *a)
{
printf("A obj: %d\n", a->x);
}
int main()
{
struct B b = {
{
5
},
10
};
struct A *a = (struct A*)&b;
printA(a);
printf("Done.\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我创建时b,指向它的指针将指向数据{ {5}, 10 }.
当我&b转向时struct A*,我向编译器保证这struct A*指向数据类型的单个数据元素的结构int.相反,我提供一个指针数据类型的两个数据元素的结构struct A,int.
即使第二个变量被忽略(因为struct …