如何根据用户输入声明变量?
How do I declare a variable based on user input?
我正在尝试创建一个程序来拼写检查用户输入的内容。他们必须正确输入单词 'type' 10 次,它会显示一条消息,说明花了多长时间。
我的问题是结局永远不会到来,它会不断要求您输入 'type'。我相信这是因为“while (attempts < required)
”。我想我需要进行 required = correct 尝试。 if attempt = type
,那么它会产生1个正确的值。任何帮助表示赞赏。也很抱歉,我不知道如何组织问题
public static void printHeading(String heading) {
System.out.println(heading.toUpperCase());
for (int i = 0; i < heading.length(); i++)
System.out.print("=");
System.out.println();
System.out.println();
}
public static int runTutorial(Scanner in, String word, int required) {
Scanner sc = new Scanner(System.in);
String attempt = word;
int correct = 0;
int attempts = 0;
while (attempts<required)
{
System.out.print("Enter '" + word + "': ");
attempt = sc.nextLine();
if(attempt.equals("type")) {
System.out.println("Correct");
}
else {
System.out.println("Try again");
}
}
return attempts;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int TIMES = 10; //required number of correct repetitions
final String WORD = "type"; //the word to type
long startTime, endTime; //start and end time of typing test
double seconds; //elapsed time in seconds
int attempts;
printHeading("Typing Tutor");
System.out.println("You need to type a word " + TIMES +
" times correctly, as quickly as you can");
System.out.println("Your word today will be '" + WORD + "' (do not enter the quotes)");
System.out.println();
System.out.println("Type anything and press enter to begin");
//The test
System.out.println("Press enter to start the test");
sc.nextLine();
startTime = System.currentTimeMillis();
attempts = runTutorial(sc, WORD, TIMES);
endTime = System.currentTimeMillis();
//Test report
seconds = (double)(endTime - startTime) / 1000;
System.out.println("You took " + seconds + " seconds and " + attempts +
" attempts to correctly type '" + WORD + "' " + TIMES + " times");
System.out.println();
printHeading("Come back for more pracice soon");
}
}
在System.out.println("Correct");
下,您需要增加attempts
变量,否则它将保持为0。
if(attempt.equals("type")) {
System.out.println("Correct");
attempts++; // <-- here
}
我正在尝试创建一个程序来拼写检查用户输入的内容。他们必须正确输入单词 'type' 10 次,它会显示一条消息,说明花了多长时间。
我的问题是结局永远不会到来,它会不断要求您输入 'type'。我相信这是因为“while (attempts < required)
”。我想我需要进行 required = correct 尝试。 if attempt = type
,那么它会产生1个正确的值。任何帮助表示赞赏。也很抱歉,我不知道如何组织问题
public static void printHeading(String heading) {
System.out.println(heading.toUpperCase());
for (int i = 0; i < heading.length(); i++)
System.out.print("=");
System.out.println();
System.out.println();
}
public static int runTutorial(Scanner in, String word, int required) {
Scanner sc = new Scanner(System.in);
String attempt = word;
int correct = 0;
int attempts = 0;
while (attempts<required)
{
System.out.print("Enter '" + word + "': ");
attempt = sc.nextLine();
if(attempt.equals("type")) {
System.out.println("Correct");
}
else {
System.out.println("Try again");
}
}
return attempts;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int TIMES = 10; //required number of correct repetitions
final String WORD = "type"; //the word to type
long startTime, endTime; //start and end time of typing test
double seconds; //elapsed time in seconds
int attempts;
printHeading("Typing Tutor");
System.out.println("You need to type a word " + TIMES +
" times correctly, as quickly as you can");
System.out.println("Your word today will be '" + WORD + "' (do not enter the quotes)");
System.out.println();
System.out.println("Type anything and press enter to begin");
//The test
System.out.println("Press enter to start the test");
sc.nextLine();
startTime = System.currentTimeMillis();
attempts = runTutorial(sc, WORD, TIMES);
endTime = System.currentTimeMillis();
//Test report
seconds = (double)(endTime - startTime) / 1000;
System.out.println("You took " + seconds + " seconds and " + attempts +
" attempts to correctly type '" + WORD + "' " + TIMES + " times");
System.out.println();
printHeading("Come back for more pracice soon");
}
}
在System.out.println("Correct");
下,您需要增加attempts
变量,否则它将保持为0。
if(attempt.equals("type")) {
System.out.println("Correct");
attempts++; // <-- here
}