在数组中输入和存储名称-Java

在数组中输入和存储名称-Java,java,Java,我一直在尝试这个问题,但我仍然找不到解决办法。感谢您的帮助!谢谢 问题-声明并创建一个数组来存储四个玩家的名字。调用数组pNames 要求用户输入四名玩家的姓名,并将他们的姓名存储在pNames数组中 Enter Name of Player 1 > Johnny Enter Name of Player 2 > Jackie Enter Name of Player 3 > Jessie Enter Name of Player 4 > Jeremy

我一直在尝试这个问题,但我仍然找不到解决办法。感谢您的帮助!谢谢

问题-声明并创建一个数组来存储四个玩家的名字。调用数组pNames

要求用户输入四名玩家的姓名,并将他们的姓名存储在pNames数组中

  Enter Name of Player 1 > Johnny
  Enter Name of Player 2 > Jackie
  Enter Name of Player 3 > Jessie
  Enter Name of Player 4 > Jeremy
输出hello消息以问候每个玩家

Hello Johnny
Hello Jackie
Hello Jessie
Hello Jeremy
答复:

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

    Scanner sc = new Scanner(System.in);

    String [] pNames = new String[4];

    for(int i = 1; i <= pNames.length; i++) {

        System.out.print("Enter name of Player" + " " + i + " > ");

        pNames[i] = sc.nextLine();
    }

    System.out.println("Hello" + " " + pNames[0]);
    System.out.println("Hello" + " " + pNames[1]);
    System.out.println("Hello" + " " + pNames[2]);
    System.out.println("Hello" + " " + pNames[3]);
}
import java.util.Scanner;
公共课Q2{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串[]pNames=新字符串[4];

对于(inti=1;i数组是基于0索引的

在循环中,您使用的是1索引基

改变

for(int i = 1; i <= pNames.length; i++) {

    System.out.print("Enter name of Player" + " " + i + " > ");

    pNames[i] = sc.nextLine();
}

for(int i=1;i不是从1开始索引,而是从数组索引开始到0,在显示“输入玩家名称”时,只需显示i的增量值乘以1即可

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

    Scanner sc = new Scanner(System.in);

    String [] pNames = new String[4];

    for(int i = 0; i < pNames.length; i++) {

        System.out.print("Enter name of Player" + " " + (i+1) + " > ");

        pNames[i] = sc.nextLine();
    }

    System.out.println("Hello" + " " + pNames[0]);
    System.out.println("Hello" + " " + pNames[1]);
    System.out.println("Hello" + " " + pNames[2]);
    System.out.println("Hello" + " " + pNames[3]);
}
import java.util.Scanner;
公共课Q2{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串[]pNames=新字符串[4];
对于(int i=0;i”;
pNames[i]=sc.nextLine();
}
System.out.println(“Hello”+“”+pNames[0]);
System.out.println(“Hello”+“”+pNames[1]);
System.out.println(“Hello”+“”+pNames[2]);
System.out.println(“Hello”+“”+pNames[3]);
}
试试这个:

从0启动for,并使用
sc.Next();
而不是
sc.nextLine();

import java.util.Scanner;
公共课Q2{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串[]pNames=新字符串[4];
对于(int i=0;i”;
pNames[i]=sc.next();
}
System.out.println(“Hello”+“”+pNames[0]);
System.out.println(“Hello”+“”+pNames[1]);
System.out.println(“Hello”+“”+pNames[2]);
System.out.println(“Hello”+“”+pNames[3]);
}

}

只需更改此行:

pNames[i] = sc.nextLine();
为此:

pNames[i-1] = sc.nextLine();

由于您访问的是1到4,因此您的数组实际上在0到3的范围内,因此您需要减去1。

最好从0开始并更改(i@nabeel No…条件是正确的,因为for从1开始,而不是从0开始。我假设他希望它是玩家1,而不是玩家0@nabeelboth工作得很好,但最好学习和理解数组从零索引开始:-)@nabeel如果循环从0开始,则需要更改System.out添加1以打印从1到4而不是从0到3的正确索引。此代码不正确,因为它要求“输入播放器的名称0>”这不是需要添加的内容,您应该添加一些注释,向寻求调试帮助的操作人员解释代码的不同之处(“为什么此代码不工作?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅:如何创建问题。使用“编辑”链接改进您的问题-不要通过评论添加更多信息。谢谢!
  import java.util.Scanner;
  public class Q2 {
  public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String [] pNames = new String[4];

for(int i = 0; i < pNames.length; i++) {

    int a = i+1;
    System.out.print("Enter name of Player" + " " + a + " > ");

    pNames[i] = sc.next();
}

System.out.println("Hello" + " " + pNames[0]);
System.out.println("Hello" + " " + pNames[1]);
System.out.println("Hello" + " " + pNames[2]);
System.out.println("Hello" + " " + pNames[3]);
}
pNames[i] = sc.nextLine();
pNames[i-1] = sc.nextLine();