【问题标题】:Printing specific array positions in Java (Console)在 Java 中打印特定的数组位置(控制台)
【发布时间】:2017-05-20 12:03:21
【问题描述】:

我有一个 Java 数组(基于控制台),我想在用户输入时打印出存储在其中一个位置的详细信息。例如,他们在控制台应用程序中输入“3”,然后程序以匹配请求的详细信息进行响应。

提前致谢!

if(input.matches("S")){
// If user input matches with S, then the user willSSN be asked to enter a array position and then the corresponding information will be shown



    KeyStroke[] patientsDetails = new KeyStroke[5];
    patientsDetails[0] = new KeyStroke(9,"OX5BJM","Peter",2039489);
    patientsDetails[1] = new KeyStroke(12,"OX1BOL","Kim",2434587);
    patientsDetails[2] = new KeyStroke(67,"OX2VBN","Patrick",2233842);
    patientsDetails[3] = new KeyStroke(34,"OX2XHB","Liam",2432340);
    patientsDetails[4] = new KeyStroke(54,"OX3BUN","Bob",2234098);

    System.out.println("Enter an array postion from 0 to 4 to show paitients corresponding postion and details");

    number1 = enterNumber0.nextInt();
     if (input.matches("0")){

        System.out.println(patientsDetails[0]);
    }

【问题讨论】:

  • 它是程序的一部分。不确定向我的工作提出特定问题有什么问题?
  • 这是一个非常基本的问题,表明没有努力学习或研究

标签: java arrays netbeans printing console


【解决方案1】:

如果我正确理解您的问题,则问题是在您将"Enter an array position from 0 to 4..." 打印到控制台之后出现的。

尝试替换

number1 = enterNumber0.nextInt();
     if (input.matches("0")){

        System.out.println(patientsDetails[0]);
    }

number1 = enterNumber0.nextInt(); // is enterNumber0 the name of your Scanner object?
System.out.println(patientsDetails[number1]); // I assume that number1 is declared as an `int` somewhere in your code?

如果我没有全面了解您的问题,请原谅并尝试更好地澄清问题。

【讨论】:

  • 嗨!这很有帮助,但是当我运行程序时,它会打印出一些我无法理解的东西。
  • "输入一个从 0 到 4 的数组位置以显示患者相应的位置和详细信息" 在此之后我输入 "0" 它给了我这个:client.KeyStroke@3e99f610
  • 这可能就是您看到的对象的哈希码。它看起来像code.KeyStroke@2f92e0f4 吗?如果是这样,您将不得不覆盖 KeyStroke 类的 .toString() 方法,或者创建自己的方法来显示各种患者的详细信息。
猜你喜欢
  • 2022-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多