gpt4 book ai didi

java - 使控制台 Print() 按顺序显示用户的输入?

转载 作者:行者123 更新时间:2023-12-02 13:14:24 26 4
gpt4 key购买 nike

public class PlayerTest {

public static void main(String[] args) {

// Ask how many players will play
System.out.print("How many players(4 max) : ");
int value = input.nextInt();

while (true) {
/* Players need to be 1-4 max */
if (value <= 4 && value >= 1) {
System.out.println("You have " + value + " players");
break;
/* Cannot be less than 1 */
} else if (value < 1) {
System.out.println("(You cannot have less than 1 player)");
System.out.println("Please Try again : ");
value = input.nextInt();
/* Cannot be more than 4 */
} else if (value > 4) {
System.out.println("(You cannot have more than 4 players)");
System.out.println("Please try again :");
value = input.nextInt();
}
}

for (int i = 1; i <= value; i++) {
System.out.println("Player" + i + " Please Enter your name : ");
String PlayerName = input.next();
System.out.println("Please enter your number : ");
int PlayerNumber = input.nextInt();

System.out.println(PlayerName);
System.out.println(PlayerNumber);

}
}
}

如果用户想要让 2 名玩家加入游戏,系统会要求他们输入姓名号码。输入它们的值后,我希望控制台输出玩家1的名称号码,然后输出玩家2 名称号码

我想要的示例

John 30
Robert 50

最佳答案

我想回答这个问题。请记住,我也是一个非常新的初学者。然而,以下内容满足了我认为您的需要,因为您不是很具体。我正在使用 @downshift 推荐的非常基本的类。

import java.util.*;

public class RandomClass
{

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

String name;
int num;

PlayerInfo[] players = new PlayerInfo[4];

// Ask how many players will play
System.out.print("How many players(4 max) : ");
int value = input.nextInt();

while (true)
{
// Players need to be 1-4 max
if (value <= 4 && value >= 1) {
System.out.println("You have " + value + " players");
break;
// Cannot be less than 1
} else if (value < 1) {
System.out.println("(You cannot have less than 1 player)");
System.out.println("Please Try again : ");
value = input.nextInt();
// Cannot be more than 4
} else if (value > 4) {
System.out.println("(You cannot have more than 4 players)");
System.out.println("Please try again :");
value = input.nextInt();
}
}

for (int i = 0; i < value; i++)
{
System.out.println("Player" + (i + 1) + " Please Enter your name : ");
String PlayerName = input.next();
System.out.println("Please enter your number : ");
int PlayerNumber = input.nextInt();

players[i] = new PlayerInfo(PlayerName, PlayerNumber);

}

System.out.println();

for (int i = 0; i < value; i++)
{
System.out.println(players[i].getName());
System.out.println(players[i].getNum());
System.out.println();
}
}
}

class PlayerInfo
{
// variables
private String name;
private int num;

// constructors
public PlayerInfo() {}

public PlayerInfo (String xname, int xnum)
{
name = xname;
num = xnum;
}

// methods
public String getName()
{
return name;
}

public int getNum()
{
return num;
}
}

关于java - 使控制台 Print() 按顺序显示用户的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43828192/

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