繁体   English   中英

程序在到达基本代码之前终止

[英]Program Terminates Before Reaching Essential Code

有点背景,我前不久在这里看到一个有关创建程序的问题,该程序询问房间中有多少人,然后您“采访”每个人的年龄,将他们分配给年龄组,然后打印他们的年龄。年龄段以及该年龄段的人数。 我决定从一个需要练习程序的想法开始尝试,不幸的是,代码在获取第一个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);
        }
    }
}

这对于循环是不好的: 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循环是保持循环还是保持循环,否则返回停止循环。 当循环尝试开始时,您的语句将为false,因此循环将永远不会开始。 这就是您的问题所在。 相反,您要告诉它只要索引小于数组的长度就可以继续循环,或者在Java中, index < totalPeople.length
  • 循环的第三项,这里是index++ ,告诉循环在每个循环完成时如何处理索引。 在这里,您告诉它增加一,这很好。

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

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

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

你应该读这个

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

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

使用此版本的for语句时,请记住:

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

for循环条件必须为true才能迭代。 它为false时会爆发。 就您而言,这是立即false ,因此它永远不会执行。

代替

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

尝试

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

和类似的其他for循环。

for循环Java教程中 ,它说明了这一点:

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM