简体   繁体   English

用户输入后打印阵列

[英]Printing an array after user input

I'm working on a class that will take user input to assign values to an object created in a source class. 我正在研究一个类,它将使用用户输入将值分配给在源类中创建的对象。 Those objects will then be added to an array, which then prints the values on it. 然后将这些对象添加到数组,然后在数组上打印值。 However, the "list" under print : list is telling me that I need to initialize the variable. 但是,print:list下的“ list”告诉我我需要初始化变量。 Why is it not recognizing that this is an array even though it seems to work fine in my do loop? 为什么即使在do循环中它似乎也可以正常工作,但为什么仍不能识别出这是一个数组?

import java.util.Scanner;
import name.Names;

 public class NameTester {

 public static void main(String[] args) {
    // TODO Auto-generated method stub
    String entry;
    Scanner firstscan = new Scanner(System.in);
    Scanner lastscan = new Scanner(System.in);
    Scanner codescan = new Scanner(System.in);
    Scanner entryscan = new Scanner(System.in);
    String first;
    String last;
    int code;

    System.out
            .println("This program will prompt you to input first name, +"
                    + "last name, and zip code for an individual. Hit \"x\" when finished\n");

    do {
        System.out.println("Enter first name:");
        first = firstscan.nextLine();
        System.out.println("Enter last name:");
        last = lastscan.nextLine();
        System.out.println("Enter zip code:");
        code = codescan.nextInt();

        Names nm = new Names(first, last, code);

        Names[] list = new Names[25];

        int count = 0;
        list[count] = nm;
        count++;

        System.out
                .println("To quit hit \"X\" or any other key followed by enter to continue:");
        entry = entryscan.nextLine();

    } while (!(entry.equalsIgnoreCase("x")));

    for (Names print : list)
        System.out.println(print + "");
}
}

For one, you are instantiating your array inside your loop, that means every time your loop runs through, it creates a new array instead of updating the old one. 首先,您要在循环中实例化数组,这意味着每次循环运行时,它都会创建一个新数组,而不是更新旧数组。 Then, once you leave your loop, you leave its "scope". 然后,一旦离开循环,便离开其“作用域”。 That means everything you declare inside the loop is not available outside. 这意味着您在循环内声明的所有内容在外部都不可用。 The solution is to declare your array outside the loop. 解决方案是在循环外声明数组。

Every block in java has its own scope (defined through brackets). Java中的每个块都有自己的作用域(通过方括号定义)。 While you can access variables that have been declared outside your block while inside it, it does not work the other way around; 虽然您可以在块内访问已在块外声明的变量,但反之则行不通。 as you can see. 如你看到的。 Just google java scope, and you will understand more. 只是google java范围,您将了解更多。 Hope that helps ;) 希望能有所帮助;)

You will need a method in the class Name that return the first, last name and the zip code because if you just use: 您将在Name类中需要一个返回名字,姓氏和邮政编码的方法,因为如果您只使用:

System.out.println(print + "") System.out.println(打印+“”)

You are printing the object Name and no the String that represents the attributes saved in the object. 您正在打印对象名称,但没有打印表示对象中保存的属性的字符串。

For example you can have the method in the class Name: 例如,您可以在类Name中使用该方法:

String getFirst()
{
       return this.first;
}

And the last line in your class Nametester can be 而类Nametester中的最后一行可以是

System.out.println(print.getFirst() + ""); System.out.println(print.getFirst()+“”);

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

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