简体   繁体   中英

Printing lower case letters with corresponding ASCII code using a for loop

So far this is what I have

System.out.println("problem 8");
int charletter = 0;
int charletter1 = 'a';
{
for (charletter = 'a'; charletter < 123; charletter++)
System.out.println(charletter);
System.out.println(charletter1);
}
}

The result is this list of according numbers without the letters as so:

Please enter the problem you want to solve:
8
problem 8
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
97

How do I make it so I can get az to show up next to or under their according number as well as getting rid of the 97 at the very end? Thanks everyone.

cast to char like so:

for (charletter = 'a'; charletter < 123; charletter++)
       System.out.println(charletter + "" + (char)charletter);

then get rid of System.out.println(charletter1); to avoid the 97 at the end.

To get rid of the 97, remove the second System.out.println . The for loop only operates on the first System.out.println . To get the answer you want, use this statement instead of the first System.out.println :

for(char charletter = 'a'; charletter <= 'z'; charletter++) {
    System.out.printf("%c %d%n", charletter, (int) charletter);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM