untiy 取代switch
时间: 2023-07-21 07:06:27 浏览: 51
Unity中的开发者通常使用条件语句来实现类似于switch语句的功能。在C#中,可以使用if-else语句或者字典来达到类似的效果。
使用if-else语句的话,可以根据不同的条件执行不同的代码块。例如:
```csharp
int value = 2;
if (value == 1)
{
// 执行代码块1
}
else if (value == 2)
{
// 执行代码块2
}
else if (value == 3)
{
// 执行代码块3
}
else
{
// 执行默认代码块
}
```
另一种方法是使用字典来实现类似于switch的功能。字典中的键可以是条件,值可以是对应的代码块。例如:
```csharp
Dictionary<int, Action> actions = new Dictionary<int, Action>
{
{ 1, () => { /* 执行代码块1 */ } },
{ 2, () => { /* 执行代码块2 */ } },
{ 3, () => { /* 执行代码块3 */ } }
};
int value = 2;
if (actions.ContainsKey(value))
{
actions[value].Invoke(); // 执行对应的代码块
}
else
{
// 执行默认代码块
}
```
这些是在Unity中取代switch语句的一些常见方法,可以根据具体情况选择适合的方式来实现逻辑。
相关问题
switch case
The switch case statement is a control structure in programming that allows a program to evaluate an expression and execute different code blocks depending on the value of the expression. It is used to simplify complex decision-making processes and replace multiple if-else statements.
The basic syntax of a switch case statement is:
```
switch(expression) {
case value1:
// code block executed if expression matches value1
break;
case value2:
// code block executed if expression matches value2
break;
...
default:
// code block executed if expression doesn't match any case
break;
}
```
The switch statement evaluates the expression and compares it to each case value. If there is a match, the code block associated with that case is executed. If there is no match, the code block associated with the default case is executed.
The break statement is used to exit the switch statement after a case has been executed. If a break statement is not used, the code will continue to execute through the subsequent cases until a break statement is encountered or the end of the switch statement is reached.
LocalDateTime until
LocalDateTime的until()方法是用来计算两个LocalDateTime对象之间的时间间隔的。它接受一个参数,即结束的LocalDateTime对象,并使用TemporalUnit来确定计算的单位。如果结束的时间早于开始的时间,结果将为负数。计算返回的是一个整数,代表了两个LocalDateTime之间的完整单位数。这个方法不会改变调用它的实例本身,它是不可变的。
需要注意的是,之前在引用中提到了一个关于Period.between()方法的误解。Period.between()方法计算的是两个日期之间的间隔,以年、月、日为单位。而不是换算后的天数。如果只需要计算天数的间隔,可以使用LocalDate类的until()方法来替换,传入的参数是结束的日期和TemporalUnit为ChronoUnit.DAYS,这样可以得到两个日期之间的天数间隔。
总结起来,LocalDateTime的until()方法可以用来计算两个LocalDateTime之间的时间间隔,而不是日期间隔。如果需要计算日期间隔,可以使用LocalDate的until()方法,并指定TemporalUnit为ChronoUnit.DAYS。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Java LocalDateTime until()用法及代码示例](https://blog.csdn.net/weixin_33431252/article/details/114917818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [Java8时间间隔计算Period.between与LocalDate.until区别&LocalDate,LocalDateTime计算两个时间的相差时间](https://blog.csdn.net/qq_43842093/article/details/127468553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]