我提供了一个简单的代码,它将输出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并通过引用进行复制,但我想了解为什么会发生这种情况.我认为这与种子有关但程序在调试模式下运行时会起作用,这让我很困惑.
此外,现在我正在质疑我是否总是需要测试调试模式是否给我正确的结果.
我看到了 3 种方法。
<%= %>内部<script>的*.html.eex #1 似乎是最简单的,但我还没有找到或想出一个好的方法来做到这一点。
注意:实时更新不是我的要求。
ueberauth_identity有一个用于登录和注册的单个回调的示例,但这会导致错误处理。有没有办法进行单独的注册回调?
让我们说我们有一个盒子类:
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#代码来计算数字的阶乘,但程序却被卡在姓氏上.有人为什么会被卡住?
谢谢,
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)