2016-11-22 94 views
0

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

阵列工作正常,但似乎无法得到“完成”部分,我目前使用的if语句.equals

有什么建议?

感谢

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"); 


     } 

    } 
+0

可能的复制[如何在输入完成后终止扫描器?](http://stackoverflow.com/questions/16206813/how-to-terminate-scanner-when-input-is-complete) – DimaSan

+2

你的'nameList'是一个'ArrayList '虽然不会等于'String''“完成”'。同样你的变量'name'似乎是无用的,因为它只是在'if'里面,它被设置为true。您可能希望在if语句之前将输入读入到字符串输入中,然后处理当前输入应该从那里进行的操作。 – SomeJavaGuy

回答

0

好,有几件事情在代码审查。

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

然后,名称列表是一个ArrayList,所以你不能把它比作一个字符串,你需要得到的ArrayList我值那么比较,如:

if (nameList.get(i).equals("DONE")) {  
    break; 
} 
0
  • 删除名称布尔变量,它似乎不是很有用

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

    0123。
     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()); 

     } 

    }  
+0

谢谢! @davidxxx。我现在在我的原始文章中看到了其他代码,它显示了最高和最低的名称和年龄(包含在数组中)尽管应用您的解决方案会产生以下错误 - 线程“main”中的异常java.lang.IndexOutOfBoundsException:Index:3 ,尺寸:3 \t在java.util.ArrayList.rangeCheck(ArrayList.java:653) \t在java.util.ArrayList.get(ArrayList.java:429) \t在NameAge.main(NameAge.java:35 ) 有什么建议么? – Fepapci

+0

你的异常是指一个ArrayList实例上的get()调用,并且在我的代码中,我没有在ArrayList实例上使用get()方法 – davidxxx

0

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

第二,使用此行:nameList.equals("DONE")您正在检查ArrayList nameList对象是否等于“DONE”。你没有检查ArrayList的元素。

如果你想要当有人进入单词“完成”停止你的程序,那么这是代码:

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()); 

} 

如果你想输入单词“完成”数组,然后检查是否数组包含“DONE “那么代码是这样的:

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());    

      } 
0

你会得到一个索引越界的原因是因为你有一个最大值设置为4,即youre设法环路即使阵列较小。既然你连接到一个时代的人,你可以使用HashMap来设置名称为重点,和年龄作为值,然后使用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)); 
相关问题