如何使代码不显示最近的输入? import java.io.*; 公共类carwip2 { 公共静态void main(字符串参数[]) { getAverageCarSales(); } 公共静态无效getAverageCarSales() { 扫描仪sc=新的扫描仪(System.in); int无车销售=0,z=0; int yr_no=0; int-yrs=0; 浮动平均值_=0F; System.out.println(“输入年数”); yr_no=sc.nextInt(); 如果(!isValid(年号)) { 返回; } 对于(int i=0;i
您需要一个变量来存储总销售额。此外,每月平均销售的汽车数量必须通过将总数量除以(年份数*6)来计算。在一个旁注中,您应该遵循例如类,如何使代码不显示最近的输入? import java.io.*; 公共类carwip2 { 公共静态void main(字符串参数[]) { getAverageCarSales(); } 公共静态无效getAverageCarSales() { 扫描仪sc=新的扫描仪(System.in); int无车销售=0,z=0; int yr_no=0; int-yrs=0; 浮动平均值_=0F; System.out.println(“输入年数”); yr_no=sc.nextInt(); 如果(!isValid(年号)) { 返回; } 对于(int i=0;i,java,output,add,Java,Output,Add,您需要一个变量来存储总销售额。此外,每月平均销售的汽车数量必须通过将总数量除以(年份数*6)来计算。在一个旁注中,您应该遵循例如类,carwip2应命名为carwip2 按如下方式操作: import java.io.*; public class carwip2 { public static void main(String args[]) { getAverageCarSales(); } public static void getAver
carwip2
应命名为carwip2
按如下方式操作:
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;
}
}
如有任何疑问/问题,请随时发表评论
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