所以我在一个简单的控制台应用程序中制作菜单.我的代码非常多:(与底部的实际代码链接!)
int input;
bool LOOPING = true;
while(LOOPING)
{
cout << "Select an option:\n";
cout << "1 - option 1\n";
cout << "2 - option 2\n";
cout << "3 - option 3\n";
cout << "4 - option 4\n>";
cin >> input;
switch(input) {
case 1:
game();
break;
case 2:
game();
break;
case 3:
game();
break;
case 4:
game();
break;
default:
cout << "ERROR: invalid input!\nPlease enter in a number!\n\n";
break;
}
}
// rest of app...
Run Code Online (Sandbox Code Playgroud)
我的问题是,程序只是进入一个恒定的文本循环!为什么会这样?为什么不default:阻止这种情况发生?如何阻止这种情况发生?
提前致谢! …
我试图在Android应用程序中使用switch语句,我必须检查一个整数是否等于Enum的某些值.代码如下:
public enum RPCServerResponseCode{
E_INCORRECT_LOGIN(4001),
E_ACCOUNT_DISABLED(4002),
E_ACCOUNT_NOT_ACTIVE(4003);
private int value;
private RPCServerResponseCode(int i) {
this.value=i;
}
public static RPCServerResponseCode getByValue(int i) {
for(RPCServerResponseCode dt : RPCServerResponseCode.values()) {
if(dt.value == i) {
return dt;
}
}
throw new IllegalArgumentException("No datatype with " + i + " exists");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的switch语句如下所示:
int errorCode;
switch(errorCode){
case RPCServerResponseCode.E_INCORRECT_LOGIN :
{
if (user.isAuthenticated)
{
// logout before login
}
break;
}
case RPCServerResponseCode.E_ACCOUNT_NOT_ACTIVE:
{
if (user.isAuthenticated)
{
//logout
}
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误说:"类型不匹配:无法从RPCCommucatorDefines.RPCServerResponseCode转换为int".有任何建议如何解决这个问题?提前致谢!!!
我在行中的代码中收到错误"切换表达式或大小写标签必须是bool,char,string,integral,enum或相应的可空类型",
switch (job_selecter.SelectedValue)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
private void start()
{
switch (job_selecter.SelectedValue)
{
case 0:
head_seal_label.Text = "Ravager's Seal: Head (8)";
break;
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么会发生这种情况以及如何解决这个问题?谢谢!
我设置了一个while循环,我想选择r或h,我不想使用forloops但我想使用开关为什么当我输入r或h时它会不断重复该案件的cout百万次?我不能只说一次..
while (chooseMove == 'r' or 'h')
{
switch (chooseMove)
{
case 'r':
cout << "you chose r";
break;
case 'h':
cout << "you chose h";
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我也试过forloops并遇到了同样的问题,我无法弄明白
这是我在PHPdoc中没有看到的东西switch()所以我不确定它是否可能,但我想要一个多条件的情况,例如:
switch($this) {
case "yes" || "maybe":
include "filename.php";
break;
...
}
Run Code Online (Sandbox Code Playgroud)
这是有效的语法/这是否可能与switch()声明?
处理以下情况的首选方法是什么:
switch (numberOfActualBytes)
{
case 1: return something1;
case 2: return something2;
case 3: return something3;
case 4: return something4;
}
Run Code Online (Sandbox Code Playgroud)
我肯定知道numberOfActualBytes由于使用过的合同是在范围内1-4.
我该如何编写不会导致not all code paths return a value错误的代码?
我怀疑我应该在这个函数结束时抛出一些异常default switch case,但是可能有一个更好的解决方案.
这是代码:
<?php
$r=rand(1,7) //get a random number from 1 to 7 inclusive...
switch($r) { //...and get a random quote
case 1:
echo '<p class="quote">welcome to Moonside.<br/>Wecomel to Soonmide.<br/>Moonwel ot Cosidme.</p>';
break;
case 2:
echo '<p class="quote">Hey! Parking Meters! And you\'re walking around!<br/>Ha ha ha... That\'s so funny.</p>';
break;
case 3:
echo '<p class="quote">"Yes" is "No" and "No" is "Yes."<br/>It makes perfect sense in Moonside.</p>';
break;
case 4:
echo '<p class="quote">Hello!<br/>And... good-bye!</p>';
break;
case 5:
echo '<p class="quote">This is a headline …Run Code Online (Sandbox Code Playgroud) 所以这是我的作业,对我而言,它似乎是一个可变范围的问题.我已经尝试定义一个全局,但也许我做错了,因为这似乎也不满足我的编译器.我目前正在使用SublimeText2进行代码编写,但似乎没什么帮助.我应该使用Eclipse或Aptana Studio等IDE吗?
// EmployeeBonus2.java - This program calculates an employee's yearly bonus.
import javax.swing.*;
public class EmployeeBonus2
{
public class Globals {
public double employeeBonus;
}
public static void main(String args[])
{
// Declare and initialize variables.
String employeeName;
String salaryString;
double employeeSalary;
String ratingString;
int employeeRating;
double employeeBonus;
final double BONUS_1 = .15;
final double BONUS_2 = .10;
final double BONUS_3 = .06;
final double NO_BONUS = 0.00;
final int RATING_1 = 1;
final int RATING_2 = 2;
final …Run Code Online (Sandbox Code Playgroud) 我试图在switch语句中改变我的char数组[10]的值,这样,如果我们有case 1,我的char数组[10] ="january",或者如果我们有case 2,那么array [10] ="二月"等等.问题是我得到错误信息,我知道我做错了什么.任何帮助将非常感激.这是我用Dev-C++编写的switch语句.
char month[10];
switch (i)
{
case 1:
month[10]="January";
cout<<month<<endl;
break;
case 2:
month[10]="February";
cout<<month<<endl;
break;
}
Run Code Online (Sandbox Code Playgroud) 我需要一些关于PHPExcel的帮助,我们有一个脚本可以将我们的Web应用程序的日志(如搜索,关键字等)输入到excel文件中.我希望脚本以这样的方式显示日志,即少数参数为黑色,少数为绿色.这是我需要修改的脚本部分:
$data = parse_command($log_command, $content);
$command = $data['command'];
$params = $data['params'];
if ($command == 'skip')
continue;
$curr_row++;
if ($save_to_excel)
{
$objPHPExcel->getActiveSheet()->getStyle('A'.$curr_row.':C'.$curr_row)->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getStyle('A'.$curr_row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$curr_row, $request_time)
->setCellValue('B'.$curr_row, $command)
->setCellValue('C'.$curr_row, $params);
}
Run Code Online (Sandbox Code Playgroud)
excel文件包含日志,搜索类型,关键字.所以我想在Black中进行几种类型的搜索,例如黑色的Venue搜索,Blue中的事件搜索,我希望我足够清楚.提前致谢.
switch-statement ×10
c++ ×3
php ×3
c# ×2
case ×2
java ×2
arrays ×1
char ×1
enums ×1
formatting ×1
phpexcel ×1
return ×1
scope ×1
variables ×1
while-loop ×1