我希望将在命令提示符中以这种方式输入的原始问题(rawQuestions):java Hangman Hello test 123,放入数组(问题)中。我知道 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] 删除。
我来说两句