问题描述
- java 难题 求大侠帮忙。小弟谢过了
-
求大虾帮帮忙。 由于我底子薄,最好能用java 代码帮我解决下。
各组 ,每组有不同月份的业绩。一共两个for循环,
for (i = 0; i<=组 ; i++)
{
for(j = 0 ;j< = 月份; j++)
{
这里是循环到月份 的业绩。取得到当月业绩。
}}
例如:一共四个组,四个月份。按月份划分, 我要算出每个月的所有组业绩之和。一组:一月 100 。二月200 。三月300. 四月。400
二组:100 200 300 400
三组:100 200 300 400
四组 同上, 最终算出1月 400 。二月800 。三月1200.。。最后得出一个list 或者 数组都可以,长度为总月份。里面包含每个月的业绩。可能用到二维数组,或者HashMap
问题: 怎么在上面公式里追加计算。。
解决方案
组数确定不?如果确定的话,可以考虑这种简单的做法:
// 总共有四组,每一组有四个月的业绩数据:100,200,300,400
int A[][] = { { 100, 200, 300, 400 }, { 100, 200, 300, 400 }, { 100, 200, 300, 400 }, { 100, 200, 300, 400 } };
int B[] = new int[4];
for (int i = 0; i < A.length; i++) {// 遍历组数
// 目标:计算出每个月的总业绩
B[i] = A[0][i]
+ A[1][i]
+ A[2][i]
+ A[3][i];
}
for (int i = 0; i < B.length; i++) {
System.out.println("各月的总业绩:"
+ B[i]);
}
测试过了,同样能实现。
解决方案二:
这里是测试结果。如果不是你想要的,可以留言。希望能够帮到你。
解决方案三:
组数,月份都不确定,十二月以内
解决方案四:
public class SumPerformanceOfMonth
{
//四个月的业绩集合
private static List mpList;
static
{
mpList = new ArrayList<MouthPerformance>();
for (int i = 1; i < 5; i++)
{
//每组业绩
Map<String, Integer> performance = new HashMap<String, Integer>();
performance.put("一组", 100 * i);
performance.put("二组", 100 * i);
performance.put("三组", 100 * i);
performance.put("四组", 100 * i);
mpList.add(new MouthPerformance(i + "月", performance));
}
}
public static void main(String[] args)
{
Map<String, Integer> countPerformance = new HashMap<String, Integer>();
//遍历四个月的业绩信息
for (int i = 0; i < mpList.size(); i++)
{
MouthPerformance m = (MouthPerformance)mpList.get(i);
Map<String, Integer> mp = m.getPerformance();
int sum = 0;
for (String s : mp.keySet())
{
sum += mp.get(s);
}
countPerformance.put(m.getMouth(), sum);
}
System.out.println(countPerformance);
}
}
//每月业绩
class MouthPerformance
{
//月份
private String mouth;
//每组业绩
private Map<String, Integer> performance;
MouthPerformance(String mouth, Map<String, Integer> performance)
{
this.mouth = mouth;
this.performance = performance;
}
/**
* @return 返回 mouth
*/
public String getMouth()
{
return mouth;
}
/**
* @param 对mouth进行赋值
*/
public void setMouth(String mouth)
{
this.mouth = mouth;
}
/**
* @return 返回 performance
*/
public Map<String, Integer> getPerformance()
{
return performance;
}
/**
* @param 对performance进行赋值
*/
public void setPerformance(Map<String, Integer> performance)
{
this.performance = performance;
}
}
解决方案五:
业绩是在那个for循环里才出现的。我的问题说的有些模糊,必须在上面两个for循环基础上做。
解决方案六:
int a[][]={100,200,300,400;100,200,300,400;100,200,300,400;100,200,300,400};
int b[]=new int[4];
for(int i=0;i<3;i++){
b[i]=0;
}
for (i = 0; i<4 ; i++)
{
for(j = 0 ;j< 4; j++)//注意不是小于等于
{
b[i]+=a[i][j];
}
}
解决方案七:
public class Results {
/*/
* 月份和每个组的成绩都不确定
*/
public static void main(String[] args) {
//总业绩二维数组,不知道你看得懂吧
int[][] performance={{100,200,300,400},{100,200,300,400},{100,200,300,400},{100,200,300,400}};
//月份数
int[] month={1,2,3,4};
for(int i=0;i<month.length;i++){
System.out.println(i+1+"月"+"业绩为"+sum(i, performance));
}
}
/*/
* 传入月份数和总业绩,返回每个月的业绩
*/
public static int sum(int month,int[][] performance){
int sum=0;
int[] results = null;
for(int i=0;i<performance.length;i++){
results=performance[i];
sum+=performance[i][month];
}
return sum;
}
}
月份数,还有总业绩的二维数组都可以随意变换的.
解决方案八:
public class Results {
/*/
* 月份和每个组的成绩都不确定
*/
public static void main(String[] args) {
//总业绩二维数组,不知道你看得懂吧
int[][] performance={{100,200,300,400},{100,200,300,400},{100,200,300,400},{100,200,300,400}};
//月份数
int[] month={1,2,3,4};
for(int i=0;i<month.length;i++){
System.out.println(i+1+"月"+"业绩为"+sum(i, performance));
}
}
/*/
* 传入月份数和总业绩,返回每个月的业绩
*/
public static int sum(int month,int[][] performance){
int sum=0;
int[] results = null;
for(int i=0;i<performance.length;i++){
results=performance[i];
sum+=performance[i][month];
}
return sum;
}
}
月份数,还有总业绩的二维数组都可以随意变换的.
解决方案九:
public class Results {
/*/
* 月份和每个组的成绩都不确定
*/
public static void main(String[] args) {
//总业绩二维数组,不知道你看得懂吧
int[][] performance={{100,200,300,400},{100,200,300,400},{100,200,300,400},{100,200,300,400}};
//月份数
int[] month={1,2,3,4};
for(int i=0;i<month.length;i++){
System.out.println(i+1+"月"+"业绩为"+sum(i, performance));
}
}
/*/
* 传入月份数和总业绩,返回每个月的业绩
*/
public static int sum(int month,int[][] performance){
int sum=0;
int[] results = null;
for(int i=0;i<performance.length;i++){
results=performance[i];
sum+=performance[i][month];
}
return sum;
}
}
月份数,还有总业绩的二维数组都可以随意变换的.
解决方案十:
public class Results {
/*/
* 月份和每个组的成绩都不确定
*/
public static void main(String[] args) {
//总业绩二维数组,不知道你看得懂吧
int[][] performance={{100,200,300,400},{100,200,300,400},{100,200,300,400},{100,200,300,400}};
//月份数
int[] month={1,2,3,4};
for(int i=0;i<month.length;i++){
System.out.println(i+1+"月"+"业绩为"+sum(i, performance));
}
}
/*/
* 传入月份数和总业绩,返回每个月的业绩
*/
public static int sum(int month,int[][] performance){
int sum=0;
int[] results = null;
for(int i=0;i<performance.length;i++){
results=performance[i];
sum+=performance[i][month];
}
return sum;
}
}
月份数,还有总业绩的二维数组都可以随意变换的.