小编JaA*_*nTr的帖子

用随机数填充数组

我需要使用构造函数创建一个数组,添加一个方法来将数组作为一个序列打印,一个方法用double类型的随机数填充数组.

这是我到目前为止所做的:

import java.util.Random;


public class NumberList {


    private static double[] anArray;

    public static double[] list(){
        anArray = new double[10];   
        return anArray;
    }

    public static void print(){
        for(double n: anArray){
        System.out.println(n+" ");
        }
    }


    public static double randomFill(){

    Random rand = new Random();
    int randomNum = rand.nextInt();
    return randomNum;
    }

    public static void main(String args[]) {

    }


}
Run Code Online (Sandbox Code Playgroud)

我正在努力弄清楚如何使用randomFill方法中生成的随机数填充数组.谢谢!

java arrays

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

无法使用brew开关更改节点版本

我正在尝试使用节点8.9.1但是当尝试使用brew进行更新时,它说已经安装了8.9.1

James-MacBook:~ james$ brew upgrade node
Error: node 8.9.1 already installed
Run Code Online (Sandbox Code Playgroud)

但是在检查它显示的版本时

James-MacBook:~ james$ node --version
    v0.10.48
Run Code Online (Sandbox Code Playgroud)

那么我试着切换到8.9.1

James-MacBook:~ james$ brew switch node 8.9.1
Cleaning /usr/local/Cellar/node/8.9.1
7 links created for /usr/local/Cellar/node/8.9.1
Run Code Online (Sandbox Code Playgroud)

但它仍然显示为0.10.48

James-MacBook:~ james$ node --version
v0.10.48
Run Code Online (Sandbox Code Playgroud)

homebrew node.js

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

从文本文件中读取行,反向并保存在新的文本文件中

到目前为止,我有这个代码:

 f = open("text.txt", "rb")
 s = f.read()
 f.close()
 f = open("newtext.txt", "wb")
 f.write(s[::-1])
 f.close()
Run Code Online (Sandbox Code Playgroud)

原始文件中的文本是:

This is Line 1
This is Line 2
This is Line 3
This is Line 4
Run Code Online (Sandbox Code Playgroud)

当它反转并保存它时,新文件如下所示:

 4 eniL si sihT 3 eniL si sihT 2 eniL si sihT 1 eniL si sihT
Run Code Online (Sandbox Code Playgroud)

当我希望它看起来像这样:

 This is line 4
 This is line 3
 This is line 2
 This is line 1
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

python python-2.7

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

画布洪水填充未填充到边缘

我正在使用洪水填充算法来填充画布上绘制的圆圈。我遇到的问题是算法没有填充到圆的边缘。

这是基于这篇博客文章的算法:

function paintLocation(startX, startY, r, g, b) {
    var colorLayer = context1.getImageData(0, 0, canvasWidth, canvasHeight);
    pixelPos = (startY * canvasWidth + startX) * 4;

    startR = colorLayer.data[pixelPos];
    startG = colorLayer.data[pixelPos + 1];
    startB = colorLayer.data[pixelPos + 2];

    var pixelStack = [
        [startX, startY]
    ];

    var drawingBoundTop = 0;
    while (pixelStack.length) {
        var newPos, x, y, pixelPos, reachLeft, reachRight;
        newPos = pixelStack.pop();
        x = newPos[0];
        y = newPos[1];

        pixelPos = (y * canvasWidth + x) * 4;
        while (y-- …
Run Code Online (Sandbox Code Playgroud)

html javascript canvas flood-fill

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

按字母顺序排序,计算每个单词出现的次数并打印在表格中

我正在努力解决问题的表格部分中的印刷品.到目前为止,我已设法按字母顺序排序用户输入的句子并计算每个单词出现的次数.这是代码:

thestring = (raw_input())
sentence = thestring.split(" ")
sentence.sort()

count = {}
for word in thestring.split():
    try: count[word] += 1
    except KeyError: count[word] = 1

print sentence
print count
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,我得到了这个:

['apple', 'apple', 'banana', 'mango', 'orange', 'pear', 'pear', 'strawberry']
{'apple': 2, 'pear': 2, 'strawberry': 1, 'mango': 1, 'orange': 1, 'banana': 1}
Run Code Online (Sandbox Code Playgroud)

但是,理想情况下,我希望它打印在一个看起来像这样的表中:

apple.....|.....2
banana....|.....1
mango.....|.....1
orange....|.....1
pear......|.....2
strawberry|.....1
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

python python-2.7

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

编写一个函数将十六进制转换为十进制

我需要编写一个将十六进制转换为十进制的函数,我已经知道如何做到这一点,但我已经陷入了第一位.到目前为止,我正在接受用户输入并以十六进制形式返回,但这只有在我一次输入1个内容时才有效.代码是:

def hex(x):
    if x == "0":
        return 0
    elif x == "1":
        return 1
    elif x == "2":
        return 2
    elif x == "3":
        return 3
    elif x == "4":
        return 4    
    elif x == "5":
        return 5
    elif x == "6":
        return 6
    elif x == "7":
        return 7
    elif x == "8":
        return 8
    elif x == "9":
        return 9
    elif x == "A":
        return 10
    elif x == "B":
        return 11
    elif x == "C":
        return …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

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

ng-if在ng-repeat内总是返回true

我正在创建一个包含英超联赛数据的表格.我试图用来ng-if检查主队是否赢了,如果是这样的话加一个单元说"主队赢了"

HTML

<div ng-if="data.full_time_result: === 'H'">
    <td>Home Team Won!</td>
</div>
Run Code Online (Sandbox Code Playgroud)

然而,这增加了"主队赢了!" 即使full_time_result不是'H',也是每一行.

这是plnkr的链接.

此外,实现此功能的推荐方法是什么?有很多ng-if块可能不是最好的方法.

javascript html-table angularjs angularjs-ng-if

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

无法从静态上下文java引用非静态变量

我知道这个问题已经被问了很多次,而且我已经阅读了很多答案,但我无法理解如何解决我的问题.这是我的代码:

public class Circle
{
    public int diameter;
    public int xPosition;
    public int yPosition;
    public String color;


    public Circle()
    {
        diameter = 30;
        xPosition = 20;
        yPosition = 60;
        color = "blue";
    }

    public void toString()
    {
        System.out.println("The diameter of the circle is " + Circle.diameter);
        System.out.println("The x position of the circle is " + Circle.xPosition);
        System.out.println("The y position of the circle is " + Circle.yPosition);
        System.out.println("The colour of the circle is " + Circle.color);
    }


   public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java

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