2014-10-11 79 views
1

是的,这是作业,但我在寻求帮助。我已经阅读了我们的书,并试图与一群人合作,并试着在这里和其他地方在网上搜索。新手写作方法 - 计算字符串中的单词

我有这种工作。它要求输入一个字符串两次(应该只询问一次),并且如果用户输入空白代码,它似乎会给出错误消息。然而,它重复“你的字符串有1个单词,你的字符串有2个单词,你的字符串有3个单词,你的字符串有4个单词,你的字符串有5个单词。”然后再重复一次 - 不管这个字符串有多少个单词。我无法弄清楚我做错了什么,并感谢任何帮助。

/* 
* Lab07a.java 
* 
* A simple program that computes the number of words in an input string. 
* Used to practice breaking code up into methods. 
* 
* @author ENTER YOUR NAMES HERE 
* 
*/ 
package osu.cse1223; 
import java.util.Scanner; 



public class Lab07a { 

public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); //get scanner// 
    getInputString(keyboard); 
    String input = getInputString(keyboard); 
    int count = getWordCount(input); 
    } 

// Given a Scanner, prompt the user for a String. If the user enters an empty 
// String, report an error message and ask for a non-empty String. Return the 
// String to the calling program. 

private static String getInputString(Scanner keyboardScanner) { 
    System.out.print("Enter a string: "); 
    String str = keyboardScanner.nextLine(); 
    if (str.length() ==0) 
    { 
     System.out.print("ERROR - string must not be empty"); 
    } 

    return str; 
} 

// Given a String return the number of words in the String. A word is a sequence of 
    // characters with no spaces. Write this method so that the function call: 
    // int count = getWordCount("The quick brown fox jumped"); 
    // results in count having a value of 5. You will call this method from the main method. 
    // For this assignment you may assume that 
    // words will be separated by exactly one space. 
    private static int getWordCount(String str) { 
    int spaces = 0; 
    int i = 0; 
    while (i <str.length()) 
     { 
     char ch = str.charAt(i); 
     if (ch == ' ') 
     i++; 
     { 

     spaces++; 
    System.out.print("Your string has " + spaces + "words in it."); 
    } 
     } 
    return spaces; 
} 
} 
+0

你难道没有str.partition(蟒蛇)的等效使它会返回包含分隔符(空间在内)的分割字符串数组,然后您只需将str.split的长度与str进行比较。分区和区别是空格的数量。 – 2014-10-11 17:40:22

+0

尝试所有= text.split(“”,-1),它似乎是str.partition等价物,并与words = text.split()进行比较。所以最后,空格= len(全部) - len(单词),这是主意。 – 2014-10-11 17:44:57

+0

可能依赖于问题规范,但如果我分配了这个,我会寻找学生解决问题,并且会特别禁止使用现有的函数来解决问题。 “不要使用Java的分割功能 - 我希望你自己做。” – 2014-10-11 17:50:43

回答

0

它要求你的字符串两次的原因是,你告诉它,就在这里:

getInputString(keyboard); //HERE YOU ASK FOR AN INPUT STRING 
String input = getInputString(keyboard); //HERE YOU ASK FOR IT AGAIN 

自讨苦吃一次而是存储结果,并继续下去。

您的主要问题的最佳答案是考虑算法,您计算这些空间的计划。用英文写出来,并确保在将它放入Java之前相信它。这里是你现在正在做的:

while we haven't reached end of string 
    if the current character is a space 
     go further in the string 
    else 
     add 1 to the number of spaces found so far 
     print the number of spaces found so far 

现在,这显然不是你的意思。首先,如果你到达一个空间,为什么只在字符串中进一步?另一方面,如果您没有找到空间,为什么要将1添加到目前为止发现的空间数量?为什么要打印迄今为止发现的空间数 - 每当你经过循环时 - 但只有当你没有找到空间时才打印空间?

难怪它打印找到1个空间,2位,3位,提前等

规划用英文写的算法是避免这样的问题是至关重要的。

所以:采取这种算法,并改变它,让它说你想说的话,仍然用英语 - 然后转换为Java。

还有一个提示:你的缩进看起来随机到最后。这让你很难看出你是否犯了一个错误。严格注意缩进,并且您将能够更好地检测是否存在问题。

-1

移动此行

是System.out.print( “你的字符串中有” +空格+ “字在里面。”);

超出循环,应该解决您的问题。没有1

我真的怀疑它是否做了两次印刷。

但是我必须说有更好的方法来做到这一点。 str.split(“”); 就是其中之一

编辑 你调用getWordsCount两次,可悲的是错误的东西与我的手机soclient格式化现在

相关问题