作业帮 > 综合 > 作业

用Java写一个程序:使其输出从A-Z的排序字母

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/21 11:30:33
用Java写一个程序:使其输出从A-Z的排序字母
要求:给定一个数(如int count = k),k是个可变量(可以是任意大于0的int值,如k = 1237),写一个for(int i=1;i=2,不足的以A补足),如此类推一直到i=count.这里假设如果到i=count时输出的是name="BFN",则前面当i = 1时,也应当则输出name = "AAA"(就是说输入的位数要相等,不足的以A补足).
如果给出好的代码的话再附加50赏分,能运行得到想要的结果并被采纳的至少也加30赏分.
按照你的要求写的如下代码,精短而高效,可以直接运行
public class Luck {
\x09public static void main(String[] args) {
\x09\x09int count = 1237;//count,手动指定范围
\x09\x09// 确定字符串的长度
\x09\x09int length = 1;
\x09\x09int temp = count;
\x09\x09while ((temp = temp / 26) > 0) {
\x09\x09\x09length++;
\x09\x09}
\x09\x09// 从1输出到count
\x09\x09for (int a = 1; a 0) {
\x09\x09\x09\x09s = (char) ('A' + i % 26) + s;
\x09\x09\x09\x09i = (i - i % 26) / 26;
\x09\x09\x09}
\x09\x09\x09while (s.length() < length) {
\x09\x09\x09\x09s = 'A' + s;
\x09\x09\x09}
\x09\x09\x09System.out.println(s);
\x09\x09}
\x09}
}
再问: 当输入count < 26时,只输出一位,这里要求最少输出两位(当count = 1时输出"AA")。当count = 676时输出"ZZ",当count = 677时输入的是从"AAA"—>"BAA"。
再答: 你好,代码修改成功,请试试 public class Luck { public static void main(String[] args) { int count = 677;// count,手动指定范围 // 确定字符串的长度 int length = 0; int temp = count - 1; while (temp > 0) { temp = (temp - temp % 26) / 26; length++; } temp = Math.max(temp, 2);// 保证输出的最小长度 // 从1输出到count for (int a = 1; a 0) { s = (char) ('A' + i % 26) + s; i = (i - i % 26) / 26; } while (s.length() < length) { s = 'A' + s; } System.out.println(s); } } }
再问: 当int count = 26*26*26+1时输出的就有不对了(输出的是……ZZZ和BAAA),位数不太对,就是length的控制还不太正确。就为这个length的值让我想了好久哦。
再答: 你好,这回终于搞定了,终于明白你的意思了 AA~ZZ,BAA~ZZZ,BAAA~ZZZZ,BAAAA~ZZZZZ... public class Luck { public static void main(String[] args) { int count = 26 * 26 * 26 + 1;// count,手动指定范围 // 从1输出到count for (int a = 1; a 0) { s = (char) ('A' + i % 26) + s; i = (i - i % 26) / 26; } // 确定字符串的长度 i = a; int length = 0; while ((i /= 26) > 0) { length++; } // 保证输出的最小长度 length = Math.max(length, 2); // 不足的前面补A while (s.length() < length) { s = 'A' + s; } System.out.println(s); } } }
再问: 不好意思,是我说得不太清楚。我想要的是这样的:由count值决定要输出多少字符串,当最后输出的是ZZ时,前面的就是AA、AB~ZZ,当最后输出的是ZZZ时,前面的就是AAA、AAB~ZZZ。就是说输出的字符(最少两位),随着最后输出的字符串长度决定前面字符串的长度。如最后输出BAAA,第一个输出的就是AAAA、第二个是AAAB,即从AAAA~BAAA。谢谢。
再答: 你好,留下QQ吧,你没发现你最后说明的这个需求,不正是我那倒数第二段代码吗?