是否可以使用十六进制case语句执行开关?
例如:
switch (integer) {
case: 1
function();
break;
case: F:
function();
break;
}
Run Code Online (Sandbox Code Playgroud)
g ++抱怨说:
example.cpp: In function ‘int main()’:
example.cpp:148:18: error: ‘F’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
我假设编译器试图将其F视为变量.我知道,而不是F我可以使用值,15但十六进制会更方便.
其他控制语句的解决方案也不错.
C++中的switch语句必须用常量编写的原因是什么?
我们来看看下面的代码:
switch(variable)
{
case 1:
case 2:
case 3:
case 4:
//Code 1
break;
case 5:
case 6:
case 7:
case 8:
//Code 2
break;
default:
//Code 3
break;
}
Run Code Online (Sandbox Code Playgroud)
在其他语言中,例如PAWN(C-Like脚本语言),我可以这样写下这段代码:
switch(variable)
{
case 1 .. 4:
//Code 1
break;
case 5 .. 8:
//Code 2
break;
default:
//Code 3
break;
}
Run Code Online (Sandbox Code Playgroud)
从石器时代开始,C++ switch语句是洙的原因是什么?(更不用说我们不能使用变量了.)
即使经过这么多年的这么多变化和更新......
说我有一个像这样的URL www.mysite.com/index.php?login=0.是否可以切换大小写$ _GET的变量并切换大小写$ _GET的变量值?
就像是:
switch ($_GET) {
case 'login' :
switch($_GET['login']) {
case '0' :
echo 'Login failed!';
break;
case '1' :
echo 'Login successful.';
break;
}
break;
case 'register' :
switch ($_GET['register']) {
case '0' :
echo 'Registration failed!';
break;
case '1' :
echo 'Thank you for registering.';
break;
}
break;
default :
echo 'Some other message';
break;
}
Run Code Online (Sandbox Code Playgroud)
我不确定是否可以在关联数组上使用switch case.我究竟做错了什么?干杯!
我有一个i映射到一组类别的变量.
我希望sector根据值的值将变量设置为匹配类别i.
我可以通过一系列if声明来做到这一点,如下所示:
if(i %in% c(7, 14, 21)) {sector = 'mining'}
if(i %in% c(28, 35, 42)) {sector = 'manu'}
if(i %in% c(49, 56, 63)) {sector = 'othr'}
if(i %in% c(70, 77, 84)) {sector = 'all'}
Run Code Online (Sandbox Code Playgroud)
这似乎应该是一种可用的分组交换机类型结构.R中有这样的东西吗?
非常感谢
如何让开关执行2个案例?
我尝试使用以下代码,但它只执行第一种情况
#!/bin/sh
action="titi"
case "$action" in
toto|titi)
echo "1_$action"
;;
tata|titi)
echo "2_$action"
;;
esac
Run Code Online (Sandbox Code Playgroud) 我正在从MainActivity启动一项服务,该服务无限期地运行以查找传入和传出的记录.
为什么示例"switch-case"不起作用?
没有错误发生,但我只能记录每个传入和传出.
录制呼叫后服务将被终止,它将被重新创建START_STICKY但永远不会开始.
请帮忙.提前致谢!
是否还有其他代码记录android中的传入和传出呼叫?
package com.exampled.demoserv;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class ParentalService extends Service
{
@Override
public void onCreate()
{
//Toast.makeText(getApplicationContext(), "Service Created", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Subu's Monitoring sTARTED", Toast.LENGTH_SHORT).show();
startMonitor();
return START_STICKY_COMPATIBILITY;
}
@Override
public IBinder onBind(Intent arg0)
{ …Run Code Online (Sandbox Code Playgroud) 我试图向Switch添加一个监听器,但由于某种原因它不会收听检查事件.
我CompoundButton.OnCheckedChangeListener在我的活动上实现 了这样的:
public class MyActivity extends Activity
implements CompoundButton.OnCheckedChangeListener
Run Code Online (Sandbox Code Playgroud)
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
newOrSavedSwitch = (Switch) findViewById(R.id.new_or_saved_switch);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(this, "Monitored switch is " + (isChecked ? "on" : "off"),
Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
吐司没有显示,我也没有看到logcat中的错误.
我想在这个问题前面说我是C的新手并因此很糟糕,所以我提前为任何明显的错误或糟糕的风格道歉.此外,我不知道如何在向您展示我的代码之前介绍问题,所以这里是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int MAX_INPUT_SIZE = 200;
volatile int running = 1;
while (running)
{
char input[MAX_INPUT_SIZE];
char *tokens[100];
const char *cmds[] = { "wait", "pwd", "cd", "exit" };
char *cmdargs[100];
printf("shell> ");
fgets(input, MAX_INPUT_SIZE, stdin);
// remove newline character at end
int nl = strlen(input) - 1;
if (input[nl] == '\n')
{
input[nl] = '\0';
}
// tokenize input string, put each token into an array
char *space;
space = strtok(input, " …Run Code Online (Sandbox Code Playgroud) 我正在为Visual Studio C#控制台中的课程编写文本冒险,并决定使用switch语句而不是if-elses链,因为goto情况有多么有用(到目前为止它已经非常有效).我知道交换机本身的每个案例都必须是一个常量,但我想知道这是否也扩展到使用goto情况.例如,我有:
switch (location)
{
case 1:
break;
case 2:
break;
case 3:
break;
//I have 10 cases, each representing a location such as "Orc Cave", I just cut it down for brevity
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够输入一个整数变量,然后转到该整数,我有以下内容来完成:
string travel2 = Console.ReadLine();//inputs a integer representing each location
int travel2A = Convert.ToInt32(travel2);
if (1<=travel2A && travel2A<=10)
{
goto case(travel2A);
}
else{
goto case(2);//current location
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但有一个"预期值不变"的警告(travel2A).是否可以通过一些调整使goto case输入变量,或者只是switch语句的限制?如果是后者,我可以做一个if-elses链,但在我看来,输入变量更方便.非常感谢任何帮助!谢谢!
我在这段代码中出错了
System.out.println("enter grade ");
Scanner input2 = new Scanner(System.in);
String grade = input2.nextLine();
switch(grade)
{
case "a":
g=10;
break;
case "b":
g=8;
break;
default:
System.out.println("invalid grade");
break;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用eclipse kepler.它给了我正确的输出.
但是用eclipse helios写的相同的代码,它说:
Cannot switch on a value of type String. Only convertible int values or enum constants are permitted
帮我解决这个问题.