我想为我的 JavaFX 应用程序创建颜色图,以便根据其值显示具有不同颜色的网格。定义了两种类型:使用DiscreteColorMap整数键和ContinuousColorMap双键。两者都必须实现该接口,以便可以像这样调用它:ColorMap
ColorMap palette1 = new DiscreteColorMap();
ColorMap palette2 = new ContinuousColorMap();
Run Code Online (Sandbox Code Playgroud)
由于这两个类都依赖于相同的接口,因此我指定了一个模板 ( public interface ColorMap<T>) 以适应它们中的每一个:
ColorMap<Integer> palette1 = new DiscreteColorMap();
ColorMap<Double> palette2 = new ContinuousColorMap();
Run Code Online (Sandbox Code Playgroud)
我想要最简单的颜色图语法,所以我需要去掉<Integer>和<Double>字符串。最优雅的方法是什么?
我的英语并不完美^^我用了“摆脱”,但这并不清楚:当我实例化我的颜色图时,我想要制作<Integer>和<Double>消失,所以我可以写ColorMap palette...而不是ColorMap<Integer> palette...。
在给定的字符串中,我想找到最长的单词,然后在控制台中打印它。
我得到的输出是第二长的单词 ie "Today",但我应该得到"Happiest"。
我可以知道我做错了什么吗?有没有更好/不同的方法来找到字符串中最长的单词?
public class DemoString {
public static void main(String[] args) {
String s = "Today is the happiest day of my life";
String[] word = s.split(" ");
String longword = " ";
for (int i = 0; i < word.length; i++)
for (int j = 1 + i; j < word.length; j++)
if (word[i].length() >= word[j].length())
longword = word[i];
System.out.println(longword + " is the longest word with " + longword.length() + …Run Code Online (Sandbox Code Playgroud) 我对 Java 还很陌生,现在已经是培训的第二个月了。我希望每次按下 时New,旧文本都会被删除,然后新文本就会出现。但这不起作用。我已经在互联网上进行了研究。
当我按下 时New,新文本会覆盖旧文本。我想如果我text(null)之前做了,它就会删除它。但这不起作用。
多次按下按钮后:

这是我的代码:
buttonNew.setOnAction(ae -> {
int randomM1 = (int) (Math.random() * 11);
int randomM2 = (int) (Math.random() * 11);
System.out.println(randomM1 + " x " + randomM2);
System.out.println(randomM1 * randomM2);
Text mText = new Text();
mText.setText(null);
mText.setText(randomM1 + " x " + randomM2);
rootM.getChildren().add(mText);
mText.setLayoutX(300);
mText.setLayoutY(200);
mText.setFont(Font.font("Verdana", 50));
});
Run Code Online (Sandbox Code Playgroud) import java.util.ArrayList;
public class LargestPrimeFactor {
private static long maxNum;
public LargestPrimeFactor(long maxNum) {
LargestPrimeFactor.maxNum = maxNum;
ArrayList<Long> listOfPrimes = new ArrayList<>();
listOfPrimes.add(2L);
for (long i = 3; i < maxNum / 2; i++) {
if (i % 2 == 1 && isPrime(i)) {
listOfPrimes.add(i);
}
}
// for (long i = 0; i < listOfPrimes.size(); i++) {
// System.out.println(listOfPrimes.get((int) i));
// }
for (long i = listOfPrimes.size() -1; i >= 0; i--) {
if (maxNum % ((long) listOfPrimes.get(i)) == …Run Code Online (Sandbox Code Playgroud)