控制台需要输出用户信息
Console needs to output user info
我真的很难执行我正在尝试创建的这个程序。我希望用户能够输入他们想要的尽可能多的信息,并且我的程序可以存储这些信息,直到用户键入单词 "quit"。一旦用户键入这个词,我希望程序结束并在控制台中列出用户输入的所有内容。我知道我需要使用数组,但我并不擅长使用它们。
我已经完成了用户可以键入 "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.
}
我希望信息如何在控制台中显示的示例是:
"Users Name" 例如Whosebug
"----------------------------------"-------------------------"
"Game:Time:points" 例如"COD:120:12345"
"Game:Time:points" 例如"FIFA:120:12345"
"Game:Time:points" 例如"GTA:120:12345"
"Game:Time:points" 例如"MINECRAFT:120:12345"
在此先感谢您的帮助。
你需要的数据结构是列表:
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);
}
}
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
*/
我真的很难执行我正在尝试创建的这个程序。我希望用户能够输入他们想要的尽可能多的信息,并且我的程序可以存储这些信息,直到用户键入单词 "quit"。一旦用户键入这个词,我希望程序结束并在控制台中列出用户输入的所有内容。我知道我需要使用数组,但我并不擅长使用它们。
我已经完成了用户可以键入 "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.
}
我希望信息如何在控制台中显示的示例是:
"Users Name" 例如Whosebug
"----------------------------------"-------------------------"
"Game:Time:points" 例如"COD:120:12345"
"Game:Time:points" 例如"FIFA:120:12345"
"Game:Time:points" 例如"GTA:120:12345"
"Game:Time:points" 例如"MINECRAFT:120:12345"
在此先感谢您的帮助。
你需要的数据结构是列表:
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);
}
}
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
*/