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

问题描述 投票:0回答:1
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年,请输入第一年每个月的一些数字,输入第二年,输入更多值;我输入的最后一个值成为售出的汽车总数,这不是我要查找的数量。我想添加所有售出的汽车数量,而不是输出最新的输入。

java output add
1个回答
0
投票

您需要有一个变量来存储总销售额。另外,每个月售出的平均汽车数量必须用总数量除以(年数* 12)得出。

执行以下操作:

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;

        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;
                }
                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 * 12);
        System.out.println("Average number of cars sold per month: " + average_sold);
    }

    public static boolean isValid(int x) {
        return true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.