我的输入是 -
我是谁 你是什么
输出是 -
我是你是谁
但我希望输出为 -
我是谁 是你的
我的代码是 -
ResourceRegistrar
答案 0 :(得分:0)
继续将输入放在字符串数组中,迭代并执行此操作。我不会写完整个代码,这是一种指导你的伪代码,但是它有排序功能。
1)用space
2)使用Arrays.sort
String[] dataArray= stingValue.split(" ");
Arrays.sort(dataArray);
3)您可以通过迭代再次concat
答案 1 :(得分:0)
private static void doSome4Bro() {
Scanner scan = new Scanner(System.in);
System.out.println("enter the value of n");
int n = Integer.parseInt(scan.nextLine());
ArrayList<String> lines = new ArrayList<String>(n);
ArrayList<String> newLines = new ArrayList<String>(n);
for (int i = 0; i < n; i++) {
lines.add(i, scan.nextLine());
}
scan.close();
for(String line:lines){
String[] words = line.split(" ");
Arrays.sort(words);
StringBuilder sb = new StringBuilder(line.length());
for(String word: words){
if(word.isEmpty())
continue;
if(sb.length() > 0){
sb.append(" ");
}
sb.append(word);
}
String newLine = sb.toString();
newLines.add(newLine);
}
System.out.println("Get some:");
for(String line: newLines){
System.out.println(line);
}
}
答案 2 :(得分:0)
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter the value of n");
int n = Integer.parseInt(scan.nextLine());
ArrayList<String> lines = new ArrayList<String>();
//0. remove and Move below
boolean once_entered = true;
//1..remove & move below
//for (int i = 0; i < n; i++) {
//}
for (int i = 0; i < n; i++) {
lines.add(i, scan.nextLine() + " ");
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> words_2 = new ArrayList<String>();
String word = "";
for (int j = 0; j < lines.get(i).length(); j++) {
char char_0 = lines.get(i).toLowerCase().charAt(j);
if (char_0 >= 'a' && char_0 <= 'z') {
word += char_0;
once_entered = false;
}
else if (!once_entered)
{
words.add(word);
word = "";
once_entered = true;
}
}
//} //2.comment
for (int t = 0; t < words.size(); t++) {
boolean contains =false;
for(int j=0;j<words_2.size();j++){
if(words_2.get(j).contentEquals(words.get(t)))
contains=true;
break;
}
if(!contains)
words_2.add(words.get(t));
}
Collections.sort(words_2);
// System.out.println(words_2.size());
for (int t = 0; t < words_2.size(); t++) {
System.out.print( " " + words_2.get(t));
}
System.out.println("");
} //3.movded to here
scan.close();
}
答案 3 :(得分:0)
我觉得你的问题过于复杂,或者你不知道可用的工具。有些集合可以在您输入对象时对其进行排序。
我能看到的逻辑是
我真的没有关注整个三个单独的列表并对字符和内容进行小写,所以我重写了这个版本。
快速解释“有分类对象的集合”声明:
一个TreeSet
,基于once_entered
变量,您可能正在尝试删除重复项?如果是这样,Set
是一个完美的数据结构。
class Driver {
private static String strJoin(Iterable<String> lst, String delimeter) {
StringBuilder sb = new StringBuilder();
for (String s : lst) {
sb.append(s).append(delimeter);
}
sb.setLength(sb.length() - delimeter.length());
return sb.toString();
}
private static void rearrangeLine(String line) {
// split the words & sort the words
Set<String> words = new TreeSet<String>();
words.addAll(Arrays.asList(line.split("\\s+")));
// Print
String output = strJoin(words, " ");
System.out.println(output);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// read the number of lines
System.out.println("enter the value of n");
int n = Integer.parseInt(scan.nextLine());
// for each line
for (int i = 0; i < n; i++) {
System.out.print(">> ");
String line = scan.nextLine();
rearrangeLine(line);
}
scan.close();
}
}
<强> I / O 强>
enter the value of n
2
>> who i am
am i who
>> what are you
are what you