如何打破循环并打印数组的内容 [英] How to break out of a loop and print the contents of an array
问题描述
当用户输入完成"时,如何跳出循环?并打印用户输入的内容?另外,我希望能够在打印过程中找到数组的最小元素和数组的最大元素.
How can I break out of a loop when the user enters "done" and print the contents of what the user entered? Also, I would like to be able to find the smallest element of the array and the largest element of the array during the printing.
import java.util.Arrays;
public class array2 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
input.useDelimiter(System.getProperty("line.separator"));
int numofpeople = 10;
Person[] persons = new Person[numofpeople];
for (int i = 0; i < numofpeople; i++) {
System.out.print("Enter the person's name: ");
String person = input.next();
if(person.equals("done")){break;}
System.out.print("Enter the persons's age: ");
int age = (Integer) input.nextInt();
persons[i] = new Person(person, age);
}
}
}
class Person implements Comparable<Person> {
public String person;
public Integer age;
public Person(String s, Integer g) {
this.person = s;
this.age = g;
}
@Override
public int compareTo(Person o) {
return (this.age>o.age?1:-1);
}
}
推荐答案
break
语句将使程序离开 for
循环.您没有代码可在 for
循环之外打印数组.实际上,循环之外没有任何代码.这将使您的程序退出,因为它位于 main
方法中.因此,您需要代码才能打印数组.
The break
statement will make the program leave the for
loop. You have no code to print the array outside of the for
loop. In fact, there is no code outside of the loop. This will make your program exit since it is in the main
method. So you need code to print the array.
完整的解决方案给出以下所有注释:
Complete Solution Given all comments below:
import java.util.ArrayList;
public class Array2 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
ArrayList<Person> persons = new ArrayList();
int numOfPeople = 10;
for (int i = 0; i < numOfPeople; i++) {
System.out.print("Enter the person's name: ");
String person = input.nextLine();
if (person.trim().equalsIgnoreCase("done")) {
break;
}
System.out.print("Enter the persons's age: ");
int age = Integer.parseInt(input.nextLine());
persons.add(new Person(person, age));
}
//Collections.sort(persons); //not sorting anymore
for (Person person : persons) {
System.out.println(person.name + ": " + person.age);
}
Person youngestPerson = getYoungest(persons);
Person oldestPerson = getOldest(persons);
System.out.println("Youngest: " + youngestPerson.name + ": " + youngestPerson.age);
System.out.println("Oldest: " +oldestPerson.name + ": " + oldestPerson.age);
}
public static Person getYoungest(ArrayList<Person> people) {
Person youngestPerson = null;
for (Person person : people) {
if (youngestPerson == null || person.age < youngestPerson.age) {
youngestPerson = person;
}
}
return youngestPerson;
}
public static Person getOldest(ArrayList<Person> people) {
Person oldestPerson = null;
for (Person person : people) {
if (oldestPerson == null || person.age > oldestPerson.age) {
oldestPerson = person;
}
}
return oldestPerson;
}
protected static class Person implements Comparable<Person> {
protected String name;
protected int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person o) {
return (this.age > o.age ? 1 : -1);
}
}
}
这篇关于如何打破循环并打印数组的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!