如何测试用户的输入以查看其单词是否在指定范围内

时间:2016-12-20 09:49:31

标签: java

我试图编写一个程序,要求用户输入他们希望在论文中出现的单词的最大值和最小值,然后输入论文。程序检查用户输入的单词数是否在他们指定的范围内。有没有办法可以将我的代码转换为方法?这是我的代码:

期望的输出:

Please enter the maximum: 9

Please enter the minimum: 5

enter: hello there

2

YAY!!!!!!!!! YOU'RE WTHIN THE RAAAANGE!!!!!!!!!!!!!!!!!!!!!!!

这是我的代码:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("\t\tPlease enter the maximum: ");
    int max = input.nextInt();
    System.out.print("\t\tPlease enter the minimum: ");
    int min = input.nextInt();

    System.out.print("enter: ");
    String word = input.next();
    int countwords = 0;

    for (int i = 0; i < word.length(); i++) {
        if (word.charAt(i) == ' ') {
            countwords++;
        }
    }
    countwords++;
    System.out.println(countwords);
    if(countwords<=max && countwords >=min){
        System.out.println(
            "YAY!!!!!!!!! YOU'RE WTHIN THE RAAAANGE!!!!!!!!!!!!!!!!!!!!!!!"
        );
    }
}

6 个答案:

答案 0 :(得分:3)

您的计划中有两个问题:

  1. 您可以使用next()代替nextLine()来获取输入文字,这样您只能得到一个字而不是整行。
  2. 你计算单词的方式不正确,因为你只计算空格,你应该使用类似word.split("\\s+").length的东西,而不是使用一系列空白字符作为分隔符来滑动word的内容然后得到获得结果词总量的长度。
  3. 所以只需改变一下:

    ...
    System.out.print("enter: ");
    String word = input.nextLine();
    int countwords = word.split("\\s+").length;
    

    NB: hello there包含2个字词,2不在59之间,因此您无法获得有了这些输入的成功消息,请尝试使用15

答案 1 :(得分:2)

我们将用户输入保留在主页中。

    Scanner input = new Scanner(System.in);
    System.out.print("Please enter the maximum: ");
    int max = input.nextInt();
    System.out.print("Please enter the minimum: ");
    int min = input.nextInt();
    input.nextLine(); 
    System.out.print("enter: ");
    String word = input.nextLine();
    if(isInRange(min,max,word)){
        System.out.println(
                "YAY!!!!!!!!! YOU'RE WTHIN THE RAAAANGE!!!!!!!!!!!!!!!!!!!!!!!"
            );
    }

现在我们将计算转移到这样的方法:

static boolean isInRange(int min, int max, String words) {
    int length = words.split("\\W+").length;
    if (length <= max && length >= min) {
        return true;
    } else {
        return false;
    }
}

如果单词数量在范围内,您将获得所需的输出。 input.readLine();消耗了来自nextInt()

的剩余消息

答案 2 :(得分:1)

你应该使用String's split method和space-regex(比如&#34; \ s +&#34;或类似的)来处理单词分离。

在拆分字符串之前,您应该使用trim method来确保字符串之前或之后没有空格以防止错误的拆分。

就像XtremeBaumer已经发布的那样,把它包装成一个方法很不错。但我会制作一个类似的方法:

public static boolean inRange(int min, int max, String str) {
    return min <= split.trim().split("\\s+").length <= max;
}

答案 3 :(得分:1)

在这里,

import java.util.Scanner;

public class WordCount {
    public static void main(String[] args) {
        wordCountCheck();
    }

    public static void wordCountCheck() {

        Scanner input = new Scanner(System.in);
        System.out.print("\t\tPlease enter the maximum: ");
        int max = input.nextInt();
        System.out.print("\t\tPlease enter the minimum: ");
        int min = input.nextInt();

        input.nextLine();
        System.out.print("enter: ");
        String word = input.nextLine();
        String[] wordArray = word.trim().split("\\s+");
        int countwords = wordArray.length;

        System.out.println(countwords);
        if(countwords<=max && countwords >=min){
            System.out.println(
                "YAY!!!!!!!!! YOU'RE WTHIN THE RAAAANGE!!!!!!!!!!!!!!!!!!!!!!!"
            );
        } else {
            System.out.println(
                    "OOPS!!!!!!!!! YOU'RE NOT WTHIN THE RAAAANGE!!!!!!!!!!!!!!!!!!!!!!!"
                );
        }

    }
}

答案 4 :(得分:1)

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("\t\tPlease enter the maximum: ");
    int max = input.nextInt();
    System.out.print("\t\tPlease enter the minimum: ");
    int min = input.nextInt();

    System.out.print("enter: ");
    Scanner lineInput = new Scanner(System.in);
    String word = lineInput.nextLine();
    String[] lengthword = word.split("\\s+");
    int countwords = lengthword.length;
    if (countwords <= max && countwords >= min) {
        System.out
                .println("YAY!!!!!!!!! YOU'RE WTHIN THE RAAAANGE!!!!!!!!!!!!!!!!!!!!!!!");
    } else {
        System.out
                .println("Ohh!!!!!!!!! YOU'RE Not in RAAAANGE!!!!!!!!!!!!!!!!!!!!!!!");
    }
}

答案 5 :(得分:1)

首先通过调用&#34; .nextLine()&#34;来完成最后一行。

public class Test{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("\t\tPlease enter the maximum: ");
        int max = input.nextInt();
        System.out.print("\t\tPlease enter the minimum: ");
        int min = input.nextInt();

        System.out.print("enter: ");
        input.nextLine();//finish the last line first
        String word = input.nextLine();
        System.out.print(word);
        int countwords = 0;

        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) == ' ') {
                countwords++;
            }
        }
        countwords++;
        System.out.println(countwords);
        if(countwords<=max && countwords >=min){
            System.out.println(
                "YAY!!!!!!!!! YOU'RE WTHIN THE RAAAANGE!!!!!!!!!!!!!!!!!!!!!!!"
            );
        }
    }
}