尝试编写一个程序,将一个从65到90的字符数组(ch)值赋给它。然后,我尝试这样打印:
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z我试着用两个循环来做这件事,有很多麻烦。这就是我到目前为止所知道的:
import java.util.*;
public class Lab10a {
public static void main(String[] args) {
char ch[] =new char[26];
for (int x = 0; x < ch.length; x++){
ch[x] = (char)(x + 65);
for(int i = 0, i < ch.length; i++)[
}
}
//It doesn't need to be converted to a string here, this is just a method i tried.
System.out.println(Arrays.toString(ch) + ", ");
}
}这是我目前得到的打印输出:
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z], 抱歉,我的代码太不清楚了,真的不太确定该怎么做。有什么建议吗?
发布于 2013-11-17 10:19:02
像这样做
for (int x = 0; x < ch.length; x++){
ch[x] = (char)(x + 65);
}
for(int i = 0, i < ch.length; i++){
if((ch.length-1)==i){
System.out.print(ch[i]);
}else{
System.out.print(ch[i] + ", ");
}
}发布于 2013-11-17 10:20:51
String out = "";
for (int i = 0; i < ch.length; i++)
out += ", " + ch[i];
System.out.println(out.subString(2));发布于 2013-11-17 10:22:06
你可以试试这个
public static void main(String[] args) {
char ch[] =new char[26];
for (int x = 0; x < ch.length; x++){
ch[x] = (char)(x + 'A');
}
// System.out.println(Arrays.toString(ch));
boolean first = true;
for (char c : ch) {
if (! first) {
System.out.print(", ");
}
System.out.print(c);
first = false;
}
System.out.println();
System.out.flush();
}https://stackoverflow.com/questions/20026383
复制相似问题