我使用下面的这个例子来询问如何将数组传递给Switch,而不是像这样将它们列出来?
也许如果我从一个数据库中获取一个数组作为页面的所有"页面".
我认为这种方法不是最优雅的方法.
switch($_GET['page'])
{
case 'home':
include('home.php');
break;
case 'oem-customers':
include('oem-customers.php');
break;
case 'job-shop':
include('job-shop.php');
break;
case 'anodized-magnet-coils':
include('anodized-magnet-coils.php');
break;
case 'design':
include('design.php');
break;
case 'services':
include('services.php');
break;
case 'black-foil':
include('black-foil.php');
break;
case 'contact':
include('contact.php');
break;
case 'order';
include('order.php');
break;
default:
include('home.php');
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
switch (currentLetter)
{
case 'A': return 'B';
case 'B': return 'C';
case 'C': return 'D';
case 'D': return 'E';
case 'E': return 'F';
case 'F': return 'G';
case 'G': return 'H';
case 'a': return 'b';
case 'b': return 'c';
case 'c': return 'd';
case 'd': return 'e';
case 'e': return 'f';
case 'f': return 'g';
case 'g': return 'h';
}
Run Code Online (Sandbox Code Playgroud)
我想到了许多方法来改变它,但我不确定选择哪个.我可以替换所有返回(char)(currentLetter + 1),使用带有ASCII值的if语句来确定范围,然后执行(char)(currentLetter + 1),使用Enumerable.Range.Contains然后查看该值是否在范围内,用if替换开关等.
这段代码不会在其他任何地方重复,我不确定这是不是最好的方法,因为代码的读者对于正在发生的事情非常清楚,他们不必考虑字符代码,算术等.此外,永远不会有任何更多的字符添加到case语句,所以它不会变得笨拙.
不确定我是应该保留原样还是更改它.
这是一个yahtzee游戏,这个方法应该计算并返回五个骰子的值.但这是一种愚蠢的方式,有一个switch语句,通过参数传递用户选择的类别,然后是每个可能类别的for循环.有可能比我的设计理念更容易做到这一点吗?
private int assignScoreToCategory(int category)
{
int computedScore = 0;
println("dice: "+dice1+" "+dice2+" "+dice3+" "+dice4+" "+dice5);
// Switches on the category the user has selected.
switch (category)
{
case ONES:
for (int i = 0; i < 4; i++){
if (diceArray[i] == ONES){
println(computedScore);
computedScore++;
}
}break;
case TWOS:
break;
case THREES: break;
case FOURS: break;
case FIVES: break;
case SIXES: println("cat 6"); break;
case UPPER_SCORE: break;
case UPPER_BONUS: break;
case THREE_OF_A_KIND: break;
case FOUR_OF_A_KIND: break;
case FULL_HOUSE: break;
case …Run Code Online (Sandbox Code Playgroud) 所以我有以下内容:
//at file scope
GLfloat point_colour[3] = {1.0, 0.5, 0.0};
...
void menu_colour(int index){
switch(index){
case RED: point_colour = {1.0, 0.0, 0.0}; break;
case GREEN: point_colour = {0.0, 1.0, 0.0}; break;
case BLUE: point_colour = {0.0, 0.0, 1.0}; break;
case ORANGE: point_colour = {1.0, 0.5, 0.0}; break;
case PURPLE: point_colour = {1.0, 0.0, 0.8}; break;
default: point_colour = {1.0, 0.0, 0.0};
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到error: expected expression before '{' token了每一个案例.
我正在尝试为数组分配一个新的值point_colour数组 - 任何帮助?我怀疑我的做法完全是错误的......
过去3个小时我一直在看这段代码而且我很困惑.我感谢任何帮助,谢谢.
file:UnsortedType.h
#include "ItemType.h"
class UnsortedType{
public:
UnsortedType();
void RetireveItem(ItemType& item, bool& found);
bool InsertItem(ItemType item);
private:
int length;
ItemType info[MAX_ITEMS];
};
Run Code Online (Sandbox Code Playgroud)
file:UnsortedType.cpp
#include "UnsortedType.h"
#include <iostream>
UnsortedType::UnsortedType() {
length = 0;
}
void UnsortedType::RetireveItem(ItemType& item, bool& found) {
bool moreToSearch = true;
int location = 0;
found = false;
moreToSearch = (location < length);
while (moreToSearch && !found) {
switch (item.ComparedTo(info[location])) {
case LESS:
location++;
moreToSearch = (location < length);
break;
case GREATER:
location++;
moreToSearch = (location < length); …Run Code Online (Sandbox Code Playgroud) 这是我的问题.我从此方法中删除了switch语句,因为我必须更改它以适应多个变量,而switch语句只允许char常量.在没有java版本7的情况下,无法将字符串用于变量.所以我所做的就是改为通常的if/else语句.但是在尝试运行程序时,我仍然在switch语句中收到错误,如下所示:
有任何想法吗?如果我希望包含测试人员的代码,我可以问一下.
java.lang.Error:未解决的编译问题:无法为源级别低于1.7的值打开String类型的值.只允许使用可转换的int值或枚举变量
在Project2.PostfixEvaluator.calculate(PostfixEvaluator.java:124)位于Project2.PostfixEvaluator.eval(PostfixEvaluator.java:71)的Project2.ExpressionEvaluator.evaluate(ExpressionEvaluator.java:29),位于Project2.ExpressionEvaluatorTester.main(ExpressionEvaluatorTester.java: 32)
public Token calculate(Token opr, Token opd1, Token opd2)
{
// Get the first String from opr, it is the operator: +, -, ...
String oper = opr.getBody();
System.out.println(opr);
//Get the two operands by converting from String to int
int op1 = Integer.parseInt(opd1.getBody());
int op2 = Integer.parseInt(opd2.getBody());
//Default return value, in case an error occurs
int res = 0;
/**
* Alterations begin here
* Performs operation and sets value for res
*/
if(oper.equals("+"))
{
res …Run Code Online (Sandbox Code Playgroud) 我在一个投资组合网站的标题中有一个小的switch语句,它管理哪个链接显示在哪个页面上.$ id的值来自GET变量,即 - '?id = index'.
switch($id) {
case "index":
//Show links to content
case !"index":
//Show link to index
case !"about":
//show link to about page
}
Run Code Online (Sandbox Code Playgroud)
问题是NOT运算符在最后两种情况下不起作用.我希望索引的链接显示用户何时不在索引页面上,同样显示about页面.目前,所有链接都显示在索引页面上(当$ id =="index"时),NONE显示在任何其他页面上.
为什么会这样?
当使用switch语句时,有可用项目的详尽列表(例如a enum),并且如果每个项目都有自己的条件代码,我应该使用default标签吗?例如:
public class MyClass {
public enum Type {
TYPE1, TYPE2
}
private Type type;
public void withDefault() {
switch (type) {
case TYPE1:
// some conditional code for TYPE1
break;
default:
// some conditional code for TYPE2
break;
}
}
public void withoutDefault() {
switch (type) {
case TYPE1:
// some conditional code for TYPE1
break;
case TYPE2:
// some conditional code for TYPE2
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在那种情况下,我应该使用什么:withDefault()方法还是方法withoutDefault()?或者也许只是品味问题?
我正在使用Ruby on Rails 3.2.9和Ruby 1.9.3.我有以下case声明:
case
when private?
case
when not_active? then [:a, :b, :c, :d]
when active? then raise "private cannot be active"
else raise "not recognized"
end
when shared?
case
when not_active? then [:a, :b, :c]
when active? then raise "shared cannot be active"
else raise "not recognized"
end
when public?
case
when not_active? then [:a, :b]
when active? then [:a]
else raise "not recognized"
end
else raise "not recognized"
end
Run Code Online (Sandbox Code Playgroud)
如何重构上面的代码?
可能重复:
不间断切换
我正在制作一个小型计算器应用程序,当单击计算按钮时,会运行一个switch语句,但生成的数字不是它应该是的,我测试了我的输入,他们在这里工作的是switch语句:
switch (currentOp)
{
case "+" :
answer = Float.toString(inp1 + inp2);
case "-" :
answer = Float.toString(inp1 - inp2);
case "X" :
answer = Float.toString(inp1 * inp2);
case "/" :
answer = Float.toString(inp1 / inp2);
}
Run Code Online (Sandbox Code Playgroud) switch-statement ×10
java ×4
php ×2
refactoring ×2
c ×1
c# ×1
c++ ×1
character ×1
ruby ×1
swing ×1