我对使用 JavaScript 编写和开发基于文本的 RPG 游戏还比较陌生。下面的代码让我能够逐步了解不同的场景,在这些场景中,你会遇到不同的坏人。
我将 For 循环与 Switch 语句结合使用,并让它先工作,但后来我重构了我的代码,使其更加面向对象和原型化。现在我的 For 循环继续循环并且不退出。我自始至终检查了 [i] 的值,发现它正确地变为 0-4,但随后它从 0 重新启动,我不明白为什么?
var scenario = new Array();
//simple function to create the number of scenarios
function Scenario () {
howManyScenarios = function(number) {
for (i=0; i <= number; i++) {
scenario[i] = ("Scenario " + (1 + i));
};
};
howManyScenarios(4); //if you change the argument, add additional switch cases
//iterating through my howManyScenarios function to build out scenarios using a switch case
createScenarios = …Run Code Online (Sandbox Code Playgroud) 我在 C 中有 switch case 语句的代码,如下所示。
float value_calc ( Type_t type, void *in_data)
{
switch ( type)
{
case 0:
{
int * data = (int*)in_data;
}
break;
case 1:
{
float * data = (float*)in_data;
}
break; <-- error :- data: redefinition; different basic types.
case 3:
{
double * data = (double*)in_data;
}
break;
default:
return 0;
}
for (int i =0; i< 1024; i++)
{
float capture = * (data + i) + diff; <-- error identifier …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序来从另一个程序读取和写入内存,但是有两个不同的版本,并且内存值在一个版本中具有偏移量,但在第二个版本中没有,因此取决于用户选择的设置(v1 或 v2 ) 我需要确定要读/写的地址。
由于我的代码如下所示,但我觉得这不是很有效,并且在真正不需要时复制相同的代码会使类变得很长。
有没有更有效的方法来做这一切:
public static int Property
{
get
{
switch (Version)
{
case Version.v1:
return Memory.ReadMemory<int>(0xB7CE50 + 0x2680);
case Version.v2:
return Memory.ReadMemory<int>(0xB7CE50);
default:
return 0;
}
}
set
{
switch (Version)
{
case Version.v1:
Memory.WriteMemory<int>(0xB7CE50 + 0x2680, value);
break;
case Version.v2:
Memory.WriteMemory<int>(0xB7CE50, value);
break;
default:
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并非所有地址都需要这个偏移量,但大多数都需要,所以我也需要考虑到这一点,因此如果 v2,我不能只添加 0x2680 值
我在下面发布的代码是我整个程序的一部分。其余代码与此问题无关。除了使用这个长switch语句之外还有其他解决方案吗?主要目标是缩短代码。该if语句仅用于将小写字母大写。
int POINTS[] = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 };
int compute_score(string word)
{
// TODO: Compute and return a score for string
int word_length = strlen(word);
int i, sum = 0;
for (i = 0; i < word_length; i++)
{
//This if statement is used to captalise lower-case letters.
if (islower(word[i]))
{
word[i] …Run Code Online (Sandbox Code Playgroud) 给出以下代码:
object value = header.DataContentType switch
{
DataContentType.DOUBLE => (double)BitConverter.ToDouble(currentValueBytes.ToArray()),
DataContentType.FLOAT => (float)BitConverter.ToSingle(currentValueBytes.ToArray()),
DataContentType.BYTE => (byte)contentData[i],
DataContentType.SHORT => (short)BitConverter.ToInt16(currentValueBytes.ToArray()),
DataContentType.INTEGER => (int)BitConverter.ToInt32(currentValueBytes.ToArray()),
DataContentType.LONG => (long)BitConverter.ToInt64(currentValueBytes.ToArray()),
_ => throw new InvalidDataException("Invalid data type"),
};
Run Code Online (Sandbox Code Playgroud)
现在,在header.DataContentTypeis 的情况下DataContentType.INTEGER,该值被分配为 double tovalue而不是 anint并(int)value导致 InvalidCastException
如果我调试,我可以清楚地看到它进入INTEGER案例,如果我BitConverter.ToInt32(currentValueBytes.ToArray())在调试控制台中评估,我会返回一个整数。然而,只要 switch case 退出,变量value的类型就是 double。
此外,如果我手动执行value = BitConverter.ToInt32(currentValueBytes.ToArray())变量是正确的类型int。由于某种奇怪的原因,暗示 switch 语句必须将类型更改为 double 。
我希望 switch case 返回返回的任何类型BitConverter。如何使BitConverter返回正确 BitConverter …
我试图逐行解释这个汇编代码在做什么,但是当我看到这个汇编中的跳转表时,我发现自己真的很困惑。这是从教科书练习题 3.63 中获取的,但没有解释 -因此,为什么我在这里问它。目标是对提供的汇编列表进行逆向工程并编写可以生成它的 C 代码(感觉 switch 语句体)。请帮忙 :(
教科书是: Randal E. Bryant, David R. O'Hallaron - 计算机系统。程序员的观点 [第 3 版](2016 年,Pearson)
qn 3.63
long switch_prob(long x, long n) {
long result = x;
switch(n) {
/* Fill in code here */
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何“解码”它或如何知道它指向哪里。
0000000000400590 <switch_prob>:
400590: 48 83 ee 3c sub $0x3c,%rsi
400594: 48 83 fe 05 cmp $0x5,%rsi
400598: 77 29 ja 4005c3 <switch_prob+0x33>
40059a: ff 24 f5 f8 06 40 00 jmpq *0x4006f8(,%rsi,8)
4005a1: …Run Code Online (Sandbox Code Playgroud) JEP 361引入了 switch 表达式,减少了 switch 常用用法的冗长。
作为此功能的一个特定细节,default不需要为枚举上的 switch 表达式编写类。以下代码编译:
public enum MyEnum {
ONE,
TWO;
}
public class OtherClass {
public static String computeAttribute(MyEnum myEnum) {
return switch (myEnum) {
case ONE -> "one";
case TWO -> "two";
};
}
}
Run Code Online (Sandbox Code Playgroud)
目前 switch 表达式是详尽的,但如果单独重新编译枚举,则枚举的新版本可能包含THREEswitch 语句没有预料到的另一个条目(例如)。
JEP 和这篇文章都提到编译器插入了一个默认子句,该子句会引发异常以表明这种不和谐。
我找不到任何说明抛出的异常的确切类型的内容。编译器在枚举值的 switch 表达式中插入的默认子句抛出的异常是什么?
我正在尝试为我的开关箱考虑任何替代方案,因为它很长我嵌套的开关箱看起来像这样
switch (currentTab) {
case pending:
switch (status) {
case approved:
case denied:
break;
case partialApproved:
finalUpdatedInvoiceAmount = updatedInvoiceAmount - (feeAmount - (approvedFeeAmount || 0));
break;
}
break;
case approved:
switch (status) {
case denied:
case partialApproved:
finalUpdatedInvoiceAmount = updatedInvoiceAmount - (feeAmount - (approvedFeeAmount || 0));
break;
}
break;
case denied:
switch (status) {
case approved:
case partialApproved:
finalUpdatedInvoiceAmount = oldApprovedFeeAmount ? updatedInvoiceAmount + (feeAmount - oldApprovedFeeAmount) : updatedInvoiceAmount - (feeAmount - (approvedFeeAmount || 0));
break;
}
break;
case partiallyApprovedTab: …Run Code Online (Sandbox Code Playgroud) javascript object-literal switch-statement typescript angular
我读到 switch 情况下的初始化应该给出编译器错误。但是当我尝试了两个不同的版本时。一个是没有给出错误(Ex1),我不明白为什么。
例 1:
switch (i)
{
case 1:
int k;
break;
case 2:
int j=3;
break;
}
Run Code Online (Sandbox Code Playgroud)
例2:
switch (i)
{
case 1:
int k=3;
break;
case 2:
int j;
break;
}
Run Code Online (Sandbox Code Playgroud) Select-String -Pattern返回每个匹配的位置。例如:
$s = 'abcxyzabc' | Select-String -Pattern 'abc(?<x>.*)abc'
$s.Matches[0].Groups | Select-Object Name, Value, Index, Length
Run Code Online (Sandbox Code Playgroud)
给出:
Name Value Index Length
---- ----- ----- ------
0 abcxyzabc 0 9
x xyz 3 3
Run Code Online (Sandbox Code Playgroud)
whereIndex和Length指定每个匹配的位置。
但是,在使用switch -regex. 例如:
switch -regex ('abcxyzabc') {
'abc(?<x>.*)abc' {
$matches
}
}
Run Code Online (Sandbox Code Playgroud)
给出:
Name Value
---- -----
x xyz
0 abcxyzabc
Run Code Online (Sandbox Code Playgroud)
我找不到像什么Index,并Length在拿到了赛位置$matches。
我还检查该类型Matches的返回Select-String -Pattern是System.Text.RegularExpressions.Match[],虽然类型$matches中switch …
switch-statement ×10
c ×3
c# ×2
javascript ×2
.net-core ×1
angular ×1
assembly ×1
c++ ×1
casting ×1
for-loop ×1
java ×1
jump-table ×1
powershell ×1
properties ×1
regex ×1
typescript ×1
x86 ×1