小编usa*_*800的帖子

JS使用变量替换所有出现的字符串

我知道str.replace(/x/g, "y")替换字符串中的所有x但我想这样做

function name(str,replaceWhat,replaceTo){
    str.replace(/replaceWhat/g,replaceTo);
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能在第一个参数中使用变量?

javascript string variables replace

24
推荐指数
1
解决办法
2万
查看次数

我的isPrime方法有什么问题?

这是我的isPrime方法:

private static boolean isPrime(int num) {
    if (num % 2 == 0) return false;
    for (int i = 3; i * i < num; i += 2)
        if (num % i == 0) return false;
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我把isPrime(9)它放回去了true.这个方法有什么问题?

java methods primes function

11
推荐指数
2
解决办法
7万
查看次数

JTextArea setText(veryLongString)花费了太多时间

我有一个非常长的字符串,我从书中得到.我使用setText()方法在JTextArea中显示它.它冻结了UI并花费了大量时间.我该如何解决这个问题?

这是SSCCE:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class SSCCE extends JFrame {

    JTextArea textArea;

    public SSCCE() {
        String text = buildLongString(400000);
        textArea = new JTextArea();
        textArea.setText(text);
        textArea.setLineWrap(true);
        add(new JScrollPane(textArea));

        setSize(400, 350);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private String buildLongString(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append("x");
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SSCCE(); …
Run Code Online (Sandbox Code Playgroud)

java swing jtextarea

2
推荐指数
1
解决办法
920
查看次数

Java String.replace无效

这是我的代码.

String x = "1+&radic;10";
x = x.replace(".*", "x");
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)

这应该返回,"x"但它正在返回"1+&radic;10".为什么这不起作用?

java regex string

1
推荐指数
1
解决办法
1276
查看次数

比较document.URL时出现意外结果

if(document.URL!="location.php?img_url="+img_url){
        window.location.href = "location.php?img_url="+img_url;
    }
Run Code Online (Sandbox Code Playgroud)

这会不断重新加载页面.我检查网址,看它是否发生变化,但不是.

javascript

0
推荐指数
1
解决办法
251
查看次数

KeyListener不起作用

这段代码有什么问题?

addKeyListener(new KeyHandler());

private class KeyHandler extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            int key = e.getKeyCode();

            System.out.println("test");
            if(key==KeyEvent.VK_SPACE || key==KeyEvent.VK_ENTER || key==KeyEvent.VK_P) {
                paused = true;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这应该在控制台中打印测试,但它没有.我究竟做错了什么?

java swing keylistener

0
推荐指数
1
解决办法
380
查看次数