samedi 9 mai 2015

Java If/else Characters change

I'm very new to Java, and learning on my own. Here's a problem I'm struggling with.

I'm wonder if we can set a string variable to one thing then use if else statements to change it to something else?

What I am thinking with codes:

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

Essentially, what I want is that if i==1, it displays 1st, and i==2, it displays 2nd, and so on and so forth with only one string variable "superscript."

And, how do I make two if conditions to change superscript to "nd" when i==2? How do I use if statement here?

Thanks very much.

And, please recommend a good resource to study/practice Java if you can :) I'm currently reading "Head First Java 2nd" and watching youtube java tutorials. Thanks again.

Aucun commentaire:

Enregistrer un commentaire