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

huangapple go评论79阅读模式
英文:

How do I make my code NOT display a recent input?

问题

import java.util.Scanner;
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;
        int totalCarsSold = 0; // Added a variable to keep track of total cars sold

        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;
                }
                totalCarsSold += no_car_sold; // Accumulate the total cars sold
            }
        }

        System.out.println("Total number of months:" + (yr_no * 6));
        System.out.println("Total number of cars sold: " + totalCarsSold);
        average_sold = totalCarsSold / (yr_no * 6); // Calculate average correctly
        System.out.println("Average number of cars sold per month: " + average_sold);
    }

    public static boolean isValid(int x) {
        return true; // You might want to implement validation logic here
    }
}

In your original code, the issue was that the variable no_car_sold was getting overwritten in each iteration of the inner loop, which caused the final value to only hold the value of the last input. I've made modifications to correctly accumulate the total cars sold across all inputs.

英文:
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(&quot;Enter number of years&quot;);
yr_no=sc.nextInt();
if (!isValid(yr_no))
{
return;
}
for (int i=0; i&lt;yr_no;i++)
{
System.out.println(&quot;Enter the year&quot;);
yrs=sc.nextInt();
if (!isValid(yrs))
{
return;
}
for (int j=0;j&lt;6;j++)
{
System.out.println(&quot;Enter number of cars sold for year &quot; + yrs + &quot; in month #&quot; + (j+1));
no_car_sold=sc.nextInt();
if (!isValid(no_car_sold))
{
return;
}
no_car_sold=no_car_sold + z; 
}
}
System.out.println(&quot;Total number of months:&quot; + (yr_no*6) );
System.out.println(&quot;Total number of cars sold: &quot; + no_car_sold);
average_sold= no_car_sold/yr_no;
System.out.println(&quot;Average number of cars sold per month: &quot; + average_sold);
}
public static boolean isValid(int x)
{
return true;
}
}

Basically, my question is how do I fix my code to where each number I input gets added together? For example, let's say I put 2 years to be calculated, input some numbers for each month for the first year, enter the next year, enter more values; the last value I input becomes the total cars sold, which isn't the number I'm looking for. I would like to add all the amounts of cars sold instead of output the most recent input.

答案1

得分: 1

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

按照以下方式执行:

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;
    }
}

一个样例运行:

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

如有任何疑问/问题,请随时留言。

英文:

You need to have a variable to store the total sale. Also, the average number of cars sold per month must be calculated by dividing the total number divided by (no. of years * 6). On a side note, you should follow Java naming conventions e.g. the class, carwip2 should be named as Carwip2

Do it as follows:

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(&quot;Enter number of years: &quot;);
yr_no = sc.nextInt();
if (!isValid(yr_no)) {
return;
}
for (int i = 0; i &lt; yr_no; i++) {
System.out.print(&quot;Enter the year: &quot;);
yrs = sc.nextInt();
if (!isValid(yrs)) {
return;
}
for (int j = 0; j &lt; 6; j++) {
System.out.print(&quot;Enter number of cars sold for year &quot; + yrs + &quot; in month #&quot; + (j + 1) + &quot;: &quot;);
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(&quot;Total number of months:&quot; + (yr_no * 6));
System.out.println(&quot;Total number of cars sold: &quot; + total_no_car_sold);
average_sold = total_no_car_sold / (yr_no * 6.0);
System.out.println(&quot;Average number of cars sold per month: &quot; + average_sold);
}
public static boolean isValid(int x) {
return true;
}
}

A sample run:

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

Feel free to comment in case of any doubt/issue.

huangapple
  • 本文由 发表于 2020年4月4日 06:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61021515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定