小编Mr.*_*irl的帖子

如何在另一个之前执行线程?

我有一个小问题.

我有两个主题:

  • HelloThread - 这打印"你好"五次.
  • GoodbyeThread - 这五次打印"再见".

我想先跑,HelloThread然后GoodbyeThread跑完了.
我已经用信号量解决了这个问题(但信号量并不是真正的java方式,它更多的是C方式).

HelloThread.java

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package modifiedthreadhellogoodbye;

    import static java.lang.Thread.sleep;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.concurrent.Semaphore;

    class HelloThread implements Runnable {

        private final Object lock;
        //private final Semaphore lock;

        public HelloThread(Semaphore lock) {
            this.lock = lock;
        }

        public …
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

如何打破在while循环中继续嵌套for循环?

我可以在嵌套的for循环中使用break来返回外部while循环并continue从for循环中使用以强制while循环继续运行吗?我无法将for循环条件纳入我的while循环条件,因此如果我无法继续特定的满足情况,则while循环可能会停止.

while(...some conditions...){
    ...print stuff
    for(...some instances are arrays, condition loop array...){
        if(...index meets conditions...){
            ...print once, some arrays might meet condition on multiple index
            break; //to prevent multiple printings
        }
    continue; //i don't want to force another while iteration if(false)
    //or is this continue for(loop) anyway?
    }
continue; //is this not essentially a while(true) loop with no return?
}
Run Code Online (Sandbox Code Playgroud)

我无法将for循环条件纳入while条件的原因是因为如果没有传入数组,则需要调用两个循环之间的条件if(array == null)和if条件.x == true getArray()如果大多数情况下条件yz打印来自while-loop但有时x满足条件所以我需要for循环.这是在for-loop打印之后,if(index true)) …

java arrays loops break

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

编写一个Raytracer,并且无法使图像正确居中?

我正在用Java编写一个Raytracer,我已经达到了可以创建对象,光线,测试交叉点然后着色像素的程度.我也做了一些基本的抗锯齿.我的问题是,如果创建一个球体,这应该是世界上(即中心0.0,0.0,0.0),然后绘制图像,我结束了这样的画面.

当红色圆圈应该在图像的中间.

主要方法

public static void main(String[] args) {
    System.out.println("Rendering...");
    long start = System.nanoTime();

    // Setting up the size of the image to be rendered
    world = new World(1920, 1080, 1.0);
    image = new Image("image.png");
    sampler = new SimpleSampler(4);
    projector = new OrthographicProjector();

    // Main loop of program, goes through each pixel in image and assigns a colour value
    for (int y = 0; y < world.viewPlane.height; y++) {
        for (int x = 0; …
Run Code Online (Sandbox Code Playgroud)

java raytracing

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

如何在java文件中运行scala jar文件?

我需要从 Java 代码运行 Scala jar 文件:

所以,如果我有这个 Scala 代码:

object test extends App {
    override def main(args: Array[String]) {
        println("Hello, world! " + args.toList)
    }
}
Run Code Online (Sandbox Code Playgroud)

我导出了demo.jar,我想从Java应用程序内部执行?我使用它的方式是抛出运行时进程,这对我不起作用?

import java.io.InputStream;
import org.apache.commons.io.IOUtils;

public class testscalajar {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"scala","-cp","d:\\demo.jar", "test"});
            // Then retrieve the process output
            InputStream in = proc.getInputStream();
            InputStream err = proc.getErrorStream();
            String inString = IOUtils.toString(in, "UTF8"); 
            String errString = IOUtils.toString(err, "UTF8"); …
Run Code Online (Sandbox Code Playgroud)

java scala

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

返回一个范围内的所有数字

function sumAll(arr) {
    var list = [];

    for (var i = arr[0]; i <= arr[1]; i++) {
        list.push(i);
    }

    var total = list.reduce(function(a, b) {
        return a + b;

    });

    return total;
}

sumAll([10, 5]);
//sumAll([1, 4]);  //returns 10
//sumAll([5, 10]); //returns 45
//sumAll([4, 1]);
Run Code Online (Sandbox Code Playgroud)

我需要对给定参数之间的每个数字求和.为sumAll([1, 4])sumAll([5, 10]).代码将通过,因为它会在两个参数之间创建所有数字并添加它.但是,对于sumAll([10, 5])sumAll([4, 1]),因为更大的数字是第一个参数,我相信它不会运行var list.我尝试.sort()在两者之间使用方法,以便对数字进行排序但无法使其工作.我如何使用Math.min()Math.max()使用此代码?

javascript sorting reduce

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

Javascript 将 Unicode 八进制字节转换为文本

我正在尝试使用 Javascript 将一系列八进制字节转换为文本,如下所示:

\n\n

输入是 \\330\\265 输出应该是 \xd8\xb5

\n\n

以下工具成功地做到了这一点:

\n\n\n\n

我正在尝试复制这个逻辑

\n

javascript unicode encoding character-encoding octal

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

chrome:// extensions页面中的访问扩展

这是我的mainfest.json

"content_scripts": [ {
    "all_frames": true,
    "css": [ "css/event.css" ],
    "matches": [ "\u003Call_urls>" ],
    "run_at": "document_start"
}
Run Code Online (Sandbox Code Playgroud)

但我无法在chrome://extensions/页面
帮助中找到内容脚本!!!

javascript google-chrome-extension

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

在Java中实现等价

正如您可能从我的代码中看到的那样,我对此很新.我正在尝试编写一个简单的程序来检索一个人的朋友.这是我的代码:

public class Person {
    private String name;
    private String friends;

    public Person(String aName) {
        name = aName;
        friends = "";
    }

    public String getFriends() {
        return friends;
    }

    public void addFriend(Person friend) {
        friends = friends + " " + friend.name; 
    }

    public void unfriend(Person nonFriend) {
        friends = friends.replace(" " + nonFriend.name, "");
    }

    public static void main(String[] args) {
        Person dana = new Person("Dana");
        Person gina = new Person("Gina");
        Person john = new Person("John");

        dana.addFriend(gina);
        dana.addFriend(john);
        john.addFriend(gina); …
Run Code Online (Sandbox Code Playgroud)

java equivalence

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

使用 JavaScript 将 CSS 更改一次性应用到整个类

我想使用 JavaScript 更改类中的所有元素。

目前它看起来像这样:

var elements = document.getElementsByClassName(classToChange);

for (var i = 0; i < elements.length; i++) {
    elements[i].style.fontFamily = newFont;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,有没有办法立即将新的 CSS 属性应用于整个类(无需制定新的 CSS 样式规则),或者只能通过单独循环元素来完成?

javascript css

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

强制ESLint使用ES6

我有我的ESLint所有设置和工作,但我希望它在我不使用ES6之类的东西时抛出错误let,const或箭头函数(=>).

.eslintrc

{
  "env": {
    "node": true,
    "es6": true,
    "mocha": true
  },
  "rules": {
    "semi": 2
  },
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "script",
    "ecmaFeatures": {
      "arrowFunctions": true,
      "binaryLiterals": true,
      "blockBindings": true,
      "classes": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

目前,这不会抛出错误:

main.js

var stars = [];
var speed = 20;

function setup() {
  createCanvas(windowWidth, windowHeight);

  // Create 1000 stars
  for (var i = 0; i < 1000; i++) {
    stars.push(new Star());
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6 eslint

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