Java,将字符串存储到数组中

安德鲁

我希望将在命令提示符中以这种方式输入的原始问题(rawQuestions):java Hangman Hello test 123,放入数组(问题)中。我知道 rawQuestion 在“将有效问题存储到数组中”部分不起作用,因为

  • “错误: ';' 预期”和“错误:不是声明”

在行中

  • "questions[vCount] = String rawQuestion;"

因此,我应该如何重写它?

public class Hangman {

    //Validation of raw questions

    public static boolean isValidQuestion(String rawQuestion){
        rawQuestion = rawQuestion.toUpperCase();
        boolean vQuestion = true;
        for(int i=0; i<rawQuestion.length(); i++){
            char c = rawQuestion.charAt(i);

            if(!(c>='A' && c<='Z'))
            vQuestion = false;
        }
        return vQuestion;
    }


    public static void main(String args[]){

        boolean vQs[] = new boolean[args.length];

        for(int i=0; i<args.length; i++){

            String rawQuestion = args[i];

            boolean b = isValidQuestion(rawQuestion);

            if(b)
                 System.out.println(rawQuestion + " is a valid question!");

            else
                 System.out.println("Error: " + rawQuestion + ", input must be a word consist of a-z or A-Z");

            vQs[i] = b;
        }

        //count the number of valid questions

        int vCount = 0;
        for(int i=0; i<vQs.length; i++){
            System.out.println(i + " " + vQs[i] );
            if(vQs[i])
                vCount++;
        }
        System.out.println("There are " + vCount + " valid questions");

        //store valid questions into an array

        String questions[] = new String[vCount];
        for(vCount=0; vCount<args.length; vCount++){
            questions[vCount] = String rawQuestion;
            System.out.println( questions[vCount] );


        }

    }
}
图马

我修复了一些东西并发表了评论,不确定它是否有效,我还没有编译它。

public static void main(String args[]){

boolean vQs[] = new boolean[args.length];
int vCount=0;

for(int i=0; i<args.length; i++){

    String rawQuestion = args[i];

    if(isValidQuestion(rawQuestion)){


    System.out.println(rawQuestion + " is a valid question!");
            //Here you have enough data to count the valid questions, three loops are not needed.
            ++vCount;
            vQs[i]=true;
        }else{
            System.out.println("Error: " + rawQuestion + ", input must be a word consist of a-z or A-Z");
            vQs[i]=false;
        }
    }

    System.out.println("There are " + vCount + " valid questions");

    //store valid questions into an array
    String questions[] = new String[vCount];

    int j=0;
    for(int i=0; i<args.length; i++){
        //You need to iterate for all strings, because vQs[] is long args.length
        if(vQs[i]){
            //Ok the i-th question was valid, let's move it and print it
            questions[j] = args[i];
            System.out.println( questions[j] );
            ++j;
        }
    }

}

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C将字符串存储到数组中

将字符串数组存储到数据属性中

将单词从文件存储到字符串Java中

使用scanf将字符串存储到字符串数组中

如何使用getline将完整字符串存储到字符串数组中?

如何将字符串生成的子字符串存储到字符串数组中?

如何将字符串数组中的字符串存储到C中的变量中

将字符串值存储到循环内的数组

将字符串值存储到数组

将 google webapp 中的字符串存储到数组中

如何将字符串值存储到php中的数组中?

如何将标记化的字符串存储到C中的数组中

将字节数组存储到 Go lang 中的字符串变量中

如何保持将数据存储到字符串,直到在数组列表中显示符号?

将字符串变量的内容存储在 for 循环中,然后将其执行到数组中

将Excel字符串数据存储到数组列表中

PHP 将新行字符串存储到数组中

接受多行输入,以逗号分隔,将每行存储到字符串数组中

如何将词干字符串存储到单个数组中?

如何将字符串数据存储到单个数组中?

使用awk将拆分后的字符串存储到数组中

将argv []存储到两个数组中,并用特殊字符串(C)分隔

c编程将字符串存储到char数组中

如何将stringbuffer存储到字符串数组中?

将存储在字符串中的数组转换为数组

将数组存储在字符串中作为数组

如何将printStackTrace存储到字符串中

将字符串存储到JSON对象python中

(java)-将输入文件中的每个单词存储在字符串数组中