gpt4 book ai didi

java - 数组列表扫描器循环退出功能

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:32 25 4
gpt4 key购买 nike

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 后并未退出,而是继续运行并将两个问题放在一行上,如下所示。我该如何解决这个问题?

  • 请输入您的用户名:abc123
  • 请进入游戏:
  • 侠盗猎车手 V
  • 请输入成就分数:
  • 1200
  • 请输入游戏时间:
  • 120
  • 任意字母以继续,或者键入 quit 以 Quit.Quit
  • 请输入您的用户名: 请进入游戏:

最佳答案

问题是您正在比较 quit 的大小写值。您需要将它们设置为相同的大小写类型以使它们相等并退出程序。

将支票更改为

 while (!answer.toLowerCase().equals("quit"));
if (answer.toLowerCase().equals("quit"));

这会将您的输入更改为小写,这就是您要比较的内容。

关于java - 数组列表扫描器循环退出功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35964531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com