小编Moo*_*ors的帖子

在C中生成随机数

在搜索CI中生成随机数的教程时发现了这个主题

当我尝试使用rand()没有参数的函数时,我总是得到0.当我尝试使用rand()带参数的函数时,我总是得到值41.每当我尝试使用arc4random()random()函数时,我都会收到LNK2019错误.

这就是我所做的:

#include <stdlib.h>
int main()
{
  int x;
  x = rand(6);
  printf("%d", x);
}
Run Code Online (Sandbox Code Playgroud)

这段代码总是生成41.我哪里错了?我正在运行Windows XP SP3并使用VS2010命令提示符作为编译器.

c random

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

如何计算存储在*.ser文件中的对象数

我正在尝试读取存储在*.ser文件中的所有对象,并将它们存储在一个对象数组中.如何获取存储在该文件中的对象数(这样我可以声明数组很number_of_objects长)?

我检查了API,但无法找到理想的功能.

-edit-
部分代码:

Ser[] objTest2 = new Ser[number_of_objects];
for(int i=0; i<=number_of_objects, i++) {
    objTest2[i] = (Ser)testOS2.readObject();
    objTest2[i].printIt(); 
}
Run Code Online (Sandbox Code Playgroud)

java serialization

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

拆分文本时遇到问题

这是我正在使用的代码:

public class splitText {
public static void main(String[] args) {
    String x = "I lost my Phone. I shouldn't drive home alone";
    String[] result = x.split(".");
    for (String i : result) {
        System.out.println(i);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

编译完美,但在运行时没有任何反应.我究竟做错了什么?

java split

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

帮助第一个网络程序

这是代码.

public class testClient {
public static void main(String[] args) {
    testClient abc = new testClient();
    abc.go();
}
public void go() {
    try {
        Socket s = new Socket("127.0.0.1",  5000);
        InputStreamReader  sr = new InputStreamReader(s.getInputStream());
        BufferedReader reader = new BufferedReader(sr);
        String x = reader.readLine();
        System.out.println(x);
        reader.close();
    } catch(IOException ex) {
        ex.printStackTrace();
    }
  }
}

public class testServer {
public static void main(String[] args) {
    testServer server = new testServer();
    server.go();
}
public void go() {
    try {
        ServerSocket s = …
Run Code Online (Sandbox Code Playgroud)

java networking

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

Python:带有装饰器的函数的 __qualname__

我在另一个类的实例方法中使用装饰器(类),如下所示:

class decorator_with_arguments(object):

    def __init__(self, arg1=0, arg2=0, arg3=0):
        self.arg1 = arg1
        self.arg2 = arg2
        self.arg3 = arg3

    def __call__(self, f):
        print("Inside __call__()")
        def wrapped_f(*args):
            print(f.__qualname__)
            f(*args)
        return wrapped_f

class Lol:
    @decorator_with_arguments("hello")
    def sayHello(self,a1, a2, a3, a4):
        print(self.sayHello.__qualname__)
Run Code Online (Sandbox Code Playgroud)

现在,当我打印出来时,self.sayHello.__qualname__它会打印decorator_with_arguments.__call__.<locals>.wrapped_f

有什么办法可以覆盖这个吗?我想在这里看到Lol.sayHello(我的原始函数的全称)。

我尝试覆盖@property __qualname__of __call__(使用静态字符串);没用。

python decorator python-3.x python-decorators

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

帮助二维数组

首先,初学者在这里.

我正在使用此代码.

class MDArrays {
    public static void main(String[] args) {
        int[][] x;
        int t=2;
        x = new int[2][3];

        for(int i=0; i<=1; i++) {
            for(int j=0; i<=2; j++) {
                x[i][j] = t;
                t += 2;
                System.out.println(x[i][j]);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它编译得很完美,但在运行时,正确显示3个数字后,我收到以下错误.

Exception in thread "main" java.Lang.ArrayindexOutOfBoundsException : 3 at MDArrays.main(MDArrays.java:13)

我哪里错了?

java arrays

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

帮助第一个Polymorphism类

我正在创建一些随机类来更好地理解多态性.编码如下:

Poly1:

public abstract class Poly1 {
    int comPoly;
}
Run Code Online (Sandbox Code Playgroud)

SubPoly1:

public class SubPoly1 extends Poly1 {
    String testPoly;
}
Run Code Online (Sandbox Code Playgroud)

SubPoly2:

public class SubPoly2 extends Poly1 {
    int x;
}
Run Code Online (Sandbox Code Playgroud)

testPoly:

public class testPoly {
public static void main(String[] args) {
    Poly1[] testObj = new Poly1[2];
    testObj[0] = new SubPoly1();
    testObj[1] = new SubPoly2();
    testObj[1].x = 1;
    testObj[1].comPoly = 2;
    System.out.println("Test Output : " + testObj[1].x+ " and " + testObj[1].comPoly);
    testObj[0].testPoly = "Hello";
    testObj[0].comPoly = …
Run Code Online (Sandbox Code Playgroud)

java polymorphism

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

异常已经发现错误

这是代码:

public class Exc {
int x = 2;
public void throwE(int p) throws Excp, Excp2 { 
    if(x==p) {
        throw new Excp();
    }
    else if(x==(p+2)) {
        throw new Excp2();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是处理程序代码:

public class tdExc {
public static void main(String[] args) {
    Exc testObj = new Exc();
    try {
        testObj.throwE(0);
        System.out.println("This will never be printed, so sad...");
    } catch(Exception Excp) {
        System.out.println("Caught ya!");
    } catch(Exception Excp2) {
        System.out.println("Caught ya! Again!!!!");
    } finally {
        System.out.println("This will always be printed!");
    } …
Run Code Online (Sandbox Code Playgroud)

java exception-handling

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

第一个异常程序

这是我做过的第一个异常编码,猜测是什么,它产生了一个错误.伤心.

public class Exc {
int x = 2;
public void throwE(int p) throws Excp { 
    if(x==p) {
        throw new Excp();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我认为我不需要发布处理程序代码,因为即使这个类没有通过编译器.

我得到错误cannot find symbolExcp.我完全按照书.有什么我想念的吗?

java exception

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

无法停止线程

这是我正在使用的代码.

public class timerApp {
Runnable timerRun = new runX();
Thread thread1 = new Thread(timerRun);
public static void main(String[] args) {
    timerApp xyz = new timerApp();
    xyz.createGUI();
}
public void createGUI() {
    JButton button = new JButton("Start timer");
    JButton button2 = new JButton("Stop timer");
    JFrame frame = new JFrame();
    JLabel label = new JLabel("under_construction");
    frame.getContentPane().add(BorderLayout.NORTH,button);
    frame.getContentPane().add(BorderLayout.SOUTH,button2);
    frame.getContentPane().add(BorderLayout.CENTER,label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setVisible(true);
    button.addActionListener(new b1L());
    button2.addActionListener(new b2L());
}
public class b1L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        thread1.start();
    }
}
public …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
2
解决办法
691
查看次数