小编Cha*_*Han的帖子

运行程序给出了与调试模式不同的结果

我提供了一个简单的代码,它将输出10个随机数字,介于0和100之间.当我在Visual Studio(C#)中使用F5运行时,我获得相同的数字10次.但是,如果我通过调试模式,使用F10或F11逐行运行它,我得到10个不同的数字(可能不是全部不同,但它们是随机的).

public static void rand() {
    for (int j = 0; j < 10; j++) {
        Random r = new Random();
        Console.WriteLine( r.Next(100));
    }   
}
Run Code Online (Sandbox Code Playgroud)

我知道如何解决这个问题,这是通过在循环外部实例化Random r并通过引用进行复制,但我想了解为什么会发生这种情况.我认为这与种子有关但程序在调试模式下运行时会起作用,这让我很困惑.

此外,现在我正在质疑我是否总是需要测试调试模式是否给我正确的结果.

c# random debugging

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

将数据从 Phoenix 传递到 Javascript 的最佳实践

我看到了 3 种方法。

  1. 使用<%= %>内部<script>*.html.eex
  2. 使用通道将数据传递给 Javascript
  3. 构建一个 json api

#1 似乎是最简单的,但我还没有找到或想出一个好的方法来做到这一点。

注意:实时更新不是我的要求。

elixir phoenix-framework

8
推荐指数
2
解决办法
4343
查看次数

如何为ueberauth身份进行单独的注册回调

ueberauth_identity有一个用于登录和注册的单个回调的示例,但这会导致错误处理。有没有办法进行单独的注册回调?

elixir phoenix-framework ueberauth

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

自动选择和创建类对象

让我们说我们有一个盒子类:

class Box
{
    private int width;
    private int height;

    //Box Constructor
    public Box( int height )
    {
        this.height = height;
        width = 450;
    }

}
Run Code Online (Sandbox Code Playgroud)

和我们主要的一系列Box对象:

Box Box1 = new Box(147);
Box Box2 = new Box(178);
Box Box3 = new Box(784);
Run Code Online (Sandbox Code Playgroud)

有没有办法使用"for"循环来浏览这些对象?另外,您如何让计算机为我们创建类对象?例如.创建10个对象:

for( int i=0; i>10; i++)
{
    //method
}
Run Code Online (Sandbox Code Playgroud)

c# java oop object

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

为什么这个while循环卡住了?

我刚刚编写了一个简单的c#代码来计算数字的阶乘,但程序却被卡在姓氏上.有人为什么会被卡住?

谢谢,

OMIN

using System;

//1. Write a program which finds the factorial of a number entered by the user.

namespace Beginner1{

class ProblemOne
{
    static void Main (string[] args)
    {
        bool play = true;
        while ( play ) {
            Console.Write ("Type in the number you would like to find Factorial of: ");
            int num = Convert.ToInt16( Console.ReadLine ());
            int sum = 1;
            for (int i = 2; i <= num; i++) {
                sum = sum * i;
            }
            Console.WriteLine …
Run Code Online (Sandbox Code Playgroud)

c# while-loop do-while

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