Java在用户输入完成后终止程序'

时间:2016-11-22 13:08:02

标签: java arrays if-statement

我正在处理一个存储名称字符串和年龄整数的数组,我希望数组最多为4并打印出这些结果,除非用户输入单词' done&#39 ;如果是这样,程序终止并且数组输出它存储的内容。

阵列工作正常,但似乎无法完成“#”;我正在使用.equals

的if语句

有什么建议吗?

由于

import java.util.Scanner;
import java.util.ArrayList;

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

        final int MAX_VALUE = 4;
        //boolean name = false;

        ArrayList<String> nameList = new ArrayList<String>();
        ArrayList<Integer> ageList = new ArrayList<Integer>();

        Integer[] ages = new Integer[4];

        for (int i = 0; i < MAX_VALUE; i++) {

            System.out.print("Enter a name: ");
            String currentLine = input.next();

            if (currentLine.equals("done")) {
            break;
            }

            nameList.add(currentLine);

            System.out.print("Now enter an age for " + currentLine + ": ");
            ageList.add(input.nextInt());

          }

        System.out.print("\n");
        for(int i = 0; i < MAX_VALUE; i++) {

            System.out.println("Name: " + nameList.get(i) + "   Age: " + ageList.get(i));


        }


    // display youngest and oldest member of array
    int smallest = ageList.get(0);
        int largest = ageList.get(0);

        String oldest = nameList.get(0);
        String youngest = nameList.get(0);

        for (int i = 0; i < ages.length; i++) {
            if(ageList.get(i) > largest) {
                largest = ageList.get(i);
                oldest = nameList.get(i);

            } else if(ageList.get(i) < smallest) {
                smallest = ageList.get(i);
                youngest = nameList.get(i);
            }
        }
                        System.out.println("\nThe youngest person is " + youngest + " who is " + smallest + " years old");
            System.out.println("The oldest person is " + oldest + " who is " + largest + " years old");


        }

    }

4 个答案:

答案 0 :(得分:0)

好的,在该代码中有几件事要审查。

变量name在那里没有用处,因为每次迭代它只被设置为true。 (你可能会在=声明中与==混淆if吗?

然后,nameList是一个ArrayList,所以你无法将它与String进行比较,你需要得到ArrayList的值i然后进行比较,例如:

if (nameList.get(i).equals("DONE")) {    
    break; 
}

答案 1 :(得分:0)

  • 删除名称boolean变量,似乎没用。

  • 为什么要使用两台扫描仪。一个就够了。

         Scanner input2 = new Scanner(System.in);
    

    不需要。

  • 将第一个input.nextLine()存储在临时变量中,而不是存储在最终列表中,以便决定:继续或停止:

    String currentLine = input.nextLine()

  • 不需要年龄数组。您已经有一个列表来存储年龄值:

    ArrayList<Integer> ageList

  • Scanner.nextLine()读取整行。它可能有副作用。在您的情况下,您只需要行当前输入:仅使用next()或nextInt()。

它应该做的工作:

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

    final int MAX_VALUE = 4;

    ArrayList<String> nameList = new ArrayList<String>();
    ArrayList<Integer> ageList = new ArrayList<Integer>();

    for (int i = 0; i < MAX_VALUE; i++) {

        System.out.print("Enter a name: ");
        String currentLine = input.next();

        if (currentLine.equals("DONE")) {
        break;
        }

        nameList.add(currentLine);

        System.out.print("Now enter an age for " + currentLine + ": ");
        ageList.add(input.nextInt());

      }

    }    

答案 2 :(得分:0)

首先,您为什么需要:if(name = true) {}您不需要,如果您想测试名称是否为真,则使用双==

其次,使用此行:nameList.equals("DONE"),您正在检查ArrayList nameList对象是否等于&#34; DONE&#34;。您没有检查ArrayList的元素。

如果你想在有人输入单词时停止你的程序&#34; DONE&#34;那么这就是代码:

Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);

final int MAX_VALUE = 4;
boolean name = false;
String chk = "";

ArrayList<String> nameList = new ArrayList<>();
ArrayList<Integer> ageList = new ArrayList<>();

Integer[] ages = new Integer[4];

for (int i = 0; i < MAX_VALUE; i++) {

    System.out.print("Enter a name: ");

    chk = input.nextLine();

    if (chk.equals("DONE"))
        break;

    nameList.add(chk);

    System.out.print("Now enter an age for " + nameList.get(i) + ": ");
    ageList.add(input2.nextInt());

}

如果你想输入单词&#34; DONE&#34; to array,然后检查数组是否包含&#34; DONE&#34;代码是这样的:

Scanner input = new Scanner(System.in);
          Scanner input2 = new Scanner(System.in);

          final int MAX_VALUE = 4;
          boolean name = false;
          String chk = "";

          ArrayList<String> nameList = new ArrayList<>();
          ArrayList<Integer> ageList = new ArrayList<>();

          Integer[] ages = new Integer[4];

          for(int i = 0; i < MAX_VALUE; i++ ) {

                  System.out.print("Enter a name: ");
                  nameList.add(input.nextLine());
                  if (nameList.contains("DONE"))    //use contains
                      break; 



              System.out.print("Now enter an age for " + nameList.get(i) + ": ");
              ageList.add(input2.nextInt());              

          }  

答案 3 :(得分:0)

您获得索引超出范围的原因是因为您将最大值设置为4,即使数组较小,您也尝试循环。由于您有一个人连接到一个年龄,您可以使用HashMap将name设置为key,并将age设置为value,然后使用lambda表达式在完成添加新条目时打印hashmap:

    Scanner input = new Scanner(System.in);
    final int MAX_VALUE = 4;
    HashMap<String, Integer> people = new HashMap<String, Integer>();

    for (int i = 0; i < MAX_VALUE; i++) {
        System.out.print("Enter a name: ");
        String name = input.next();

        if (name.equals("done")) {break;}

        System.out.print("Now enter an age for " + name + ": ");
        int age = input.nextInt();

        people.put(name, age);
    }
    input.close();
    System.out.print("\n");
    people.forEach((k,v)->System.out.println("Name : " + k + "  Age : " + v));