首先是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
共有890件3包
你必须向捆包公司支付81600卢比
打包1有350个,每个picece有96卢比
Bale 2有350个,每个picece有96卢比
Bale 3有350个,每个picece有96卢比
你可以看到输出中的最后三行,我需要我作为输入提供的确切答案,但程序只执行最终输入值。
请帮我根据给定的输入检索数组值。
First of all am new to java.
below program is based on a shop, creating my own program which i face in my own shop.
program is running perfectly but i have problem in retrieving values from an array
this is object
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; } }this is main program
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; } } }input and output
Enter the number of bales you bought: 3
Enter the number of pieces in bale1: 300
Enter the Single Piece price: 92
Enter the number of pieces in bale2: 240
Enter the Single Piece price: 85
Enter the number of pieces in bale3: 350
Enter the Single Piece price: 96
There are 890 pieces in 3 bales
you have to pay 81600 rupees to the bale company
bale 1 has 350 with 96 rupess per picece
bale 2 has 350 with 96 rupess per picece
bale 3 has 350 with 96 rupess per picece
You can See last three lines in the output, i need exact answers which i have given as input but program executed final input values only.
please help me to retrieve array value as per given input.
最满意答案
问题是Bale中的BaleCost是静态的,因此您始终将成本设置为相同的变量。 但真正的问题是设计根本不是面向对象的。
尝试创建一个工厂来创建Bale,并使用Bale作为POJO来存储数据。
HIH,巴尔加斯
The issue is Your BaleCost in Bale is static so you are always setting the cost to the same variable. But the real problem is that the design is not object oriented at all.
Try creating a factory for create the Bale's and use Bale just as a POJO to store the data.
HIH, Bargas
从数组对象中检索值(retrieving values from array objects)首先是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
共有890件3包
你必须向捆包公司支付81600卢比
打包1有350个,每个picece有96卢比
Bale 2有350个,每个picece有96卢比
Bale 3有350个,每个picece有96卢比
你可以看到输出中的最后三行,我需要我作为输入提供的确切答案,但程序只执行最终输入值。
请帮我根据给定的输入检索数组值。
First of all am new to java.
below program is based on a shop, creating my own program which i face in my own shop.
program is running perfectly but i have problem in retrieving values from an array
this is object
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; } }this is main program
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; } } }input and output
Enter the number of bales you bought: 3
Enter the number of pieces in bale1: 300
Enter the Single Piece price: 92
Enter the number of pieces in bale2: 240
Enter the Single Piece price: 85
Enter the number of pieces in bale3: 350
Enter the Single Piece price: 96
There are 890 pieces in 3 bales
you have to pay 81600 rupees to the bale company
bale 1 has 350 with 96 rupess per picece
bale 2 has 350 with 96 rupess per picece
bale 3 has 350 with 96 rupess per picece
You can See last three lines in the output, i need exact answers which i have given as input but program executed final input values only.
please help me to retrieve array value as per given input.
最满意答案
问题是Bale中的BaleCost是静态的,因此您始终将成本设置为相同的变量。 但真正的问题是设计根本不是面向对象的。
尝试创建一个工厂来创建Bale,并使用Bale作为POJO来存储数据。
HIH,巴尔加斯
The issue is Your BaleCost in Bale is static so you are always setting the cost to the same variable. But the real problem is that the design is not object oriented at all.
Try creating a factory for create the Bale's and use Bale just as a POJO to store the data.
HIH, Bargas
发布评论