Java If / else字符更改

时间:2015-05-10 00:24:44

标签: java if-statement

我是Java的新手,我自己学习。这是我遇到的一个问题。

我想知道我们是否可以将字符串变量设置为一个,然后使用if else语句将其更改为其他内容?

我在想代码:

String superscript = "th";
while (i<=10){ 
      if (i==1){
      String superscript = "st";
      }
      System.out.println("the " + i + superscript +" element is: " + X);
      i++;
}

基本上,我想要的是,如果我= = 1,它显示第1,i == 2,它显示第2,依此类推,只有一个字符串变量&#34;上标。&#34 ;

而且,如果条件要将上标改为&#34; nd&#34;当我== 2? 我如何在这里使用if语句?

5 个答案:

答案 0 :(得分:2)

您可以重新分配给现有变量,但为此,您需要省略类型声明;当您指定类型时,您将声明一个新变量(尽管名称相同)。那就是:

while (i <= 10){
   String superscript = "th"; 
   if (i == 1){
     superscript = "st";
   }
   System.out.println("the " + i + superscript +" element is: " + X);
   i++;
 }

...重新分配给现有的superscript变量。话虽如此,我们可以通过使用for循环并为此格式创建子例程来进一步改进:

for (int i = 0; i < 10; i++) {
     String element = arr[i];
     System.out.println("the " + asOrdinal(i+1) + " element is: " + element);
}

...有辅助功能&#34; asOrdinal&#34;定义如下:

private static String asOrdinal(int index) {
   String suffix = "th";
   int onesDigit = index % 10;
   switch (onesDigit) {
     case 1:
       suffix = "st";
       break;
     case 2:
       suffix = "nd";
       break;
     case 3:
       suffix = "rd";
       break;
     default:
       suffix = "th";
       break;
   }
   return Integer.toString(index) + suffix;
}

请注意,我上面的默认情况有些多余(字符串已经使用此默认值初始化),但是用于说明&#34;默认情况&#34;在switch语句中工作。你也可以使用&#34; return&#34;在每个不同的情况下,而不是分配给相同的局部变量。我通常喜欢使用&#34; return&#34;,因为重复分配给相同的变量通常是代码气味(或者说,它不是&#34;功能性&#34;样式,这倾向于是我的偏好)。简而言之,正如您可以从这里的许多答案中看到的那样,有多种不同的方法可以做同样的事情。

你应该得到的关键是:

// Outer scope
TypeOfVariable identifier [= initialization_expression1];

// Inner scope
{
   TypeOfVariable identifier [= initialization_expression2];

   // ... usage ...
}

...在上面,&#34;标识符&#34;的内部用法正在引用在内部作用域中声明的变量。此变量与外部作用域中声明的内存位置不同(但是,如果它是一个对象指针,如果它们已使用同一对象的地址分配/初始化,则它们都可以指向相同的值)。 / p>

相比之下:

// Outer scope
TypeOfVariable identifier [= initialization_expression1];

// Inner scope
{
   identifier = expression2;

   // ... usage ...
}

...在上面,&#34; =&#34;语句不是初始化而是赋值,内部和外部作用域中的两个变量都是相同的(内部作用域中的语句替换/赋值)。

答案 1 :(得分:1)

String superscript = "th";
int i = 0;
while (i<=10){ 
    if (i==1){
        superscript = "st";
    }else if (i==2){
        superscript = "nd";
    }else if (i==3){
        superscript = "th";
    }
    System.out.println("the " + i + superscript +" element is: " + X);
    i++;
}

答案 2 :(得分:1)

你的问题是你创建了一个名为&#34;上标&#34;的新局部变量。在if语句中,但在println中,您使用在while语句之外声明的语句。你不应该在&#34;上标&#34;之前写String。在if声明中。

编辑:我迟到了。)

答案 3 :(得分:1)

这是switch case结构的经典情况:

int i=1;
String superscript;
while (i<=10) {
  switch (i) {
  case 1:
    superscript = "st";
    break;
  case 2:
    superscript = "nd";
    break;
  case 3:
    superscript = "rd";
    break;
  default:
    superscript = "th";
  }
  System.out.println("the " + i + superscript +" element is: " + X);
}

答案 4 :(得分:0)

只需重新分配它,而不是创建新变量。 另外,如果你在while循环中移动初始的'th'定义,你将避免使用另一个if语句。

请记住,'青少年'号码是特殊的,但除此之外,序数后缀取决于号码中的最后一位数字。

public class Ordinal {

    public static void main(String[] args) {
        int i = 1;
        while (i <= 10) {
            String superscript = "th";
            if (i == 1) {
                superscript = "st";
            }
            if (i == 2) {
                superscript = "nd";
            }
            if (i == 3) {
                superscript = "rd";
            }
            System.out.println("the " + i + superscript + " element is: ");
            i++;
        }
    }
}
相关问题