我的代码检查一定数量的用户输入的字符串是否有任何重复的字符。例如,如果我输入字符串" google" "纸"和" water",代码返回" paper"和" water&#34 ;;因为" google"有两个Os。
我将代码部分放下,但是在打印时,在输出的最后一个字符串之后会出现一个空格,我无法弄清楚如何摆脱它。
import java.util.Scanner;
import java.util.*;
class words{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number or words: ");
String[] words = new String[sc.nextInt()];
System.out.print("Enter the strings: ");
boolean truth = false;
for (int i = 0; i < words.length; i++) {
words[i] = sc.next();
}
for(int i=0;i<words.length;i++){
int j;
for(j=1;j<words[i].length();j++) {
if(words[i].charAt(j) == words[i].charAt(j-1)){
break;
}
}
if(j==words[i].length()){
truth = true;
System.out.print(words[i]+" ");
}
}
if(!truth){
System.out.println("NONE");
}
}
}
答案 0 :(得分:1)
移动逻辑以检查重复字符到函数中;我会利用NVIDIA OpenCL Best Practices Guide和更短的数组语法。像,
private static boolean repeatedChars(String s) {
if (s == null) {
return false;
}
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length - 1; i++) {
if (chars[i] == chars[i + 1]) {
return true;
}
}
return false;
}
然后,您可以使用lambda根据没有重复字符的words
过滤Scanner sc = new Scanner(System.in);
System.out.print("Enter the number or words: ");
String[] words = new String[sc.nextInt()];
System.out.print("Enter the strings: ");
for (int i = 0; i < words.length; i++) {
words[i] = sc.next();
}
System.out.println(Arrays.stream(words).filter(s -> !repeatedChars(s))
.collect(Collectors.joining(" ")));
并使用String.toCharArray()
收集
NONE
,如果您需要显示Predicate<String>
消息,可以重新使用Predicate<String> pred = s -> !repeatedChars(s);
if (Arrays.stream(words).anyMatch(pred)) {
System.out.println(Arrays.stream(words).filter(pred).collect(Collectors.joining(" ")));
} else {
System.out.println("NONE");
}
junkdf = df = DataFrame({'User_ID': [340,558,558,558,983,422,100,740,740],
'Transaction_Type_x': ['Purchase', 'Purchase', 'Purchase', 'Purchase', 'Purchase', 'Purchase', 'Purchase', 'Purchase', 'Purchase'],
'Rev/Payout_x': [50, 20, 20,28,37,50,40,50,55],
'Cohort_x': ['2010-01','2010-01','2010-02','2010-2','2010-02','2010-02','2010-03','2010-04','2010-04'],
'Transaction_Type_y': ['Sale','Sale','Sale','Sale','Sale','Sale','Sale','Sale','Sale'],
'Rev/Payout_y': [33,42,66,69,100,22,19,98,39],
'Cohort_y' : ['2010-03','2010-01','2010-01','2010-04','2010-04','2010-02','2010-01','2010-03','2010-02']})
junkdf2 =junkdf[['User_ID','Transaction_Type_x','Rev/Payout_x','Cohort_x',
'Transaction_Type_y','Rev/Payout_y','Cohort_y']]
junkdf2
答案 1 :(得分:1)
您的问题有一个简单的解决方法。如果没有任何连续重复,不要立即打印每个单词,而是将其添加到末尾有空格的String变量中,以便每个单词用空格分隔。运行循环后,检查标志是否为false,如果为false则打印NONE。但是,如果为true,则打印结果字符串,在最后添加.trim()的所有内容。
for (int i = 0; i < words.length; i++) {
words[i] = sc.next();
}
String result = ""; /*This is the string that holds all the strings that you need to print.*/
for(int i=0;i<words.length;i++){
int j;
for(j=1;j<words[i].length();j++) {
if(words[i].charAt(j) == words[i].charAt(j-1)){
break;
}
}
if(j==words[i].length()){
truth = true;
result = result + (words[i]+" ");
}
}
if(!truth){
System.out.println("NONE");
}
else{
System.out.println(result.trim()); /*The trim function removes any redundant space in the beginning and the end of the string.*/
}
当然这样做会浪费很多堆内存,但我想这是一个小型的学习项目。但是,请查看StringBuilder如何使用它来避免在堆中创建大量内存!