有人可以解释为什么代码打印 "HelloWorld" 而不是 "HelloWorldThere" 吗?另外,为什么它会打印任何内容,因为 if 或 switch 语句中没有条件?这是代码:
#include <stdio.h>
int main()
{
int a, b;
if(printf("Hello"))
switch(printf("World"))
while(printf("There"))
{
return 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我需要根据对象的哪个属性为空来执行特定操作。
检查哪个属性已定义为“null”的 switch case 是否可能?
像这样的东西:
switch (Is Null)
{
case "property1" : foo1()
case "property2" : foo2()
case "property3" : foo3()
default : foo4()
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
int x = 10;
switch(x) {
case 10:
System.out.println("10");
break;
case 5+5:
System.out.println("5+5");
break;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,两种情况都是真的,将执行什么?第一种情况,还是两种情况
我们有4个地区: a b c d
我们想把数字放在这些地区.
如何仅使用switch语句执行此操作:
the number divisible by 10 and divisible by 7 ?n region a
the number divisible by 10 but not divisible by 7 ?n region b
the number not divisible by 10 but divisible by 7 ?n region c
the number not divisible by 10 and divisible by 7 ?n region d
Run Code Online (Sandbox Code Playgroud)
例如,如果:
input 770 out put is a
input 200 output b
input 154 output c
Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个简单的程序,演示如何在switch语句中使用String类,但我收到编译错误,因为此功能需要Java SE 7(我的计算机中已经安装了).
第5行的错误消息:无法打开String类型的值.只允许使用可转换的int值或enum consants
Eclipse是否指向JRE7?=是.Environnement变量指向JDK7 = YES.
java -version = OK(见下图).

我甚至试图用控制台提示符编译代码.看错误:

import javax.swing.JOptionPane;
public class SwitchDemo {
public static void main(String[] args) {
String name = "georges";
switch (name.toLowerCase()) {
case "Jhon":
JOptionPane.showMessageDialog(null, "Good morning, Jhon!");
break;
case "georges":
JOptionPane.showMessageDialog(null, "How's it going, georges?");
break;
case "sergei":
JOptionPane.showMessageDialog(null, "sergei, my old sergei!");
break;
case "Steph":
JOptionPane.showMessageDialog(null, "Afternoon lennert, how's the Steph?");
break;
default:
JOptionPane.showMessageDialog(null, "Pleased to meet you, xxxxxx.");
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)

所以我有两个case/switch语句,由于某种原因,两个变量激活的print语句被激活两次.我一遍又一遍地阅读代码,找不到任何会导致重复打印语句的内容.输出是:
黑暗骑士利用他的狡猾和意志力再次击败敌人.
不幸的是,有人带来了Kryptonite :(
黑暗骑士使用他的狡猾和
意志力再次击败敌人.不幸的是,有人带来了Kryptonite :(
有人看到错误吗?如果是这样,请提前感谢任何答案; 下面的代码和错误的代码是四重引号:
class Hero{
String name;
int intelligence;
boolean parents;
double strength;
public static String fight(Hero hero1, Hero hero2){
if(hero1.intelligence+hero1.strength>hero2.intelligence+hero2.strength)
return(hero1.name+" is the winner");
else{
//If hero2 wins, initiate this code
""""switch (hero2.name){
//If the hero's name is ___ then print ____
case "Batman":
System.out.println("The Dark Knight uses his cunning and strength of will to beat down the enemy once again.");
break;
case "Superman":
System.out.println("Superman is literally invincible. The Son of Krypton wins …Run Code Online (Sandbox Code Playgroud) 我有一个程序从文件中读取参数.该文件一次读取一行,并检查每一行以查看它是否包含特定值或空白.如果line不是空格,则行中的值将传递给switch语句.以下是有问题的swtich声明的一部分:
switch(stName)
{
//GENERAL section
case "JOBNAME":
_JobName = stValue;
break;
case "RUN AS-OF DATE":
_RunDate = stValue;
break;
case "USER NOTIFICATION EMAIL ADDRESS":
_UserEmailAddr = stValue;
break;
default:
System.Exception ex = new System.Exception("Unexpected parameter");
ex.Data.Add("Config File", oCommandArgs.ConfigFile);
ex.Data.Add("Parm Line", stIniLine);
ex.Data.Add("Delimiter", cDelimiter);
ex.Data.Add("Name", stName);
ex.Data.Add("Value", stValue);
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
在转到switch语句之前,stName中的值将转换为大写.我的问题是,是否可以对未存储在变量中的字符串值使用.ToUpper()方法?基本上,这样代码就像这样:
switch(stName)
{
//GENERAL section
case "Jobname".ToUpper():
_JobName = stValue;
break;
case "Run as-of date".ToUpper():
_RunDate = stValue;
break;
case "User notification e-mail address".ToUpper():
_UserEmailAddr = stValue;
break;
default:
System.Exception …Run Code Online (Sandbox Code Playgroud) 基本上我的交换机案例在那些我认为不符合的条件的语句中无法正常工作,无论如何都在执行.
输入示例:
CerseiTyrion
Run Code Online (Sandbox Code Playgroud)
预期产量:
Cersei
Tyrion
Run Code Online (Sandbox Code Playgroud)
实际产量:
Cersei
Tyrion
Jon
Dany
Tyrion
Jon
Dany
Run Code Online (Sandbox Code Playgroud)
以下功能的目标是解析冒险游戏的一些输入文本,让游戏了解文本中包含的一些重要信息.然后它继续使用它发现的执行代码,如果满足某些条件,该代码应始终与函数的其余部分一起运行.
最小,完整; 可验证的例子:
void HandleGameState( const std::string& text ) {
enum Characters {
Cersei = 0x01,
Tyrion = 0x02,
Jon = 0x04,
Dany = 0x08
};
unsigned int mask = 1;
switch( characterMask & mask )
{
case Cersei:
{
std::cout << "Cersei" << std::endl;
};
case Tyrion:
{
std::cout << "Tyrion" << std::endl;
};
case Jon:
{
std::cout << "Jon" << std::endl;
};
case Dany: …Run Code Online (Sandbox Code Playgroud) 下面提到的程序是开关盒的基本形式。我正在尝试在输出中打印宏变量。有关更多详细信息,请参见输出。它不打印宏变量。
开关盒
#include<stdio.h>
#define a 4
#define d 5
int main()
{
int x = 10;
switch (x)
{
case a: printf("Number is 40");
break;
case d: printf("Number is 50");
break;
default: printf("Default case");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
预期:数字为40
实际:默认情况
请参考第1点和第2点。
#include<stdio.h>
#define a 4
#define d 5
int main()
{
int x = 4; //(Point1)Here I'm declared the x value as 4 which is equal to the above defined one of the macros. Hence the output has printed the expected …Run Code Online (Sandbox Code Playgroud) 关注这个问题为什么gcc不允许将const int作为case表达式?,什么促销类型用于switch-case表达式比较基本相同?或者有没有办法在C中使用具有常量索引的常量数组作为开关案例标签?.
从第一个链接,我试图替换:
case FOO: // aka 'const int FOO = 10'
Run Code Online (Sandbox Code Playgroud)
用:
case ((int) "toto"[0]): // can't be anything *but* constant
Run Code Online (Sandbox Code Playgroud)
这使 :
https://ideone.com/n1bmIb - > https://ideone.com/4aOSXR =在C++中工作
https://ideone.com/n1bmIb - > https://ideone.com/RrnO2R = C失败
我不太明白,因为"TOTO"字符串不能是任何东西,但一个常数之一,它甚至不是一个变量,它位于编译器内存的空白.我甚至没有玩C语言的'const'模糊逻辑(它实际上代表"只读,不是常数,你期望什么?"),问题是"数组访问"或"指针引用" "进入一个不用C语言评估的常量表达式,但在C++中做得很好.
我希望使用这个"技巧"来使用HASH_MACRO(str)从密钥标识符生成唯一的case标签值,最终使编译器在发生冲突时引发错误,因为找到了类似的标签值.
好的,好的,我被告知这些限制是为了简化语言工具(preproc,编译器,链接器)而C不是没有LISP,但你可以拥有全功能的LISP解释器/编译器,其大小只相当于C等价物的一小部分,所以这不是借口.
问题是:C11是否有"扩展",只允许这个"toto"东西在GCC,CLANG和...... MSVC中工作?我不想去C++路径(typedef的前向声明不再起作用)和嵌入式东西(因此编译时哈希计算用于时空失真).
是否有一种中间的"C +"语言更加"宽容"和"理解"嵌入更好一点,比如 - 赞美领主 - "作为位域成员的枚举",以及我们不能拥有的其他很好的东西(由于以外的原因)现实标准像沙漠太阳下的蜗牛一样演变?
#provemewrong,#changemymind,#norustplease
switch-statement ×10
c ×4
java ×3
c# ×2
c++ ×2
string ×2
.net ×1
case ×1
constants ×1
if-statement ×1
int ×1
printf ×1
while-loop ×1