我知道在 Swift 中我可以做一个简单的 Swift switch 语句来将计算值与单个变量进行比较:
let distanceTravelled = -145.1
switch distanceTravelled {
case _ where distanceTravelled < 15:
print("You travelled the right distance")
default:
print("Sad face")
}
Run Code Online (Sandbox Code Playgroud)
但我希望能够使用元组设置比较矩阵,如下所示:
let distanceTravelled = -145.1,
origin = 2
switch (origin, distanceTravelled) {
case (2, _ where distanceTravelled < 15):
print("You travelled the right distance")
default:
print("Sad face")
}
Run Code Online (Sandbox Code Playgroud)
但这不能编译,抱怨expected ',' separator和expected expression in list of expressions。
显然这在语法上是不正确的,但是考虑到它是有效的,它不应该有效吗?
switch (2, -145.1) {
case (2, _):
print("You travelled the right …Run Code Online (Sandbox Code Playgroud) 在尝试学习 C# 时,我尝试使用 Enum 和 switch 语句。如何将用户输入传递到变量中,以便让他选择枚举中的选项之一?
传递用户输入的正确方法是什么?
我的代码如下所示:
using System;
namespace switchDemo
{
class Program
{
enum Movies
{
LOTR,
Starwars,
Matirx
}
static void Main(string[] args)
{
Console.WriteLine("Choose the best movie (LOTR, Starwars, Matirx): ");
Movies bestMovie = Convert.ToString(Console.ReadLine());
//Movies bestMovie = Movies.LOTR;
switch (bestMovie)
{
case Movies.LOTR:
Console.WriteLine("thats correct - LOTR");
break;
case Movies.Matirx:
Console.WriteLine("Martix is not the best");
break;
case Movies.Starwars:
Console.WriteLine("StarWars is 2nd best");
break;
default:
Console.WriteLine("choose an option");
break;
}
Console.WriteLine("Hello World!");
}
} …Run Code Online (Sandbox Code Playgroud) 我一直想知道是否有办法替换当前的 switch 语句。下面是我的代码示例,尽管我的语句更长并且只会变得更大。switch 方法通过文件读取器调用,因此它读取一行,然后使用分配的值调用此函数。
public static void example(String action, String from, String to){
switch (action) {
case ("run"):
runTo(from,to);
break;
case ("walk"):
walkTo(from,to);
break;
case ("hide"):
hideAt(to);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我很好奇是否有更好的方法而不是像上面的场景那样使用 switch 语句。
我对示例进行了一些更新,使其更有意义。某些方法调用不需要使用所有参数。
给定一个像这样的 switch 语句:
switch(text.toLowerCase()) {
case("aaa"):
doSomething();
return;
case("bbb"):
doSomething();
return;
case("ccc"):
doSomething();
return;
default:
return;
}
Run Code Online (Sandbox Code Playgroud)
开关本身内的表达式被调用多少次?
它会在检查的每种情况下调用 toLowerCase() (这意味着最好将此表达式插入到该 switch case 之前的变量中,并在 switch 内使用该变量)
或者它会计算 toLowerCase() 一次并为所有情况使用最终表达式的值?
我尝试这样做,但它总是输出默认情况,是的,我已将 k 和 j 设置为 1 和 3
switch(k,j){
case 1&&3:
cout << "1 3 \n";
break;
default:
cout << "invalid \n";
break;
Run Code Online (Sandbox Code Playgroud) 为了遵循这次讨论,我实施了Yurii Kotov的建议:
struct ContentView: View {
@State private var index = 0
@ViewBuilder
var body: some View {
if index == 0 {
MainView()
} else {
LoginView()
}
}
Run Code Online (Sandbox Code Playgroud)
效果很好。但如果我尝试使用 switch 语句来代替:
switch index {
case 0: MainView()
case 1: LoginView()
default:
print("# error in switch")
}
Run Code Online (Sandbox Code Playgroud)
什么都没发生。没有错误警报,但也根本没有结果。有人可以帮忙吗?
drink = 'beer'
switch(drink){
case 'beer':
case 'whiskey':
console.log('The drink is BEER or WHISKEY');
case 'juice':
console.log('The drink is JUICE');
default:
console.log('Nothing to drink');
}Run Code Online (Sandbox Code Playgroud)
对于上面的代码,为什么我会在控制台中收到所有三个消息?有人可以解释一下吗?如果没有break,我期望打印案例消息和默认消息,但为什么“juice”相关消息也出现在控制台中?
我有一个带有一堆提交输入的表单,每个输入都有一个唯一的名称,现在我想在控制器中检查正在单击哪个输入。
表格
<input type="submit" name="button_1" value="1">
<input type="submit" name="button_2" value="2">
<input type="submit" name="button_3" value="3">
<!-- and more submit inputs -->
Run Code Online (Sandbox Code Playgroud)
在控制器中,我现在使用 if else 来检查正在单击哪个输入,例如我可以使用这个
if ($request->input('button_1'))
{$message = '1'; // and more code
}
elseif ($request->input('button_2'))
{$message = '2'; // and more code
}
elseif ($request->input('button_3'))
{$message = '3'; // and more code
}
// ...
Run Code Online (Sandbox Code Playgroud)
或者我可以用这个
if ($request->has('button_1')) {$message = '1';}
elseif ($request->has('button_2')) {$message = '2';}
elseif ($request->has('button_3')) {$message = '3';}
// ...
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来检查哪个提交输入被单击,例如 switch 方法,因为这将使编写逻辑更加简单和清晰,但我无法弄清楚。
为什么我在执行匹配表达式时遇到错误:
$number = '1';
$result = match($number) {
1 => 'one',
2 => 'two',
3, 4 => 'three or four',
};
echo $result;
Run Code Online (Sandbox Code Playgroud)
致命错误:未捕获 UnhandledMatchError:未处理的字符串类型匹配值
也许这是一个愚蠢的问题
我有一个这样的 switch case 语句:
self.text = type.rawValue
switch type {
case .teuro:
self.backgroundColor = UIColor.sapphireColor()
case .lesson:
self.backgroundColor = UIColor.orangeColor()
case .profession:
self.backgroundColor = UIColor.pinkyPurpleColor()
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以将其写成类似以下示例的内容:
self.backgroundColor = {
switch type {
case .teuro:
return UIColor.sapphireColor()
case .lesson:
return UIColor.orangeColor()
case .profession:
return UIColor.pinkyPurpleColor()
}
}
Run Code Online (Sandbox Code Playgroud)
任何评论或回答表示赞赏。谢谢。
switch-statement ×10
swift ×3
enums ×2
if-statement ×2
javascript ×2
c# ×1
c++ ×1
case ×1
java ×1
laravel ×1
php ×1
php-8 ×1
request ×1
swiftui ×1