首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使我的代码不显示最近的输入?

如何使我的代码不显示最近的输入?
EN

Stack Overflow用户
提问于 2020-04-03 22:45:21
回答 1查看 32关注 0票数 1
代码语言:javascript
复制
import java.io.*;
public class carwip2
{
    public static void main(String args[])
    {
        getAverageCarSales();
    }
    public static void getAverageCarSales()
    {
        Scanner sc= new Scanner(System.in);
        int no_car_sold=0 , z=0;
        int yr_no=0;
        int yrs=0;


        float average_sold=0F;

        System.out.println("Enter number of years");
        yr_no=sc.nextInt();
        if (!isValid(yr_no))
        {

            return;
        }

        for (int i=0; i<yr_no;i++)
        {
            System.out.println("Enter the year");
            yrs=sc.nextInt();
            if (!isValid(yrs))
            {

                return;
            }
            for (int j=0;j<6;j++)
            {
                System.out.println("Enter number of cars sold for year " + yrs + " in month #" + (j+1));
                no_car_sold=sc.nextInt();
                if (!isValid(no_car_sold))
                {

                    return;
                }
                no_car_sold=no_car_sold + z; 
            }
        }
            System.out.println("Total number of months:" + (yr_no*6) );
            System.out.println("Total number of cars sold: " + no_car_sold);
            average_sold= no_car_sold/yr_no;
            System.out.println("Average number of cars sold per month: " + average_sold);
    }
    public static boolean isValid(int x)
    {
        return true;
    }
}

基本上,我的问题是如何将我的代码修正到我输入的每个数字被加到一起的地方?例如,假设我花了2年时间来计算,第一年的每个月输入一些数字,进入下一年,输入更多的值;我输入的最后一个值变成了汽车销售总量,这不是我想要的数字。我想把所有的汽车销量加起来,而不是最近的投入。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-03 22:56:55

您需要有一个变量来存储总销售额。此外,每月销售汽车的平均数量必须除以总数除以(否)。年* 6)。另外,您应该遵循Java命名约定,例如,类carwip2应该被命名为Carwip2

这样做如下:

代码语言:javascript
复制
import java.util.Scanner;

public class Carwip2 {
    public static void main(String args[]) {
        getAverageCarSales();
    }

    public static void getAverageCarSales() {
        Scanner sc = new Scanner(System.in);
        int total_no_car_sold = 0, no_car_sold = 0, z = 0;
        int yr_no = 0;
        int yrs = 0;

        double average_sold = 0.0;

        System.out.print("Enter number of years: ");
        yr_no = sc.nextInt();
        if (!isValid(yr_no)) {

            return;
        }

        for (int i = 0; i < yr_no; i++) {
            System.out.print("Enter the year: ");
            yrs = sc.nextInt();
            if (!isValid(yrs)) {

                return;
            }
            for (int j = 0; j < 6; j++) {
                System.out.print("Enter number of cars sold for year " + yrs + " in month #" + (j + 1) + ": ");
                no_car_sold = sc.nextInt();
                if (!isValid(no_car_sold)) {

                    return;
                }
                total_no_car_sold = total_no_car_sold + no_car_sold;
            }
        }
        System.out.println("Total number of months:" + (yr_no * 6));
        System.out.println("Total number of cars sold: " + total_no_car_sold);
        average_sold = total_no_car_sold / (yr_no * 6.0);
        System.out.println("Average number of cars sold per month: " + average_sold);
    }

    public static boolean isValid(int x) {
        return true;
    }
}

示例运行:

代码语言:javascript
复制
Enter number of years: 2
Enter the year: 2005
Enter number of cars sold for year 2005 in month #1: 2
Enter number of cars sold for year 2005 in month #2: 3
Enter number of cars sold for year 2005 in month #3: 4
Enter number of cars sold for year 2005 in month #4: 1
Enter number of cars sold for year 2005 in month #5: 2
Enter number of cars sold for year 2005 in month #6: 3
Enter the year: 2006
Enter number of cars sold for year 2006 in month #1: 4
Enter number of cars sold for year 2006 in month #2: 3
Enter number of cars sold for year 2006 in month #3: 1
Enter number of cars sold for year 2006 in month #4: 3
Enter number of cars sold for year 2006 in month #5: 4
Enter number of cars sold for year 2006 in month #6: 2
Total number of months:12
Total number of cars sold: 32
Average number of cars sold per month: 2.6666666666666665

如有任何疑问/问题,请随时发表意见。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61021515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档