我正在回答Java测试并遇到问题:
下列哪项为真?
A.在断言语句中,冒号(:)之后的表达式可以是任何Java表达式.
B.如果交换机块没有默认值,则认为添加断言默认值是合适的.
C.在断言语句中,如果冒号(:)之后的表达式没有值,则断言的错误消息将为空.
D.使用catch子句处理断言失败是合适的.
正确的答案是B.说实话,我通过排除另一个明显错误的案例回答了这个问题,但实际上我无法理解这个问题.谁能解释为什么这是真的?哪里有帮助?
我想知道如何执行switch- case语句:
例
说一个有以下代码:
Scanner sc = new Scanner(System.in);
int v = sc.nextInt();
switch(v) {
case 0 :
System.out.println("Zero");
break;
case 1 :
System.out.println("One");
break;
case 2 :
System.out.println("Two");
break;
//...
default :
System.out.println("No one digit number");
}
Run Code Online (Sandbox Code Playgroud)
可以这样实现:
if(v == 0) {
System.out.println("Zero");
}
else if(v == 1) {
System.out.println("One");
}
else if(v == 2) {
System.out.println("Two");
}
//...
else {
System.out.println("No one digit number");
}
Run Code Online (Sandbox Code Playgroud)
但是更有效的程序是:
if(v >= 0 && v <= 9) {
if(v …Run Code Online (Sandbox Code Playgroud) switch (i) {
case ("+" || "/"):
setOperator("i");
break;
}
Run Code Online (Sandbox Code Playgroud)
在Java中执行此操作的最佳方法是什么?
我正在我的程序中捕获一个网络数据包作为一个char*数组.数组的第一个字节表示我收到的数据包的类型,对于每种类型,我希望我的通用Packet基类的单独子类传递给字节数组以及它将被解释的位置.
我想避免使用switch语句,其中将评估数据包的第一个字节并调用相应的构造函数.首先,因为在OOP中你应该避免使用switch子句,其次是因为每次添加数据包类时我都不想在switch语句中添加单独的case.
我查看了工厂方法模式,但我不确定在这种情况下它是如何帮助我的,或者它是否能解决我的问题.
基本上我想要避免的是在10个不同的地方编辑我的代码只是为了添加一个数据包类.
试图编写一个程序来计算盒子的体积,表面积或"周长".我希望能够为每个主题输入一个字母,然后在计算该主题后,输入下一个主题的字母.该程序还希望在键入Q时结束.我相信我的数学是正确的,但这个程序对我不起作用.它编写得很好,但音量根本不会计算,尽管其他两个主题会.我还得到了三行答案,当我输入第二个字母作为第二个值时,程序就疯了.任何和所有的帮助非常感谢.谢谢你的时间.
include <iostream>
include <iomanip>
using namespace std;
const char SENTINEL = 'Q';//found this suggestion online to quit a program
int main ()
{
char V, A, G, inputChar;
float length, width, depth, totalV, totalA, totalG;
cout<<"Enter character V for Volume, character A for Surface Area,\n";
cout<<"character G or Girth plus Depth or Q to quit\n"<<endl;
cin>>inputChar;// pick letter
while (inputChar!=SENTINEL)// if not Q then check the if-ele statments
{
switch (inputChar)
case 'V':
{
cout<<"Enter Length, Width, and Depth …Run Code Online (Sandbox Code Playgroud) 关于C ++中的这个问题,存在类似的问题,但是我在这里使用JavaScript。我基本上和其他帖子中的OP处于相同的情况。
var input = prompt();
while(true) {
switch(input) {
case 'hi':
break;
case 'bye':
//I want to break out of the switch and the loop here
break;
}
/*Other code*/
}
Run Code Online (Sandbox Code Playgroud)
反正有这样做吗?
我/*Other code*/在代码也应该中断的部分中也有多个开关。
这是一段代码,它给了我错误:
const char* name = pAttr->Name(); // attribute name
const char* value = pAttr->Value(); // attribute value
switch(name) // here is where error happens: must have integral or enum type
{
case 'SRAD': // distance from focal point to iso center
double D = atof(value);
break;
case 'DRAD': // distance from iso center to detector
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
这switch(name)是发生错误的地方.它说它是一个整体或枚举类型.那么我该怎么做一个char*类型的switch case或者等价?
我有一个头文件中定义enum的成员structure.例如,
struct abc{
enum xyz{
FIRST =1;
SEC =2;
}qw;
};
Run Code Online (Sandbox Code Playgroud)
在我的.cpp文件中,我已经包含了这个标题.我有一个switch case在我的文件中,这些enums将被用作case constants.
struct abc az;
switch(az.qw){
case FIRST:....
case SEC:...
default:..
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误FIRST is not declared in this scope.如何克服这个问题.
public AlertStatus nextStatus(){
int randNum = randNumGen.nextInt(3);
switch(randNum){
case 0: return new AlertStatusGreen();
case 1: return new AlertStatusYellow();
case 2: return new AlertStatusRed();
default: System.out.println("ERROR: no random number.");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我必须为学校准备的课程的一种方法。开关采用随机整数,并使用它返回从AlertStatus类派生的某个特定类的对象。
由于某种原因,我在上面的代码块的第9行(上面的代码的最后一行)中始终收到错误消息“缺少返回语句}”。我不明白为什么这样说,尽管我已经看到每种情况的返回语句。
使用switch block时遇到"变量可能未初始化"错误.
这是我的代码:
public static void foo(int month)
{
String monthString;
switch (month)
{
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
}
System.out.println(monthString);
}
Run Code Online (Sandbox Code Playgroud)
错误:
Switch.java:17: error: variable monthString might not have been initialized
System.out.println (monthString);
Run Code Online (Sandbox Code Playgroud)
据我所知,当您尝试访问尚未初始化的变量时会发生此错误,但是当我在交换机块中为其分配值时,我是否正在初始化它?
同样,即使月份是编译时常量,我仍然会收到相同的错误:
public static void foo()
{
int month = 2;
String monthString;
switch (month)
{
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
}
System.out.println(monthString);
}
Run Code Online (Sandbox Code Playgroud) switch-statement ×10
c++ ×4
java ×4
while-loop ×2
assert ×1
break ×1
char ×1
constructor ×1
enums ×1
javascript ×1
return ×1
string ×1
struct ×1
variables ×1