好吧,所以我的代码不起作用:我试图在"降序"中安排输入的字符串。和#34;升序"但有时字符串只会在列表中出现(按照正确的顺序排列,或者根本不会进入降序/升序字符串)
import java.util.Scanner;
public class Stringseries
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input 'quit'");
String encore = scanner.nextLine();
int loop = 0;
String smallest = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; // we set a "smallest" string to know where to put the new string in the "descending" and "ascending" strings.
String longest = "";
String ascending = "";
String descending = "";
String lastInput = "";
while (!encore.equals("quit")) {
loop = ++loop;
encore = encore.replaceAll("\\s+",""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces.
if (loop == 1) {
descending = encore;
ascending = encore;
} if (loop >= 2) {
if (encore.length() < smallest.length()) {
descending = descending + " " + encore;
ascending = encore + " " + ascending;
} if (encore.length() > longest.length()) {
descending = encore + " " + descending;
ascending = ascending + " " + encore;
}
}
if (longest.length() < encore.length()) {
longest = encore;
} if (smallest.length() > encore.length()) {
smallest = encore;
}
System.out.println("Enter the string you want to put in your sequence of strings");
lastInput = encore;
encore = scanner.nextLine();
}
if (descending != null && !descending.isEmpty()) { // we check to see if the "descending" string is empty (we could do this with "ascending" mind you).
System.out.println("Here are your strings in ascending order : " + ascending);
System.out.println("Here are your strings in descending order : " + descending);
System.out.println("Here is the longest string : " + longest);
} else if (descending == null | descending == "") {
System.out.println("You have not entered any strings, therefore the program doesn't display any string :("); // customised message.
}
} // end Method
} // end Class
答案 0 :(得分:1)
我会采取完全不同的方法。你的本土是非常本土的,Java内置了可以做到这一点的东西,最重要的是这里,Stream API和Comparators
String quitString = "quit";
List<String> userInputList = new ArrayList<>();
try(Scanner scanner = new Scanner(System.in)){ // This is called a "try with resources"
System.out.println("Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input \"" + quitString + "\"." + System.lineSeparator());
String encore = scanner.nextLine();
while(!encore.equalsIgnoreCase(quitString)){
encore = encore.replaceAll("\\s+", ""); // this way, the length of the strings is only defined by the characters in the string, and not characters + whitespaces.
System.out.println("Enter the string you want to put in your sequence of strings");
encore = scanner.nextLine();
if(encore != null && !encore.isEmpty() && !encore.equalsIgnoreCase(quitString)) {
userInputList.add(encore);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
List<String> ascending =
userInputList.stream()
.sorted((strA, strB) -> strA.length() - strB.length())
.collect(Collectors.toList());
List<String> descending =
userInputList.stream()
.sorted((strA, strB) -> strB.length() - strA.length())
.collect(Collectors.toList());
StringBuilder sbAscending = new StringBuilder();
sbAscending.append("Here are your strings in ascending order: ");
ascending.forEach(userInput -> {
sbAscending.append(System.lineSeparator() + userInput);
});
System.out.println(sbAscending.toString());
StringBuilder sbDescending = new StringBuilder();
sbDescending.append("Here are your strings in descending order: ");
descending.forEach(userInput -> {
sbDescending.append(System.lineSeparator() + userInput);
});
System.out.println(sbDescending.toString());
输出:
Start the sequence by inputting a string DIFFERENT than 'quit'. When you DO want to end it, input "quit".
Start
Enter the string you want to put in your sequence of strings
test
Enter the string you want to put in your sequence of strings
test2
Enter the string you want to put in your sequence of strings
test23
Enter the string you want to put in your sequence of strings
test234
Enter the string you want to put in your sequence of strings
quit
Here are your strings in ascending order:
test
test2
test23
test234
Here are your strings in descending order:
test234
test23
test2
test
答案 1 :(得分:1)
假设你想要自己做事,因为这似乎是一个练习任务。否则请使用j.seashell的答案。
您当前的代码只能将值输入到列表的末尾。这意味着如果您输入
测试
第二次测试
第三次测试
前两个输入后的结果将是
ascending = "Test SecondTest"
descending = "SecondTest Test"
你的下一个值应该介于这两个之间,所以正确的结果会变成
ascending = "Test ThirdTest SecondTest"
descending = "SecondTest ThirdTest Test"
但您的代码现在只能附加到字符串。 您还可以过滤掉不是最短或最长字符串的字符串。要解决此问题,您必须实现某种方式来拆分列表,并在拆分值的中间插入值。这可以通过多种方式完成,例如
List<String> ascending;
ascending.split(" ");
最简单的方法是使用Javas内置的List结构,即
List<String> ascending = new ArrayList<>();
然后可以将
boolean inserted = false;
//We loop to the correct location and add it
for(int i = 0; i < ascending.size(); i++) {
if(ascending.get(i).length() > encore.length()) {
ascending.add(i, encore);
inserted = true;
break;
}
}
//If it wasn't inserted its the longest one yet, so add it at the end
if(!inserted) {
ascending.add(encore);
}
您可以使用相同的循环但是将比较切换为<
而不是获得降序列表。
最后,您可以使用
打印值for(String value : ascending) {
System.out.println(value);
}
答案 2 :(得分:0)
/*
Hello Mister Dracose.
perhaps you should use something a bit more appropriated for this goal.
in fact you can not manage more than 2 strings at a time on your currently code, so you rather be using
*/
List<String> supplierNames1 = new ArrayList<String>();
/*
java structures, for save all user inputs, before you can go any further.
after that, than you could use your ordenating algotithm exatcly the same way you re already doing.
hope this help
*/
答案 3 :(得分:0)
使用链接列表。每次添加单词时,一次向下查看一个项目,并将新节点插入位置n,其中n-1.length =&gt; n.length> N + 1.length 要向后阅读它,您可以将其实现为双向链接列表,或者将单个链接列表读入堆栈并弹出堆栈