【问题标题】:Accessing counter outside while loop java在while循环java之外访问计数器
【发布时间】:2017-09-28 18:34:01
【问题描述】:

我得到的错误是除以 0。

这是因为totDrivers 的值。我不确定是不是因为while 循环循环时计数器没有更新。或者是因为totDrivers 我从循环外部不正确地访问了变量。

我读过的其他帖子说要在循环外访问变量,您需要在循环外对其进行初始化。但它没有说明这样做是否会出现计数器更新值。

[Exception in thread "main" java.lang.ArithmeticException: / by zero
at Statistics.main(Statistics.java:84)]

import java.util.Scanner;

public class Statistics
{

    public static void main (String [] args)
    {

        Scanner in=new Scanner(System.in);

        //counters initilization
        int f=0; //female
        int m=0;//male
        int u=0;//unspecified
        int u25=0;//less than 25 in age
        int mu=0;//male and under 25
        int bt=0;//between 25 and 75
        int ab=0;//above 75 
        int y=0;//res
        int n=0;//non res

        int age=0;
        String gender="U";
        String nlRes="Y";
        int totDrivers=0;

        while (age !=0)
        {

            totDrivers++;
            System.out.print("Please enter age\\(Enter 0 if theres no more input\\): ");
            age= in.nextInt();
            System.out.print("Please enter gender(M,F,or U): ");
            gender= in.next();
            System.out.print("Please enter gender: ");
            nlRes= in.next();       


            if (age<25)
            {
                u25++;
            }
            if(gender.equals("M"))
            {
                mu++;
            } 
            else if(age>=25&&age<=75)
            {
                bt++;
            }
            else if(age>75)
            {
                ab++;
            }


            if(gender.equals("M"))
            {
                m++;
            }
            else if(gender.equals("F"))
            {
                f++;
            }
            else if(gender.equals("U"))
            {
                u++;
            }


            if(nlRes.equals("Y"))
            {
                y++;
            }
            else if(nlRes.equals("N"))
            {
                n++;
            }
            System.out.println("totd"+totDrivers);
        }
        //output

        double perU25=(u25/totDrivers)*100;
        double perMU25=(mu/m)*100;
        double perF=(f/totDrivers)*100;
        double perNonNL=(n/totDrivers)*100;
        double perAbove=(ab/totDrivers)*100;

        System.out.println("The % of drivers under 25 is : %"+perU25);
        System.out.println("The % of male drivers under 25 is : %"+perMU25);
        System.out.println("The % of female drives is : %"+perF);
        System.out.println("The % of out of province drivers is : %"+perNonNL);
        System.out.println("The % of drivers over the age of 75 is : %"+perAbove);  
    }
}

【问题讨论】:

  • 哪条线失败了?我不认为第 84 行除以 totDrivers
  • 一个简单的if(totDrivers &gt; 0 &amp;&amp; m &gt; 0) { // do your math here } 将解决您的问题。你有没有想过代码可能永远不会进入你的循环?

标签: java while-loop counter


【解决方案1】:

在进行除法之前检查您的计数器是否 = 0

if(m != 0)
{
double perMU25=(mu/m)*100;
}

【讨论】:

  • 如果这样做了,那么就会出现一个错误,说它找不到变量,这是因为它没有进入循环。这意味着 totDrivers 没有更新,我不知道为什么
  • 这是因为您将年龄设置为 0,并且您的循环在年龄!= 0 时执行
猜你喜欢
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 2016-02-27
  • 2016-04-02
  • 1970-01-01
  • 2023-01-08
相关资源
最近更新 更多