简体   繁体   English

如何在 java 中使用并行 arrays 打印扫描仪元素?

[英]how to print Scanner element using parallel arrays in java?

I am new to learning about parallel arrays and want to know how to effectively print content/elements using parallel array only, I tried but couldn't get it to work and function the way I want.我是学习并行 arrays 的新手,想知道如何仅使用并行数组有效地打印内容/元素,我试过但无法让它工作,function 我想要的方式。

The task is : The program inputs an integer from the user representing how many peoples' information will be entered.任务是:程序从用户那里输入一个 integer,代表将输入多少人的信息。 Then, the program inputs the information one person at a time (name first, then age), storing this information in two related arrays.然后,程序一次输入一个人的信息(首先是姓名,然后是年龄),将此信息存储在两个相关的 arrays 中。

Next, the program inputs an integer representing the person on the list whose information should be displayed (to get information for the first person, the user would enter '1').接下来,程序输入一个 integer 代表列表中应显示其信息的人(要获取第一个人的信息,用户将输入“1”)。 The program makes a statement about the person's name and age.该程序会声明此人的姓名和年龄。

Although I got the name and age to work until the integer the user inputs, but after that I am not sure how to do虽然我得到了工作的姓名和年龄,直到用户输入 integer,但之后我不知道该怎么做

Sample input:样本输入:

4
Vince
5
Alexina
8
Ahmed
4
Jorge
2
2 // I am confused about this part, how would I make it so that it prints the desired name,
     //for example with this input, it should print:

Sample output:样品 output:

Alexina is 8 years old

My code:我的代码:

import java.util.Scanner;

class Example {
  public static void main (String[] args) {

    Scanner keyboard = new Scanner(System.in);

    int[] numbers = new int[keyboard.nextInt()];
    for (int x = 0; x < num.length; x++){
    String[] name = {keyboard.next()};
    int[] age = {keyboard.nextInt()}; 
    }
    int num2 = keyboard.nextInt();
        System.out.println(); // what would I say here?
  }
}

You need to rewrite your code so your arrays aren't being assigned within the loop.您需要重写代码,以便不会在循环中分配 arrays。 You want to add values to the arrays, not reset them each time, and you want to be able to access them afterwards.您希望向 arrays 添加值,而不是每次都重置它们,并且您希望之后能够访问它们。 Below is a modified version of your code:以下是您的代码的修改版本:

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

    int num = keyboard.nextInt();
    keyboard.nextLine(); //you also need to consume the newline character(s)
    String[] name = new String[num]; //declare the arrays outside the loop
    int[] age = new int[num]; 
    for (int x = 0; x < num; x++){
        name[x] = keyboard.nextLine(); //add a value instead of resetting the array
        age[x] = keyboard.nextInt();
        keyboard.nextLine(); //again, consume the newline character(s) every time you call nextInt()
    }
    int num2 = keyboard.nextInt() - 1; //subtract one (array indices start at 0)
    System.out.println(name[num2] + " is " + age[num2] + " years old"); //construct your string with your now-visible arrays
}

As I think you have to think about the local and global variable usage in java.In brief, Local variables can only use within the method or block, Local variable is available only to method or block in which it is declared.正如我认为您必须考虑 java 中的局部和全局变量的使用。简而言之,局部变量只能在方法或块内使用,局部变量仅适用于声明它的方法或块。 For example:例如:

{ 
  int y[]=new Int[4];
}

this y array can be accessed within the block only.这个 y 数组只能在块内访问。 Global Variable has to be declared anywhere in the class body but not inside any method or block.全局变量必须在 class 主体中的任何位置声明,但不能在任何方法或块内声明。 If a variable is declared as global, it can be used anywhere in the class.如果一个变量被声明为全局变量,它可以在 class 的任何地方使用。 In your code you try to create arrays and use them out of the For loop.在您的代码中,您尝试创建 arrays 并在 For 循环之外使用它们。 But your arrays are valid only inside the For loop.但是您的 arrays 仅在 For 循环内有效。 After every loop runs all info is lost.there will be new array creation for every iteration of For loop.每次循环运行后,所有信息都会丢失。For 循环的每次迭代都会创建新的数组。

therefore, In order to access and save the information you have to declare your arrays before the For loop and access and store data using iteration number as the index.因此,为了访问和保存信息,您必须在 For 循环之前声明您的 arrays 并使用迭代号作为索引访问和存储数据。 finally, to print the data you gathered, you have to scan new input as integer variable.then you can access your arrays as you wanted.最后,要打印您收集的数据,您必须将新输入扫描为 integer 变量。然后您可以根据需要访问您的 arrays。


    //For example 
    int [] age=new int[4]; //global -Can access inside or outside the For loop
    int[] numbers = new int[keyboard.nextInt()];
    for (int x = 0; x < 4; x++){

       age[x] = keyboard.nextInt(); //Local 
    }
    int num2 = keyboard.nextInt();
    System.out.println(age[num2]); // Here you have to access global arrays using num2 num2
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM