我正在使用Microsoft Visual Studio 2013
我目前有这个switch语句:
switch (option)
{
case 1:
getline(cin, newname);
cout << "What would you like your new username to be?\nName: ";
getline(cin, newname);
name = newname;
cout << "\nYour username is now '" << name << "' with your balance being $" << balance << "\n";
options();
break;
case 2:
cout << "\nOpenning cheat menu\n";
cheats();
break;
default:
cout << "\nExitting to main title...\n";
title();
break;
}
Run Code Online (Sandbox Code Playgroud)
你可以忽略案例2和默认情况,只是显示所以你知道这些案例是有效的.案例2和默认工作完全正常,但案例1表现得很奇怪.正如你在案例1中看到的,我有两个getline(cin,newname); 您可能认为我应该只有一个,但由于某种原因,我需要两个才能使此代码正常运行.
如果我只有其中一个代码跳过用户应该给出的输入.如果我有两个它正常工作.
我问过这个人他们也不明白,因此:为什么这样做.
我正在为Android编写基于文本的RPG游戏,以便熟悉Java和XML.我想在按钮的ListView中显示一个字符串数组选项,并且根据用户按下的按钮,我想用一组新选项相应地更新活动.
例如,假设游戏开始时有以下选择:
{option1, option2, option3, option4}
Run Code Online (Sandbox Code Playgroud)
如果用户点击option2,我想显示:
{2-option1, 2-option2, 2-option3, 2-option4}
Run Code Online (Sandbox Code Playgroud)
或者说让用户点击4而不是2.然后:
{4-option1, 4-option2, 4-option3, 4-option4}
Run Code Online (Sandbox Code Playgroud)
所以,我试图确定一种速度有效/内存有效的方法来确定下一个String[]显示.
最初我正在考虑构建一个GIANT if-then-else树(或switch语句 - 无关紧要),其中每个可能的选项都会有一个if语句.例如:
// choice是用户选择的选项
if(choice==option1){
return {"1-option1", "1-option2", "1-option3"}
}else if(choice== 1-option1 ){
return {"1-1-option1","1-1-option2","1-1-option3"}
Run Code Online (Sandbox Code Playgroud)
并且List将继续,直到每个可能的选择返回String [],该String []对应于要显示的下一组选项.
然后,我开始思考使用对象可能更干净,更容易:
MyObject newObject = MyObject(choice)
newObject.getOptions()
Run Code Online (Sandbox Code Playgroud)
where choice是一个构造函数参数,用于将a初始化String[]为正确的下一组选项; 和getOptions()返回此String[]
但是,这将是很多对象,我不确定它会简化if/switch语句方法.
那么,有什么想法可以有效地处理这样的事情吗?
我正在尝试处理组合用户输入以及要处理的交换机情况,并且它似乎一直顺利,直到最后一次切换
System.out.println("\t output switch = " + state.get(2));
switch(state.get(2)){
//Case MCNP
case 0:
{
abundances = verifyAndNorm(abundances, new MCNPVerifier(MCNP));
out = toMCNP(mat, abundances);
System.out.println("\t MCNP");
}
//Case SCALE
case 1:
{
abundances = verifyAndNorm(abundances, new SCALEVerifier(SCALE));
out = toSCALE(mat, abundances, weightFracFlag);
System.out.println("\t SCALE");
}
}
Run Code Online (Sandbox Code Playgroud)
打印出来
output switch = 0
MCNP
SCALE
Run Code Online (Sandbox Code Playgroud)
结果是out = toScale(...),并且由于它同时打印MCNP和SCALE,它必须同时打击两种情况,但它只适用于一个......
我在这里错过了什么?
#include <stdio.h>
int main()
{
int i = 0;
char c = 'a';
while (i < 2){
i++;
switch (c) {
case 'a':
printf("%c ", c);
break;
break;
}
}
printf("after loop\n");
}
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是什么?第二次休息是否意味着什么?
所以基本上我想创建一个键盘,当用户单击一个字母时,应该会发生一些事情:
@IBAction func letterBtn(sender: UIButton) { // All the letter buttons are linked to this func.
switch sender.currentTitle! {
case "A":
moveLetters(sender)
case "B":
moveLetters(sender)
case "C":
moveLetters(sender)
case "D":
moveLetters(sender)
case "E":
moveLetters(sender)
case "F":
moveLetters(sender)
case "G":
moveLetters(sender)
default :
println("Error")
}
}
func animateLetter (pos: UILabel, btn: UIButton) { // Make the letter move towards a label.
UIView.animateWithDuration(0.5, animations: { () -> Void in
btn.center = pos.center
})
}
func moveLetters (btn: UIButton) { // Determine which …Run Code Online (Sandbox Code Playgroud) 我正在查看Lourakis&Argyros 的稀疏包调整库(sba)的源代码.更确切地说,我正在研究以下函数nrmL2xmy,该函数计算两个向量的平方L2差.sba_levmar.c从行开始从文件中复制以下代码146:
/* Compute e=x-y for two n-vectors x and y and return the squared L2 norm of e.
* e can coincide with either x or y.
* Uses loop unrolling and blocking to reduce bookkeeping overhead & pipeline
* stalls and increase instruction-level parallelism; see http://www.abarnett.demon.co.uk/tutorial.html
*/
static double nrmL2xmy(double *const e, const double *const x, const double *const y, const int n)
{
const int blocksize=8, bpwr=3; /* 8=2^3 …Run Code Online (Sandbox Code Playgroud) 我想知道在执行代码块时是否有可能在Javascript switch语句中使用类似于“ this”的东西。
例如:
switch(x) {
case 'The pyramids of Giza':
console.log(this);
break;
case 'The Leaning Tower of Pisa':
console.log(this);
break;
default:
console.log('Not found');
}
Run Code Online (Sandbox Code Playgroud)
是相同的:
switch(x) {
case 'The pyramids of Giza':
console.log('The pyramids of Giza');
break;
case 'The Leaning Tower of Pisa':
console.log('The Leaning Tower of Pisa');
break;
default:
console.log('Not found');
}
Run Code Online (Sandbox Code Playgroud)
纯粹出于效率目的,谢谢!
我知道这是一个愚蠢的问题,但我想知道案例标签为什么不采用变量.代码是 -
public class Hello
{
public static void main(String[] args)
{
final int y=9;
int a=1,b=2,c=3;
switch(9)
{
case y:
{
System.out.println("Hello User");
break;
}
case a:
{
System.out.println("Hello World");
break;
}
case b:
{
System.out.println("Buff");
break;
}
default:
{
System.out.println("Yo bitch");
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然,我已初始化a,b和c,但它显示错误.为什么?
我有以下代码片段.
var caseObj = function () {
}
switch (typeof caseObj) {
case "function":
console.log("it is function");
case "object":
console.log("It is object now");
}
Run Code Online (Sandbox Code Playgroud)
它的输出是
it is function.
It is object now.
Run Code Online (Sandbox Code Playgroud)
但是typeof caseObj给出输出function但它仍然评估
案例"对象"的情况.
怎么可能?我做错了吗?
编辑:
typeof caseObj给予function,所以它执行该案件,但它也执行object案件.为什么这种奇怪的行为?
我需要实现具有动态值的switch case语句,如下所示.
int v1 = 5;
int key = xx.getKey();
switch (v1) {
case key:
.......
case key+1:
.......
case key+2:
.......
default:
.......
}
Run Code Online (Sandbox Code Playgroud)
使用它时,java抱怨"case表达式必须是常量表达式".有没有办法在案例中使用动态值.(开关变量也是动态值)
switch-statement ×10
java ×4
c ×2
javascript ×2
android ×1
c++ ×1
case ×1
debugging ×1
dynamic ×1
for-loop ×1
function ×1
if-statement ×1
optimization ×1
performance ×1
swift ×1
visual-c++ ×1
xcode ×1