繁体   English   中英

试图在 object 中创建的方法中返回一个值,并且该方法包含 for 循环和 if 语句但不能这样做

[英]Trying to return a value in a method created in an object and the method contains for loops and if statements but can't do it

因此,我被要求编写一个程序,从用户那里读取一个句子,报告并删除错误的重复(如果有的话)。 错误的重复是指一个词(或更多)被重复(出现两次或更多次)并且这些重复是连续的。

`

public class checker {
    
    private String sentence;
    
    checker(String sentence){this.sentence=sentence;}
    
    public String check(){
        
        String[] word = sentence.split(" ");
        
        for(int i=0; i<word.length; i++){
            for(int j=i+1; j<word.length; j++){
                if(word[i].equals(word[j])){
                    word[j]="error";}}}
        
        for(int i=0; i<word.length; i++) {
            if (!"error".equals(word[i])) {
                System.out.print(word[i] + " ");}}
        
        return "";}
}

***这是我的代码的 output:***

Enter a Sentence: The operator did not skip his meal 操作员没有跳过他的用餐 构建成功(总时间:12 秒)

所以我的代码完成了查找重复单词并打印更正版本的工作,但问题是我无法找到一种方法让它打印出来,就像在示例运行中应该做的那样!

[样本运行:

- 输入一个句子:接线员没有不吃饭 - 这个句子包含错误的重复。 -句子应该是:The operator did not skip his meal

- 输入一句话:快乐的人活得更久 - 没有错误的重复]

**我确实知道我的问题,每次我尝试编写一段包含任何时间类型的循环和 if 语句的代码时,我只是不知道从循环和条件语句中提取我想要的内容,我试图

`

for(int i=0; i<word.length; i++){
    for(int j=i+1; j<word.length; j++){
        if(word[i].equals(word[j])){
            System.out.println("The sentence includes wrong repetitions.\nThe sentence should be: ");
            word[j]="error";}
        else{
             System.out.println("There are no wrong repetitions");
            }
    }   
}

` 但我知道它会在每次执行循环时打印它!

我真的很感激能随身携带的有用提示!

提前谢谢大家!**

你没有任何机制来存储你的有效单词,所以你可以在你的句子评估完成后以你需要的格式打印它们。 我建议将有效单词存储在 StringBuilder 或 ArrayList 中,就像您 go 一样,这样您就可以在准备好时打印结果字符串。

下面是使用 StringBuilder 完成此操作的示例程序。 您还可以使用 ArrayList 并调用String.join(" ", wordList); 完成后创建句子。

final String sentence = "This is an invalid invalid sentence that that needs corrected";
final String[] words = sentence.split(" ");
StringBuilder sentenceBuilder = new StringBuilder();
for (int i = 0; i < words.length; i++)
{
    final String checkWord = words[i];
    final boolean lastWord = i == words.length - 1;
    final boolean duplicate = !lastWord && checkWord.equals(words[i + 1]);
    if (lastWord || !duplicate)
        sentenceBuilder.append(checkWord);
    if (!lastWord && !duplicate)
        sentenceBuilder.append(' ');
}
if (!sentence.equals(sentenceBuilder.toString()))
    System.out.printf("The sentence includes wrong repetitions.%nThe sentence should be: %s%n", sentenceBuilder);
else
    System.out.println("There are no wrong repetitions");

上周我在作业中遇到了同样的问题。 我无法解决它,但我必须提交一些东西,这样我就不会得到零分。 我的程序中的错误是在重复单词的位置留下了一个空格,它只检测到两次出现。

这是我写的代码:

 String [] n= s.split(" ");
   boolean check=false;
 
   for (int i=1;i<n.length;i++){
       String    temp=n[i-1];
       if (n[i].equals(temp) == true) {
       check = true;
           System.out.println("The sentence includes wrong repetitions.");
     n[i]="";
     System.out.print("The sentence should be: ");
   for(int j=0;j<n.length;j++) {
       System.out.print(n[j]+" ");
       
   }
       }
      
   }
   
   if (check == false)
            System.out.println("There are no wrong repetitions");   
    }
    
}

如果您找到了解决方案或者是否可以改进我的代码,请告诉我

讲师给我们发了解决方案,在这里添加它以备将来参考:

/* 

CSCI300 - 作业 1 问题 2 的解决方案 */ package 次重复;

导入 java.util.Scanner;

公共 class 重复 {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    boolean hasSuccessiveReptition = false; //Assume no repetitions
    
    System.out.print("Enter a sentence: ");
    // Read the whole sentence into one String:
    String sentence = scan.nextLine();
    
    //create an array of Strings to handle each word separately
    String[] words = sentence.split(" ");
    //Create a string to add non repetetive words to
    String repetitionFreeSentence = words[0];  // add the fisrt word

    for(int i = 1; i < words.length; i++)
        if(words[i].equalsIgnoreCase(words[i-1])) // compare successive words
            hasSuccessiveReptition = true;
        else // add the word to the corrected setence
            repetitionFreeSentence += " " + words[i];        
    if(hasSuccessiveReptition){
        System.out.println("The sentence includes wrong repetitions.");
        System.out.println("The sentence should be: " + repetitionFreeSentence);
    }
    else
        System.out.println("There are no wrong repetitions");
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM