首先是java新手。
以下程序是基于一个商店,创建我自己的程序,我在自己的商店。
程序运行正常但我在从数组中检索值时遇到问题
import java.util.Scanner;
public class bale
{
static int pieces;
static int BaleCost = 0;
static int singlepiece;
public bale()
{
Scanner in = new Scanner(System.in);
pieces = in.nextInt();
System.out.print("Enter the Single Piece price: ");
singlepiece = in.nextInt();
BaleCost = singlepiece*pieces;
}
}
import java.util.Scanner;
public class ShopProfit
{
static bale[] bales;//creating bales array
static int TotalBaleAmount;
static int n;
static int totalpieces = 0;
public static void main(String[] args)
{
bales();
System.out.println("There are "+totalpieces+" pieces in "+n+" bales");
System.out.println("you have to pay "+TotalBaleAmount+" rupees to the bale company");
for(int i = 0; i<=n-1; i++)
{
System.out.println("bale "+(i+1)+" has "+bales[i].pieces+" with "+bales[i].singlepiece+" rupess per picece");
}
}
static void bales()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of bales you bought: ");
n = in.nextInt();
bales = new bale[n];
for(int i = 0; i<=n-1; i++)
{
System.out.print("Enter the number of pieces in bale"+(i+1)+": ");
bales[i] = new bale();
totalpieces = totalpieces + bale.pieces;
TotalBaleAmount = TotalBaleAmount + bale.BaleCost;
}
}
}
输入您购买的包数:3
输入bale1:300
中的件数输入单件价格:92
输入bale2中的件数:240
输入单件价格:85
输入bale3中的件数:350
输入单件价格:96
有3包890件
你必须向捆包公司支付81600卢比
bale 1有350个,每个picece有96卢比
bale 2有350个,每个picece有96卢比
bale 3有350个,每个picece有96卢比
答案 0 :(得分:0)
问题是Bale中的BaleCost是静态的,因此您始终将成本设置为相同的变量。 但真正的问题是设计根本不是面向对象的。
尝试创建工厂来创建Bale,并使用Bale作为POJO来存储数据。
HIH, Bargas