我有两个枚举类,比如Enum1和Enum2:
enum Enum1 {ONE, TWO, THREE}
enum Enum2 {FOUR, FIVE}
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的方法:
public <E extends Enum<E>> method (E arg) {
switch (arg) { // Here is the compile error -- Cannot switch
// on a value of type E. Only convertible int
// values, strings or enum variables are permitted
// (And of course, all the cases are incorrect
// because the enum set is unknown)
case ONE:
// do something
case TWO:
// do something
case THREE:
// do something …Run Code Online (Sandbox Code Playgroud) 为什么if块中的代码以任何方式执行?
switch(v)
{
case a:
break;
...
if(condition)
{
case f:
...
break;
case g:
...
break;
}
...
case z:
...
break;
default:
}
Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <sstream>
int main(int argc, char* argv[]) {
if ( argc != 2 ) {
std::cout << "usage: " << argv[0] << " <n> " << std::endl;
return 0;
}
std::stringstream strm;
strm << argv[1];
int count = 0;
int number;
strm >> number;
switch ( number ) {
case 0: ++count;
case 1: ++count;
case 2: ++count;
case 3: ++count;
case 4: ++count;
}
std::cout << "count: " << count << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个新手问题,但我刚开始使用C++.我参加了一个游戏设计课程,这是教授在SVN上的第一个例子.当我在编译后运行编程时,
./run …
我发现问题很接近,但没有任何帮助,任何人都可以看到我为什么要纠结这个错误吗?它有一个问题"(移动)"代码的第6行.谢谢
// move the spirit left/right/up/down
-(BOOL)moveSpiritWidthOrientation:(GameSpirit*)spirit moveEnum:(GameMoveEnum)move setTransform:(BOOL)trans{
CGPoint gridPoint = [self getGridPointWithPoint:spirit.endPoint];
CGAffineTransform transform = CGAffineTransformIdentity;
switch (move) {
case GameMoveTopEnum:
gridPoint.y -= 1;
transform = CGAffineTransformMakeRotation(-M_PI/2);
break;
case GameMoveBottomEnum:
gridPoint.y += 1;
transform = CGAffineTransformMakeRotation(M_PI/2);
break;
case GameMoveLeftEnum:
gridPoint.x -= 1;
transform = CGAffineTransformMakeScale(-1, 1);
break;
case GameMoveRightEnum:
gridPoint.x += 1;
break;
}
Run Code Online (Sandbox Code Playgroud) 所以这里只是一个关于带有char的开关盒的快速问题.
所以:
char c = a.charAt(i);
switch(c){
case 'a': System.out.print("This is an a");
case ''': System.out.print("How can one get this character checked in the case?);
}
Run Code Online (Sandbox Code Playgroud)
那么一个案例如何检查一个字母'当检查案件中的字符的样式是'时?
将非常感谢帮助.
测试了以下内容:
var timer:int = getTimer();
trace(timer);
for ( var g:int = 0; g < 10000000; ++g)
{
var mod0:int = g % 10;
var sum0:String = "";
switch(mod0)
{
case 0: sum0 = "0"; break;
case 1: sum0 = "1"; break;
case 2: sum0 = "2"; break;
case 3: sum0 = "3"; break;
case 4: sum0 = "4"; break;
case 5: sum0 = "5"; break;
case 6: sum0 = "6"; break;
case 7: sum0 = "7"; break;
case 8: sum0 …Run Code Online (Sandbox Code Playgroud) 我正在使用LINQ和HtmlAgilitypack从HTML创建数据表.以下获取html表头并构造数据表列:
var nodes = htmlDoc.DocumentNode.SelectNodes("//table[@class='ps-sellers-table crts']/tr");
nodes[0].Elements("th")
.Skip(0)
.Select(th => th.InnerText
.Trim())
.ToList()
.ForEach(header => dt.Columns.Add(header));
Run Code Online (Sandbox Code Playgroud)
到目前为止它的工作完美,除了我需要一些定制.
这个select语句将完成上述两件事:
switch (header)
{
case ("id") : dt.Columns.Add(header,typeof(int)); break;
case ("name") : dt.Columns.Add(header,typeof(string)); break;
case ("price") : dt.Columns.Add(header,typeof(decimal)); break;
case ("shipping") : dt.Columns.Add(header,typeof(decimal)); break;
default : //skip the column and dont add it
}
Run Code Online (Sandbox Code Playgroud)
但是我对LINQ和C#真的很新,我想在第一个片段中的foreach中实现上面的开关案例,我知道我应该使用ternary operator,但我不确定sytax.
我的Rails应用程序中有一些逻辑检查current_user变量的类类型:
logger.debug current_user.class # => Instructor
logger.debug current_user.class == Instructor # => true
case current_user.class
when Admin
logger.debug "Admin"
when Student
logger.debug "Student"
when Instructor
logger.debug "Instructor"
else
logger.debug "Guest"
end #=> "Guest"
Run Code Online (Sandbox Code Playgroud)
尽管Instructor是类类型(如注释中所示),但似乎case语句总是评估else的后退.有人可以解释一下原因吗?为了给出一点背景知识,我正在使用Devise实现STI用户模型设置.
我有一些代码创建一个表,根据条目的行值交替行颜色.
<table class="authorList" cellspacing="0">
{exp:channel:entries channel="team" disable="categories|member_data|pagination" orderby="team-last-name" sort="asc"}
{if team-not-with-us != 'y'}
<tr class="{switch="odd|even"} authorInfo">
<th class="authorName">
{if team-bio != ''}<a href="{site_url}about/the-team/{url_title}">{/if}
{title}
{if team-bio != ''}</a>{/if}
</th>
<td class="position">{team-position}</td>
</tr>
{/if}
{/exp:channel:entries}
</table>
Run Code Online (Sandbox Code Playgroud)
问题是当我删除一个条目时,我最终连续两个奇数或两个偶数,留下两个相同颜色的行并排.
虽然切换功能很方便,但它引用了数据库中的行数.我不相信我可以应用它来引用if语句中的实际行计数来解决我的问题.(如我错了请纠正我.)
我知道如何使用php进行此更改:
<?php $oddevenrow = 0; ?>
{if team-not-with-us != 'y'}
<?php $oddevenrow++; ?>
<?php ($oddeven = ($oddevenrow % 2 ? 'odd' : 'even')); ?>
<tr class="<?php echo $oddeven; ?> authorInfo">
Run Code Online (Sandbox Code Playgroud)
但我不允许在EE安装中打开PHP.
在EE中我能做些类似的事吗?
谢谢!
考虑以下代码段...
public static UserStatus getEnum(int code) {
switch (code) {
case 0:
return PENDING;
case 1:
return ACTIVE;
case 2:
return SUSPENDED;
case 3:
return DELETED;
case 4:
return LOGIN_DISABLED;
default:
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,案件中的第3和第4(案例3和案例4)被SONAR检测为幻数.
为了避免这个问题,我改变了我的代码段如下...
public static UserStatus getEnum(int code) {
final int Pending=0;
final int Active=1;
final int Suspended=2;
final int Deleted= 3;
final int Login_details=4;
switch (code) {
case Pending:
return PENDING;
case Active:
return ACTIVE;
case Suspended:
return SUSPENDED;
case Deleted:
return DELETED;
case Login_details: …Run Code Online (Sandbox Code Playgroud) switch-statement ×10
java ×3
c++ ×2
enums ×2
c# ×1
char ×1
devise ×1
if-statement ×1
linq ×1
logic ×1
performance ×1
ruby ×1
semantics ×1
sonarqube ×1
xcode ×1