作为 Java SE 12 的一部分,引入了switch表达式,并且从 Java SE 14 开始,它们已经标准化。它们与switch陈述有何不同?
我正在创建一个具有文本图像网格的应用程序,每个应用程序打开一个不同的活动.它工作正常,但只是为设计目的,我想取代我if-else statements与switch statements(我假设我能做到),但它不工作.现在我在每个图像上设置标签的工作代码是:
if(position == 0)
textView.setText(R.string.zero);
else if(position == 1)
textView.setText(R.string.one);
else if(position == 2)
textView.setText(R.string.two);
else if(position == 3)
textView.setText(R.string.three);
else if(position == 4)
textView.setText(R.string.four);
else if(position == 5)
textView.setText(R.string.five);
ect....
Run Code Online (Sandbox Code Playgroud)
我想用:
switch(position)
case 0:
textView.setText(R.string.zero);
case 1:
textView.setText(R.string.one);
case 2:
textView.setText(R.string.two);
case 3:
textView.setText(R.string.three);
case 4:
textView.setText(R.string.four);
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,标签是我定义的最后一个(在我的例子中它将是"四").我也有一个类似的代码,每个对象开始intent与position变量不同但是相反,并使每个意图等于第一个.我的语法错了还是不适用于我的情况?
在Java中是否有一种方法可以在一天结束时使用序数字符格式化日期
就像是 :
七月一日?
我到处都搜索过,在任何地方找不到它?
对于以下switch语句:
如果选择0-9之间的值,则输出正常.如果选择大于9的值,则输出始终为小写z.
for (int i = 0; i < 3; i++)
{
random[i] = randomnumber.nextInt(36);
if (random[i] > 9)
{
switch(random [i])
{
case 10: character[i] = "A";
case 11: character[i] = "B";
case 12: character[i] = "C";
case 13: character[i] = "D";
case 14: character[i] = "E";
case 15: character[i] = "F";
case 16: character[i] = "G";
case 17: character[i] = "H";
case 18: character[i] = "I";
case 19: character[i] = "J";
case 20: character[i] = "K";
case 21: …Run Code Online (Sandbox Code Playgroud)