1

有一点背景知识,不久前我在这里看到一个问题,关于创建一个程序,询问房间里有多少人,然后你“采访”每个人的年龄,将他们分配到一个年龄组,然后打印他们年龄组和该年龄组的人数。我决定尝试一下,因为需要一个练习程序的想法,不幸的是,代码在获得第一个 for 语句之前就终止了,我不确定为什么。我认为这将是一个语法错误,但老实说我不知道​​,所以非常感谢任何帮助。

import java.util.Scanner;


public class UnkownProjects {
  public static void main(String[] args){
    Scanner stringInput = new Scanner(System.in);
    Scanner numInput = new Scanner(System.in);

    System.out.println("How many people are in the room?");
    int amountOfPeople = numInput.nextInt();
    int[] totalPeople = new int[amountOfPeople];
    System.out.println("Test");

    for(int index = 0; index == totalPeople.length; index++){
        System.out.println("Please enter an age for each person in the room:");
        int ageOfPerson = numInput.nextInt();
        ageOfPerson = totalPeople[index];
        System.out.println("Test");
    }
    for(int index = 0; index == totalPeople.length; index++){
        if(totalPeople[index] < 20 && totalPeople[index] > 0){
            int[] underTwenty = null;
            underTwenty[index] = totalPeople[index];
            System.out.println("Test");
         }
      }
   }
}

我也知道间距有点偏离,但我只是复制/粘贴并试图让它看起来对你们所有人来说都很漂亮,所以不用担心。哦,'println' 语句只是用来检查和查看程序在哪里终止的。

输出:

房间里有多少人?

(您将在此处输入的数字)

测试

忍者编辑:

决定我应该回到这篇文章,并将完成的代码放在这里,供遇到这个问题并想看看成品的任何人使用。

import java.util.InputMismatchException;
import java.util.Scanner;

public class InterviewClass {
    public static void main(String[] args){

        try{
            Scanner numInput = new Scanner(System.in);

            System.out.println("How many people are in the room? (Ex: 5, 10, 24)");
            int totalPeopleInRoom = numInput.nextInt();

            int[] agesOfPeopleInRoom = new int[totalPeopleInRoom];
            int youngPeople = 0, middleAged = 0, oldPeople = 0, deadPeople = 0;

            System.out.println("Please enter an age for " + totalPeopleInRoom + " people (Ex: 17, 21, 45):");

            for(int index = 0; index < agesOfPeopleInRoom.length; index++){
                int tempAgePlaceHolder = numInput.nextInt();
                agesOfPeopleInRoom[index] = tempAgePlaceHolder;

                if((index + 1) == (totalPeopleInRoom/2)){
                    System.out.println("Half way there!");
                }
            }
            System.out.println("Age Group\tAmount In Group");

            for(int index = 0; index < agesOfPeopleInRoom.length; index++){
                if(agesOfPeopleInRoom[index] < 30 && agesOfPeopleInRoom[index] > 0){
                    youngPeople = youngPeople + 1;
                }
                if(agesOfPeopleInRoom[index] < 60 && agesOfPeopleInRoom[index] > 30){
                    middleAged = middleAged + 1;
                }
                if(agesOfPeopleInRoom[index] < 115 && agesOfPeopleInRoom[index] > 60){
                    oldPeople = oldPeople + 1;
                }
                else if(agesOfPeopleInRoom[index] < 0 || agesOfPeopleInRoom[index] > 115){
                    deadPeople = deadPeople + 1;
                }
                }
                System.out.println("Young People:\t" + youngPeople);
                System.out.println("Middle Aged:\t" + middleAged);
                System.out.println("Old People:\t" + oldPeople);
                System.out.println("Dead People:\t" + deadPeople);
                System.out.print("Total People:\t");
                System.err.println(totalPeopleInRoom);

        }catch(InputMismatchException inputException){
            System.err.println("[ERROR] Wrong type of input used: " + inputException);
        }
    }
}
4

5 回答 5

2

这是一个不好的 for 循环:for(int index = 0; index == totalPeople.length; index++)

而是这样做:for(int index = 0; index < totalPeople.length; index++)

让我们分解 for 循环:

  • 循环的第一部分int index = 0是初始条件。它告诉循环在循环开始时应该将索引设置为什么。
  • for 循环中的第二项,在你的循环中index == totalPeople.length,是条件语句,它告诉 for 循环是在 true 时继续循环还是在 false 时停止循环。当循环尝试开始时,您的陈述将是错误的,因此循环将永远不会开始。所以这就是你的问题所在。相反,您想告诉它只要索引小于数组的长度就继续循环,或者在 Java 中,index < totalPeople.length.
  • 循环中的第三个项目 hereindex++告诉循环在每个循环完成时如何处理索引。在这里你告诉它增加一,这很好。
于 2013-10-22T21:24:17.463 回答
1

for(int index = 0; index == totalPeople.length; index++)应该

for(int index = 0; index < totalPeople.length; index++)

否则布尔条件被评估为假,因此循环不执行

你应该读这个

for 语句的一般形式可以表示为:

for (initialization; termination; increment) { statement(s) }

当使用这个版本的 for 语句时,请记住:

1. 初始化表达式初始化循环;它在循环开始时执行一次。
2. 当终止表达式的计算结果为假时,循环终止。
3. 循环每次迭代后调用增量表达式;这个表达式增加或减少一个值是完全可以接受的。

于 2013-10-22T21:24:30.543 回答
1

for循环条件必须是让它true迭代;它是什么时候爆发的false。在你的情况下,它是false马上的,所以它永远不会执行。

代替

for(int index = 0; index == totalPeople.length; index++){

尝试

for(int index = 0; index < totalPeople.length; index++){

对于另一个for循环也是如此。

在有关循环的Java 教程中for,它指出:

当终止表达式的计算结果为 false 时,循环终止。

于 2013-10-22T21:24:31.923 回答
1
 for(int index = 0; index == totalPeople.length; index++){

括号中的第二部分不是停止条件,而是继续检查。利用:

 for(int index = 0; index < totalPeople.length; index++){
于 2013-10-22T21:24:36.790 回答
0

您的 for 循环说在索引等于数组长度时继续执行此代码

您的意思是在索引小于数组长度时继续执行此代码

于 2013-10-22T21:28:59.937 回答