在for循环中打印出平均值

时间:2017-06-09 06:55:08

标签: java

这是我的问题

  

输入一个表示周数的正整数,循环   持续到输入值为正。每周输入   升的值和千米的值。对于每个值,应该   循环,直到输入的值为正。这两个值都是真实的   数。然后输出该周的燃油经济性(升除以   公里)。最后,输出平均燃油经济性。你必须做   在你的答案中充分利用子模块。

这是我的工作

import java.util.*;
public class Exam8 {
    public static void main(String[] args) {
        int numweek = 0;
        double valkms = 0;
        double vallits = 0;
        double average = 0;
        double result = 0;
        int count = 0;
        double sum = 0;

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of week: ");
        numweek = sc.nextInt();
        while (numweek < 1) {
            System.out.println("Enter a positive of number week: ");
            numweek = sc.nextInt();
        }
        while (true) {
             count++;
             System.out.print("Enter value of litres: : ");
             vallits = sc.nextDouble();
             while (vallits < 0) {
                 System.out.print("Positive litres: ");
                 vallits= sc.nextDouble();
             }
             System.out.print("Enter value of kilometres: : ");
             valkms = sc.nextDouble();
             while (valkms < 0) {
                 System.out.print("Positive kilometres: ");
                 valkms = sc.nextDouble();
             }
             if (vallits == 0 || valkms == 0) {
                 break;
             }
             result = vallits / valkms; 
             sum = result + (double)count;
             System.out.println(result); 
        }
        //average = getAverg(sum,count);
        System.out.print("Average of fuel economy is: " + average);
    }

    public static double getAverg(double sum, int count) {
        double average;
        average = sum/count;
        return average;
    }
}

当输入值为lit和km时,我遇到问题,例如,我喜欢停止何时放置一个值为lit或km的值。然后我输出结果的平均值(点亮/公里)有另一个问题。

1 个答案:

答案 0 :(得分:0)

此代码应该有效:

import java.util.*;
public class App {
    public static void main(String[] args) {
        int numweek = 0;
        double valkms = 0;
        double vallits = 0;
        double average = 0;
        double result = 0;
        int count = 0;
        double sum = 0;

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of week: ");
        numweek = sc.nextInt();
        while (numweek < 1) {
            System.out.println("Enter a positive of number week: ");
            numweek = sc.nextInt();
        }
        while (count < numweek) {
             count++;
             System.out.print("Enter value of litres: : ");
             vallits = sc.nextDouble();
             while (vallits < 0) {
                 System.out.print("Positive litres: ");
                 vallits= sc.nextDouble();
             }
             System.out.print("Enter value of kilometres: : ");
             valkms = sc.nextDouble();
             while (valkms < 0) {
                 System.out.print("Positive kilometres: ");
                 valkms = sc.nextDouble();
             }
             if (vallits == 0 || valkms == 0) {
                 break;
             }
             result = vallits / valkms; 
             sum += result;
             System.out.println(result); 
        }       
        System.out.print("Average of fuel economy is: " + getAverg(sum, numweek));
    }

    public static double getAverg(double sum, double numOfWeeks) {
        double average;
        average = sum/numOfWeeks;
        return average;
    }
}

您的第一个错误是while循环中的条件不正确。为了使循环停止,您需要指定适当的条件。如果condition为true,它将无限循环或直到你设置break。

您的第二个错误是您没有正确计算平均值。你应该每周拿一笔钱并将其加到当前总和(在开始时为零),然后将这个总和除以周数来得到平均值。

你也没有在任何地方使用你的getAverg函数。