我想要做的是使用枚举轻松指定不同的绘制模式.到目前为止,这就是我所拥有的:
class Grid {
enum drawMode { GRID, EROSION, RIVERS, HUMIDITY, ATMOSPHERE }
drawMode activeDraw;
void draw() {
switch(activeDraw) {
case GRID:
drawGrid();
break;
case EROSION:
drawErosion();
break;
// etc..
}
void keyPressed(int key) {
switch(key) {
case ' ':
// Cycle activeDraw to next drawMode
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果用户点击空格键,activeDraw将从枚举更改为下一个值.因此,如果当前activeDraw是打空格后GRID它将改变糜烂,如果activeDraw是大气它将改变电网.
有一个简单的解决方案吗?谢谢.
我有2个boolean变量,例如:
boolean isWhite;
boolean isTall;
Run Code Online (Sandbox Code Playgroud)
所以我想确定4种可能情况的不同行为({(isWhite, isTall), (!isWhite, isTall), (isWhite, !isTall), (!isWhite, !isTall)}).
1)有没有比if/else每次使用更优雅和实用的方法?
if(isWhite) {
if(isTall) {
// Case 1
}
else {
// Case 2
}
else {
if(isTall) {
// Case 3
}
else {
// Case 4
}
Run Code Online (Sandbox Code Playgroud)
2)做这样的事情会有什么不同吗?
if (isWhite && isTall)
// Case 1
if (isWhite && !isTall)
// Case 2
if (!isWhite && isTall)
// Case 3
if (!isWhite && …Run Code Online (Sandbox Code Playgroud) 一些上下文:我正在解析一个具有account1和account2作为int类型的会计分类帐.每个都是[0,99999]范围内的数字.我有很多组合需要考虑.
理想情况下,我想使用类似的东西
switch (account1, account2){
case (1,1):
/*account1 is 1, account2 is 1*/
case (2,1):
/*account1 is 2, account2 is 1*/
}
Run Code Online (Sandbox Code Playgroud)
等等.(我只需要考虑大约20种可能的组合).
有没有办法在Java中实现这一目标?
我已经考虑过这个问题在java中存储数字对
并且可以建立一个IntPair班级.也许如果我定义bool equals那么我可以switch在一个实例上,以类似的方式你可以打开java.lang.String.
我正在玩一个接受用户输入的应用程序,并打印一个简单的输出.
我的代码看起来像这样:
switch text {
case "Hi", "hi", "Hello", "hello", "Good day", "good day":
return "Hello, sir."
default:
return "Sorry, I didn't understand!"
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,不是一个输出有5-10个可能的输入,是否可以放置一个不区分大小写的输入?例如,我可以只是把"你好",并让我的声明检查"你好","你好","你好"等?
我在一个控制器中有4个不同的collectionViews,在第一个collectionView中,我想显示3个不同的单元格。在下面的代码中,该应用程序不会崩溃,但在第一个indexPath.item为0的情况下,仅加载0:(“ cellId”)。它不会加载其他两种情况。在此先感谢您的帮助!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
switch indexPath.item {
case 0:
return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
default:
return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId4", for: indexPath)
}
} else if indexPath.item == 1 {
return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath)
} else if indexPath.item == 2 {
return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId3", for: indexPath)
} else {
let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
return myCell
}
}
Run Code Online (Sandbox Code Playgroud)
// 2个collectionView单元格-每个单元格中只有一个部分
// //单元格1-collectionViewController中带有reuseIdentifier“ cellId”
class TravelGuideHomeCellForStats: BaseHomeCell, …Run Code Online (Sandbox Code Playgroud) switch-statement uicollectionview uicollectionviewcell swift indexpath
我是一个尝试用JavaScript制作文本冒险游戏的初学者,我需要重复一个switch语句,直到用户输入一个有效的答案:
opts = prompt("Do you want to TALK or LOOK?").toUpperCase();
switch(opts) {
case "TALK":
mes = "Is anyone in charge here?";
speech = "Our leader is in the big building.";
talkNot();
break;
case "LOOK":
window.alert("The buildings are quite simple, and the doorways are much shorter than you. You notice, however, that there is a very tall, thin building at the end of the street.");
break;
default:
window.alert("That's not an option.");
}Run Code Online (Sandbox Code Playgroud)
任何答案都会非常有用 - 谢谢!
每次输入正确的char输入时都会执行默认语句.我错过了什么?
我的产出:
正确的产出:
#include <stdio.h>
void main() {
char ch = '?';
float f;
double a = 10.00, b = 20.00;
int i;
for (i = 0; i < 10; i++) {
scanf("%c", &ch);
switch (ch) {
case '+':
f = a + b;
printf("f = %.0f\n", f);
break;
case '-':
f = a - b;
printf("f = %.0f\n", f);
break;
case '*':
f = a * b;
printf("f = %.0f\n", f);
break;
case '/':
f = a / b; …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个简单的开关来确定基于季度的开始日期.
我的代码如下:
qtr_start <- function(qtr, yr){
switch(qtr,
1 = paste(yr, "0101", sep = ""),
2 = paste(yr, "0104", sep = ""),
3 = paste(yr, "0107", sep = ""),
4 = paste(yr, "0110", sep = ""))
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误:
Error: unexpected '=' in:
"switch(qtr,
1 ="
> 2 = paste(yr, "0104", sep = ""),
Error: unexpected ',' in " 2 = paste(yr, "0104", sep = ""),"
> 3 = paste(yr, "0107", sep = ""),
Error: unexpected ',' in " 3 = paste(yr, …Run Code Online (Sandbox Code Playgroud) 考虑这个基准,我们比较地图访问与交换机
var code = []int32{0, 10, 100, 100, 0, 10, 0, 10, 100, 14, 1000, 100, 1000, 0, 0, 10, 100, 1000, 10, 0, 1000, 12}
var mapCode = map[int32]int32{
0: 1,
10: 2,
100: 3,
1000: 4,
}
func BenchmarkMap(b *testing.B) {
success := int32(0)
fail := int32(0)
for n := 0; n < b.N; n++ {
// for each value in code array, do a specific action
for _, v := range code {
c, ok := mapCode[v] …Run Code Online (Sandbox Code Playgroud) 我正在根据哈希值分配一组值.
我必须根据传入的哈希键的值为另一个哈希键分配值.
传入哈希中有超过10个键(因此超过10个案例).
我想最小化代码.是否有任何替代方法可以缩短代码.
@hash1.each do |h1|
case h1.mapped_field
when 'value1'
@hash2[h1.field_id] = value_1
when 'value2'
@hash2[h1.field_id] = value_2
when 'value3'
@hash2[h1.field_id] = value_3
when 'value4'
@hash2[h1.field_id] = value_4
when 'value5'
@hash2[h1.field_id] = value_5
end
end
Run Code Online (Sandbox Code Playgroud) switch-statement ×10
java ×2
swift ×2
arrays ×1
c ×1
c++ ×1
c++11 ×1
enums ×1
for-loop ×1
go ×1
hashmap ×1
if-statement ×1
indexpath ×1
javascript ×1
performance ×1
r ×1
ruby ×1
user-input ×1