【发布时间】:2021-03-14 05:39:44
【问题描述】:
我希望能够排序一个句子样本输入Python:PHP:C++:C:Java:HTML
不使用数组或数组方法对句子进行排序。
输出应该是这样的;
Top programming languages in alphabetical order:
1. C
2. C++
3. HTML
4. Java
5. PHP
6. Python
这是我开始使用的,但被卡住了,在互联网上找不到任何不使用 Array 的东西。
import java.util.*;
public class SortProgram {
public static void main(String[] args) {
// Declare Variables
String topLang = "";
int separator = 0;
String holder = "";
String top = "";
// Create a Scanner object attached to the keyboard
Scanner input = new Scanner(System.in);
// input
System.out.print("Enter a list of the top programming languages: ");
topLang = input.next();
// separate each word
while (topLang.length() > 0) {
separator = topLang.indexOf(":");
holder = topLang.substring(0, separator);
topLang = topLang.substring(separator, topLang.length());
}
System.out.println(" Position Language");
System.out.println("==========================");
}
}
【问题讨论】:
-
为什么不想使用数组?
String[] words = topLang.split(":");
标签: java loops while-loop