控制台需要输出用户信息

时间:2016-01-08 10:14:14

标签: java arrays eclipse console output

我真的很难执行我想要创建的程序。我希望用户能够输入他们想要的尽可能多的信息以及我的程序来存储它,直到用户键入单词“quit”。一旦用户输入这个单词,我希望程序结束并在控制台中列出用户输入的所有内容。我知道我需要使用数组,但我并不擅长使用它们。

我已达到用户可以输入“退出”的部分,程序将结束,但我不知道如何让它列出控制台中的所有内容。

这是我遇到错误的地方:

import java.util.Scanner;
public class Requirement1B {
public static void main(String[] args) {

Scanner scan = new Scanner (System.in);
String name, entry, exit = "";

System.out.println("Please Enter Your Name");
name = scan.nextLine();
System.out.println("Players Name: " + name);

while (true) {

    Scanner scan1 = new Scanner(System.in);
    System.out.println("Please Enter a Game Name, time spent playing the game(in Minutes) and your points"
            + " in this format...\t Game:Time:Points" ); 
    entry = scan.nextLine(); //this is the information I want show on the console once "quit"

    Scanner scan2 = new Scanner (System.in);
    System.out.println("If You Are Done type \"quit\" if not press return");
    exit = scan.nextLine();

    if (exit.equals("quit")) {
        break;
    } // This Works. but doesn't show information.

}

我希望信息在控制台中出现的一个例子是:

“用户名”,例如StackOverflow

“------------------------------------”

“游戏:时间:分数”,例如“COD:120:12345”

“游戏:时间:分数”,例如“FIFA:120:12345”

“游戏:时间:分数”,例如“GTA:120:12345”

“游戏:时间:分数”,例如“MINECRAFT:120:12345”

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您需要的数据结构是一个列表:

List<String> games = new ArrayList<>();
String game;
Scanner in = new Scanner(System.in);
while ((game = in.nextLine()) != null) {
  if (game.equals("quit")) {
    // process 'games' however you want, for example
    for (String g : games) {
      System.out.println(g);
    }
  } else {
    games.add(game);
  }
}

答案 1 :(得分:0)

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String line = "";
    String exit = "";
    boolean isInput = true;
    StringBuilder sb = new StringBuilder();
    System.out.println("Please Enter a Game Name, time spent playing the game(in Minutes) and your points"
            + " in this format...\t Game:Time:Points");
    System.out.println("If You Are Done type \"quit\" if not press return");

    while (isInput) {
        line = scan.nextLine();
        sb.append(line);
        if (line.equalsIgnoreCase("quit")) {
            isInput = false;
        }
    }
    System.out.println("Your String : " + sb.toString());
}
/*
 * Please Enter a Game Name, time spent playing the game(in Minutes) and
 * your points in this format...    Game:Time:Points If You Are Done type
 * "quit" if not press return Output : Game1: Points Game2: Points2 quit
 * Your String : Game1: PointsGame2: Points2 quit exit loop
 */