我的理解switch()是它避免重复字符串.
如果是这样,为什么它不支持正则表达式,如下面的代码?或者我错过了什么意思switch()?
switch($username){
case "":
array_push($errors, "Username cannot be blank");
break;
case "admin":
array_push($errors, "Username cannot be 'admin'");
break;
case regex_switch('/xxx.*/'):
array_push($errors, "Username cannot begin 'xxx'");
break;
}
Run Code Online (Sandbox Code Playgroud) 如何对以下内容进行编码以便编译C#?编译器错误是:the label 'case 3:' already occurs in this switch statement.
switch (customerType)
{
case CustomerType.Private:
return externCustomerType.P;
case CustomerType.Business:
return externCustomerType.B;
case CustomerType.Interstate:
case CustomerType.Overseas:
return externCustomerType.I;
default:
return externCustomerType.N;
}
public enum CustomerType
{
Unknown = 0,
Private = 1,
Business = 2,
Interstate = 3,
Overseas = 3
}
Run Code Online (Sandbox Code Playgroud) 我正在为我的班级写一个罗马数字程序.我正在使用switch语句将字符串转换为整数.但是,当我运行它时,我得到一个不兼容的类型错误.我正在运行java 7,所以这不是问题.这是我的代码:
public static void main()
{
// Declare local variables
Scanner input = new Scanner(System.in);
String rNum;
int i;
int[] rArray;
// Display program purpose
System.out.println("This Program Converts Roman numerals to decimals");
System.out.println("The Roman numerals are I (1), V (5), X (10), L (50), C (100), D (500) and M (1000).");
System.out.print("Enter your roman numeral: ");
rNum = input.next().toUpperCase();
rArray = new int[rNum.length()];
for(i = 0; i < rNum.length(); i++){
switch(rNum.charAt(i)){
case "I": rArray[i] = 1;
break;
}
}
Run Code Online (Sandbox Code Playgroud) 当程序进入第二个循环时,即使我将第二个循环的值设置为false,它也会变为无限.问题出在哪里?第一个while循环我从来没有遇到任何问题退出,但第二个...我只是看不到在哪里或为什么.
int RareData::assignRareType()
{
int switchType;
std::cin >> switchType;
bool valid = true;
bool valid2 = true;
while (valid)
{
switch (switchType)
{
case 0:
std::cout << "1 - Character Change\n2 - Account Change\n3 - Both\n";
int switchData;
std::cin >> switchData;
valid2 = true;
while (valid2)
{
switch (switchData)
{
case 1:
valid2 = false;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
assignCharacter();
break;
case 2:
valid2 = false;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
assignAccount();
break;
case 3:
valid2 = false;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
assignAccount();
assignCharacter(); …Run Code Online (Sandbox Code Playgroud) 通常我会在返回特定对象的方法中实现switch case.如下所示:
private string testing(string input){
switch(input)
{
case "a":
{
....
return "TestingResult";
}
case "b":
{
....
return "TestingResultB";
}
default:
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道是否可以为分配值编写一个开关案例?如下所示:
private string testing(string input){
string TEST="";
switch(input)
{
case "a":
{
....
TEST = "TestingResult";
}
case "b":
{
....
TEST = "TestingResultB";
}
default:
}
return TEST;
}
Run Code Online (Sandbox Code Playgroud)
因为它可以通过简单的If-Else语句实现,这个问题对我来说是了解switch case的更多功能
当然,经过测试,我收到错误
control cannot fall through from one case label('case: "a"') to another
Run Code Online (Sandbox Code Playgroud) 我试图养成使用switch/case而不是if的习惯.我有以下几点:
switch($res['perm']){
case '0': $perm = "Invalid";
case '1': $perm = "Operator";
case '2': $perm = "Team Lead";
case '3': $perm = "Admin";
default: $perm = "Unknown - ".$res['perm'];
}
Run Code Online (Sandbox Code Playgroud)
哪里$res['perm'] = (int) 3..我试过没有单引号,但它仍然默认为"未知"..我做错了什么?
switch(type)
{
case 'home':
console.log('home switch');
break;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有写入控制台,以下内容也没有:
switch(type)
{
case "home":
console.log('home switch');
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,以下内容:
if (type == 'home')
{
console.log('home if');
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么.这不是一个显示阻止,我可以使用该if声明,但我真的很好奇为什么会这样.
注意:这些声明是直接替换,此处无需考虑.范围没有变化,没有代码我没有提到可能会干扰价值type.
javascript if-statement switch-statement conditional-statements string-evaluation
我目前正在尝试完成此程序,但是当我测试我的switch语句时,它直接进入我的默认情况并说我输入了无效信息.
我的任务
我必须收到用户一个月的时间并将其发送到我的case语句,以便为特定情况执行我的代码.您可能会注意到,每个案例中都有一个密钥,此密钥用于个人目的.请忽略.
我的问题
case语句直接进入我的默认语句,该语句向用户发出无效的信息消息.
我的进步
这将是我的完整程序,除了我的case语句将每个月识别为无效输入之外,一切似乎都正常工作
// Import Libraries
import javax.swing.*;
import java.util.*;
import java.io.*;
// This is my program.
public class DateCalc
{
public static void main (String[] args)
{
String month;
String day;
String inputYear;
Scanner keyboard = new Scanner(System.in);
// receiving input for my age variable
System.out.print( "Please Enter The Month Of The Date :");
month = keyboard.nextLine();
// receiving input for my weight variable
System.out.print( "Please Enter The Day Of The Date : ");
day …Run Code Online (Sandbox Code Playgroud) 我是Objective-C的新手,但是使用JavaScript和许多VB.NET以及一些C进行固件开发.我正在编写初级应用程序,在这种情况下,我的View上有一个SWITCH控件.我想知道如何使用IF语句来测试它的ON/OFF状态.示例代码是最好的,因为我在法律上是盲目的,并且难以在线阅读大量文本.
Stepper控件的相同问题(带有+/_按钮的控件用于递增/递减值.)
谢谢.
所以我有这个代码:
package com.sunil.phpconnect;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button nao;
Button foto;
Button novamen;
TextView ola;
HttpClient httpclient1, httpclient;
HttpGet request1, request;
HttpResponse response1, response;
String url,iduser, urlmensagens;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
iduser= extras.getString("userid");
TextView result = (TextView) findViewById(R.id.tvResult);
//String g = result.getText().toString();
// String …Run Code Online (Sandbox Code Playgroud) switch-statement ×10
java ×3
c# ×2
if-statement ×2
php ×2
android ×1
arrays ×1
c++ ×1
controls ×1
enums ×1
ios ×1
javascript ×1
objective-c ×1
regex ×1
while-loop ×1