import java.util.ArrayList;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> game = new ArrayList<String>();
ArrayList<Integer> score = new ArrayList<Integer>();
ArrayList<Integer> time = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
String answer = "yes";
{
do {
System.out.print("Please enter your username: ");
name.add(scanner.nextLine());
System.out.println("Please enter the game: ");
game.add(scanner.nextLine());
System.out.println("Please Enter Achievement Score: ");
score.add(scanner.nextInt());
System.out.println("Please Enter Playtime: ");
time.add(scanner.nextInt());
System.out.print("Any letter to continue alternatively type quit to Quit.");
answer = scanner.next();
} while (!answer.equals("quit"));
if (answer.equals("quit")); //want it to go back to start another direcotry here
else {
System.out.println("Thanks for adding to the database.");
for (int i = 0; i < name.size(); i++) {
System.out.print("Username:" +name.get(i)+"\n");
System.out.print("~~~~~~~~~~~~" +"\n");
System.out.print("Game:" +game.get(i)+"\t");
System.out.print("Score:" +score.get(i)+"\n");
System.out.print("Minutes played:"+time.get(i));
System.out.println("");
}
}
}
}
}
这个问题是程序在输入Quit时没有退出,但它正在继续并在一行上放两个问题,如下所示。我该如何解决这个问题?
答案 0 :(得分:0)
问题在于您要比较退出的大写和小写值。您需要将它们设置为相同的大小写类型才能使它们相等并退出程序。
将支票更改为
while (!answer.toLowerCase().equals("quit"));
if (answer.toLowerCase().equals("quit"));
这会将您的输入更改为小写,这是您要比较的。
答案 1 :(得分:0)
您的退出信息System.out.print("Any letter to continue alternatively type quit to Quit.");
表示您需要将quit
全部小写字词结尾。
同时将answer = scanner.next();
更改为answer = scanner.nextLine();
编辑: 由于您没有任何错误控制,您可以考虑以下代码:
do {
System.out.print("Please enter your username: ");
name.add(scanner.nextLine());
System.out.println("Please enter the game: ");
game.add(scanner.nextLine());
System.out.println("Please Enter Achievement Score: ");
score.add(Integer.valueOf(scanner.nextLine()));
System.out.println("Please Enter Playtime: ");
time.add(Integer.valueOf(scanner.nextLine()));
System.out.print("Any letter to continue alternatively type quit to Quit.");
answer = scanner.nextLine();
} while (!answer.equals("quit"));
我将scanner.nextInt()
更改为Integer.valueOf(scanner.nextLine())
。使用nextLine()
可帮助扫描程序在代码进入下一行之前清除所有数据。
答案 2 :(得分:0)
除非您没有关闭扫描仪,否则我没有看到任何问题。您可以运行此操作,您可以看到扫描仪已关闭并在退出单词后进入正常执行状态。
import java.util.ArrayList;
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> game = new ArrayList<String>();
ArrayList<Integer> score = new ArrayList<Integer>();
ArrayList<Integer> time = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
String answer = "yes";
{
do {
System.out.print("Please enter your username: ");
name.add(scanner.nextLine());
System.out.println("Please enter the game: ");
game.add(scanner.nextLine());
System.out.println("Please Enter Achievement Score: ");
score.add(scanner.nextInt());
System.out.println("Please Enter Playtime: ");
time.add(scanner.nextInt());
System.out.print("Any letter to continue alternatively type quit to Quit.");
answer = scanner.next();
} while (!answer.equals("quit"));
if(answer.equals("quit")){
System.out.println("you have just quit");
scanner.close();
} //want it to go back to start another direcotry here
else {
System.out.println("Thanks for adding to the database.");
for (int i = 0; i < name.size(); i++) {
System.out.print("Username:" +name.get(i)+"\n");
System.out.print("~~~~~~~~~~~~" +"\n");
System.out.print("Game:" +game.get(i)+"\t");
System.out.print("Score:" +score.get(i)+"\n");
System.out.print("Minutes played:"+time.get(i));
System.out.println("");
}
}
System.out.print("You are done with scanner");
}
}
}
答案 3 :(得分:0)
我刚试过你的程序,如果你输入&#34; quit&#34;它就会退出。 (全部小写),如命令行说明建议:
System.out.print(&#34;任何要继续的字母或者输入Quit退出。&#34;);
如果您希望quit指令不区分大小写,则应替换此行:
} while (!answer.equals("quit"));
用这个:
} while (!answer.equalsIgnoreCase("quit"));
我希望有所帮助!