我可以在 C 开关中使用类似 javas finally 子句的东西。我的大多数案例都有一组共享的功能,我想将其放在一个案例中。我正在考虑使用 goto 语句来实现这一点,充分了解 goto 代码混淆能力,将共享案例放在 switch 语句的底部似乎仍然是比将共享功能分区到单独函数中更“干净”的方法。
\n\n不管怎样,我一直在尝试做这样的事情:
\n\nswitch( x ) {\ncase 0:\n printf("Case 0\\n");\n goto case 2;\n break;\ncase 1:\n printf("Case 1\\n");\n goto case 2;\n break;\ncase 2:\n printf("Case 2\\n");\n break;\ndefault:\n // do nothing\n break;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n但是,使用 gcc 会失败并出现错误
\n\nerror: expected identifier or \xe2\x80\x98*\xe2\x80\x99 before \xe2\x80\x98case\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n\n关于如何使其发挥作用有什么建议吗?或者可能有更好的方法?
\n如何让 switch 语句中的元组具有多种可能性?注意我已经尝试过
var duel = (comp1CurrCard, comp2CurrCard)
switch duel {
case (1||14||27||40, 1||14||27||40):
println("ace duel")
case (2,15,28,41),(2,15,28,41):
println("2 duel")
}
Run Code Online (Sandbox Code Playgroud)
comp1CurrCard和comp2CurrCard都是 Int 类型。本质上我想要的是 if comp1CurrCard== 1 || 14 || 27|| 40 && comp2CurrCard== 1 || 14 || 27|| 40 比 println("王牌决斗")
但是我不知道如何最好地做到这一点,我确实知道我希望使用 switch 语句,因为它似乎是解决它的最佳方法
我知道由于错误我做错了一些事情:P 感谢任何帮助!
所以我有一些如下所示的 C# 代码:
while(condition)
{
switch(anumber)
{
case 0:
//do something
break;
case 1:
//do something
break;
//and so on
}
}
Run Code Online (Sandbox Code Playgroud)
作为一名编程新手,我最近将关键字添加到continue了我的词汇表中。经过一些研究后,我发现了这一点:
continue 语句与封闭循环相关
所以我的代码也应该像这样工作:
while(condition)
{
switch(anumber)
{
case 0:
//do something
continue;
//and so on
}
}
Run Code Online (Sandbox Code Playgroud)
但编写不产生编译器错误的代码并不是一切。continue在循环封闭的开关块中使用是一个好主意吗?例如,在性能方面是否有任何差异,或者这只是两种语法上不同但在其他方面非常相似的方法来实现相同的结果?
我对 Java 还很陌生,但熟悉其他一些编程语言后,我知道其中很多内容的基本布局。我遇到的一件事是循环菜单,特别是主菜单。
我不知疲倦地研究了它的方法,但似乎没有一个适用于我的程序。我假设它是我在更基本的程序中遗漏的一些愚蠢而小的东西。
检查下面,感谢您的任何提示。
import java.util.Scanner;
public class basicCalc {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
boolean mainLoop = true;
int choice;
do{
System.out.println("Calculator Main Menu\n");
System.out.print("1.) Addition \n");
System.out.print("2.) Subtraction.\n");
System.out.print("3.) Multiplication.\n");
System.out.print("4.) Division.\n");
System.out.print("5.) Generate Random Number.\n");
System.out.print("6.) Exit\n");
System.out.print("\nEnter Your Menu Choice: ");
choice = input.nextInt();
}
while(choice >7);
switch(choice){
case 1:
//Definitions
int adNumf, adNuml, sum;
System.out.print("Please Enter The First Number: ");
adNumf = input.nextInt(); …Run Code Online (Sandbox Code Playgroud) 我正在创建一个脚本,该脚本将根据条件更新 Excel 电子表格。
这就是我目前所拥有的:
if ($endIRs -ne $null) {
$endIRs | ForEach-Object {
try {
$classification = $_.Classification
$priority = $_.Priority
$title = $_.Title
$id = $_.Id
switch ($classification) {
{($_ -eq 'Reports') -and ($priority -eq '1')} {
$GeAppsReportSheet.Cells.Item(8,2).Interior.ColorIndex = 3
$GeAppsReportSheet.Cells.Item(8,2) = 'RED'
}
#more switch statements to go here
}
catch {#catch tickets with $classification not listed}
}
}
Run Code Online (Sandbox Code Playgroud)
一$endIRs开始保存了过去 12 小时内记录的一系列高优先级“事件”。如果没有,一切都将是默认设置的“绿色”。
我试图通过该语句实现的switch目标是if (($classification -eq 'Reports') -and ($priority -eq '1')) {'change the cell …
如何在 C++ 中为单个案例接受多个值?我知道您可以使用case 1..2其他一些语言为一个案例(例如)设置一系列值,但它似乎不适用于 Xcode 上的 C++。
int main() {
int input;
cin >> input;
switch (input) {
case 1:
cout << "option 1 \n";
break;
case 2..3: //This is where the error occurs
cout << "option 2 and 3 \n";
break;
default:
break;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序显示错误,指出“浮动常量上的后缀无效'.3'”范围所在。
while如果n等于 3,我想打破这个循环,但我该怎么做?
n = 0;
while(true){
switch (n){
case 3:
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud) switch (`(${wf}, ${hf})`) {
case '(0, 0)': return 0;
case '(1, 0)': return 1;
case '(2, 0)': return 2;
case '(0, 1)': return 3;
case '(1, 1)': return 4;
case '(2, 1)': return 5;
case '(0, 2)': return 6;
case '(1, 2)': return 7;
case '(2, 2)': return 8;
}
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以做到这一点?我试图找到一个模式,但不能。
试图理解 switch 表达式并想出了以下代码。事实上,对于所有“break - String”组合,我都会收到“not a statement”错误。我究竟做错了什么?
String i = switch(value) {
case 0:
break "Value is 0";
case 1:
break "Value is 1";
case 2:
break "Value is 2";
default:
break "Unknown value";
};
Run Code Online (Sandbox Code Playgroud) 在 C/C++ 中,我有一个场景,如果应该根据空大小执行。如果变量 a 的大小为 0,则应打印字符串 'add name',否则应打印 'leave it' 字符串。但是我也有用于编码的 switch case,对于每种情况,如果条件会有所不同。
switch(encoding)
case utf:
if(a->size-5 == 0)
{
cout<<"add name";
}
else
{
cout<<"leave it";
}
case ansi:
if(a->size-4 == 0)
{
cout<<"add name";
}
else
{
cout<<"leave it";
}
case ansi8:
if(a->size-8 == 0)
{
cout<<"add name";
}
else
{
cout<<"leave it";
}
Run Code Online (Sandbox Code Playgroud)
我想以最少的方式做到这一点。那么最好的方法是什么。
switch-statement ×10
c# ×2
c++ ×2
java ×2
loops ×2
break ×1
c ×1
c++11 ×1
case ×1
goto ×1
if-statement ×1
java-13 ×1
javascript ×1
menu ×1
powershell ×1
swift ×1
tuples ×1
while-loop ×1