试图在C#中使用WinForm进行简单的杀菌游戏,但细菌(我Panel暂时使用)似乎并没有随意移动.
具体来说,我遇到的问题是,细菌试图向左上角移动并仅在那里移动.理想情况下,细菌需要均匀地在矩形范围内移动,但我不确定如何实现.看看下面的gif文件.

如您所见,红色Panel仅在左上角移动.我怎样才能让它随意均匀地随处移动?
这是我的代码:
private Panel _pnlBacteria; //Panel representing a piece of bacteria
private Random r = new Random(); //For randomly-generated values
private int _prevX; //Stores the previous X location
private int _prevY; //Stores the previous Y location
public Form1()
{
InitializeComponent();
_pnlBacteria = new Panel();
/* Get more property assignments to this._pnlBacteria (omitted) */
//Bacteria's start position is also randomly selected
_prevX = r.Next(50, 300);
_prevY = r.Next(50, 500);
}
//Timer runs every …Run Code Online (Sandbox Code Playgroud) 大家好我现在正在使用.net核心2.1.3当我通过"dotnet new webapi"安装webapi项目时,当我尝试通过firefox或chrome打开时它给了我这个错误
HttpsConnectionAdapter [1]无法验证HTTPS连接.System.IO.IOException:身份验证失败,因为远程方已关闭传输流.
我认为F#意味着比C#更快,我做了一个可能不好的基准测试工具,C#得到16239ms,而F#做得更差,为49583ms.有人可以解释为什么会这样吗?我正在考虑离开F#并回到C#.是否可以通过更快的代码在F#中获得相同的结果?
这是我使用的代码,我尽可能地使它成为平等.
F#(49583ms)
open System
open System.Diagnostics
let stopwatch = new Stopwatch()
stopwatch.Start()
let mutable isPrime = true
for i in 2 .. 100000 do
for j in 2 .. i do
if i <> j && i % j = 0 then
isPrime <- false
if isPrime then
printfn "%i" i
isPrime <- true
stopwatch.Stop()
printfn "Elapsed time: %ims" stopwatch.ElapsedMilliseconds
Console.ReadKey() |> ignore
Run Code Online (Sandbox Code Playgroud)
C#(16239ms)
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{ …Run Code Online (Sandbox Code Playgroud) 这个C#代码发生了什么?我甚至不确定它为什么编译.具体来说,它在设置Class1Prop尝试使用对象初始化器语法的地方发生了什么?它似乎是无效的语法,但它在运行时编译并产生一个空引用错误.
void Main()
{
var foo = new Class1
{
Class1Prop =
{
Class2Prop = "one"
}
};
}
public class Class1
{
public Class2 Class1Prop { get; set; }
}
public class Class2
{
public string Class2Prop { get; set; }
}
Run Code Online (Sandbox Code Playgroud) Console.WriteLine("Enter the cost of the item");
string input = Console.ReadLine();
double price = Convert.ToDouble(input);
Run Code Online (Sandbox Code Playgroud)
您好,我希望禁用键盘按钮,AZ,支架,问号等.我想要它,如果你输入它,它将不会出现在控制台中.我只希望数字1-9出现.这是在C#Console应用程序中.谢谢您的帮助!
我在C#中很新,我在字符串比较中发现了一些我不太懂的东西.
有人可以解释一下为什么字符之间的比较给出了与下面代码中一个字符长度字符串的比较相反的结果吗?
我预计这"9" < "="将是true(因为'9'(57)的unicode代码比'='(61)的unicode代码少但是它是假的...后面的字符串的比较逻辑是什么,为什么它不同于比较字符?
码:
bool resChComp = '9' < '=';
bool resStrComp = String.Compare("9", "=") < 0;
Console.WriteLine($"\n'9' < '=' : {resChComp}, \"9\" < \"=\" : { resStrComp }");
Run Code Online (Sandbox Code Playgroud)
输出:
'9' < '=' : True, "9" < "=" : False
Run Code Online (Sandbox Code Playgroud) 我正在使用 google play 开发者 API,因此在用户购买产品后,我想在服务器端获取该购买的详细信息,因此我使用 google play 开发者 api 来获取该信息,但不幸的是,该 API 不会返回任何信息用户支付了多少费用(价格和货币)的信息 API 仅返回以下信息
{
"kind": "androidpublisher#productPurchase",
"purchaseTimeMillis": long,
"purchaseState": integer,
"consumptionState": integer,
"developerPayload": string,
"orderId": string,
"purchaseType": integer
}
Run Code Online (Sandbox Code Playgroud)
如何通过 google play api 获取购买产品的价格?
我有以下方法:
protected T AttachComponent<T>(){
T gsComponent = gameObject.GetComponent<T>();
if(gsComponent == null){
gsComponent = gameObject.AddComponent<T>();
}
return gsComponent;
}
Run Code Online (Sandbox Code Playgroud)
在AddComponent行我收到以下错误:
类型'T'不能用作泛型类型或方法'GameObject.AddComponent()'中的类型参数'T'.从'T'到'UnityEngine.Component'没有装箱转换或类型参数转换.
我不知道我能做些什么来解决这个错误,为什么我不能这样做?
阅读斯蒂芬克莱里关于消除异步的博客文章并等待我决定去玩它.我用HttpClient使用Visual Studio For Mac编写了非常简单的控制台应用程序.
public static async Task Main(string[] args)
{
Console.WriteLine(await Get());
Console.WriteLine("Hello World!");
}
public static Task<string> Get()
{
using (var http = new HttpClient())
return http.GetStringAsync("http://google.com");
}
Run Code Online (Sandbox Code Playgroud)
根据博客文章,它应该抛出异常,但事实并非如此.如果我切换到Windows并尝试运行此应用程序,我将按预期获得TaskCancelledException,但在macOS上它完全正常.
证明Google.com在macOS上毫无例外地打印到控制台中
我相信这种行为背后的原因是两个平台上HttpClient中IDisposable的不同实现,但是......为什么?
通过使用以下URL请求ICloud API并登录
string url = "https://setup.icloud.com/setup/ws/1/login?clientBuildNumber=1P24&clientId=" + RANDOM_GUID;
Run Code Online (Sandbox Code Playgroud)
从上面的url响应获取服务器url和dsid.使用以下代码进一步请求联系人列表
var localUrl2 = "https://p66-contactsws.icloud.com/co/startup?clientBuildNumber=1P24&clientId=" + RANDOM_GUID + "&clientVersion=2.1&dsid=" + dsid + "&locale=en-EN&order=last%2Cfirst";
var webRequest2 = (HttpWebRequest)WebRequest.Create(localUrl2);
webRequest2.Method = "GET";
webRequest2.Headers.Set("Origin", "https://www.icloud.com");
// get the X-APPLE-WEBAUTH-TOKEN and X-APPLE-WEBAUTH-USER from webResponse.Headers.SetCookie
webRequest2.Headers.Set("X-APPLE-WEBAUTH-TOKEN", XXXXXXX);
webRequest2.Headers.Set("X-APPLE-WEBAUTH-USER", XXXXXXXX);
WebResponse webResponse2 = webRequest2.GetResponse();
Run Code Online (Sandbox Code Playgroud)
仍然有例外
远程服务器返回错误:(421)错误请求.
请告诉我上面的代码有什么问题.