手动录入数组,数据,排序,计算平均数。

排序,计算平均数。

package com.stx.exercise.p2;

import java.util.Scanner;

/**
 * This program can do receive some numbers .it will sort the numbers and calculate the average.
 * @param va  will be save the sum.
 * @param num is record the array length.
 */
public class Four_TanZheng1 {
int va;
int num;
/**
 * the method will receive numbers and sort finally calculate the average
 * */
    void receiveGrade() {
        Scanner input = new Scanner(System.in);
        System.out.println("input your number split by comma:");
        String str = input.nextLine();
        String arrStr[] = str.split(",");
        double arrInt[] = new double[arrStr.length];
        for (int i = 0; i < arrStr.length; i++) {
            double a = Integer.parseInt(arrStr[i]);
            arrInt[i] = a;
        }
        /**
         * the method is bubble sort .
         * idea is using for loop the length and compare the value.
         * meanwhile set the flag judge the all condition if the all element was sorted that  will be over;
         * */
        boolean flag = true;
        for (int i = 0; i < arrInt.length - 1; i++) {
            for (int j = 0; j < arrInt.length - 1 - i; j++) {
                if (arrInt[j] > arrInt[j + 1]) {
                    double temp = arrInt[j];
                    arrInt[j] = arrInt[j + 1];
                    arrInt[j + 1] = temp;

                    flag = false;
                }
            }
            if (flag) {
                break;
            }

        }
        /*
        * create the double type arrays copy before array that was watch the elements.
        * other record the sum
        * */
        System.out.println("sorted arrays:");
        double[] generativeGrade=arrInt;
        for (double arr:generativeGrade){
            System.out.println(arr);
        }
        System.out.println();
        double[] aa=arrInt;
        for (int i = 0; i < aa.length; i++) {
            va+=aa[i];
        }
        System.out.println("you average is :"+va);
        //record the times was calculate the average.
        num=aa.length;
        System.out.println(va/num);
    }

    public static void main(String[] args) {
        Four_TanZheng1 tan = new Four_TanZheng1();
        tan.receiveGrade();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下代码将 Excel 数据录入到 C# 数组中: 1. 安装 `Microsoft.Office.Interop.Excel` 库。 2. 引用命名空间 `using Microsoft.Office.Interop.Excel;` 3. 打开 Excel 文件并获取工作表: ```csharp Application excel = new Application(); Workbook workbook = excel.Workbooks.Open(@"C:\example.xlsx"); Worksheet worksheet = workbook.Worksheets[1]; ``` 4. 获取数据范围: ```csharp Range range = worksheet.UsedRange; ``` 5. 获取数据范围的行和列: ```csharp int rows = range.Rows.Count; int columns = range.Columns.Count; ``` 6. 创建数组并将数据录入: ```csharp string[,] data = new string[rows, columns]; for (int row = 1; row <= rows; row++) { for (int column = 1; column <= columns; column++) { Range cell = range.Cells[row, column]; if (cell.Value2 != null) { data[row - 1, column - 1] = cell.Value2.ToString(); } } } ``` 完整代码如下: ```csharp using Microsoft.Office.Interop.Excel; ... Application excel = new Application(); Workbook workbook = excel.Workbooks.Open(@"C:\example.xlsx"); Worksheet worksheet = workbook.Worksheets[1]; Range range = worksheet.UsedRange; int rows = range.Rows.Count; int columns = range.Columns.Count; string[,] data = new string[rows, columns]; for (int row = 1; row <= rows; row++) { for (int column = 1; column <= columns; column++) { Range cell = range.Cells[row, column]; if (cell.Value2 != null) { data[row - 1, column - 1] = cell.Value2.ToString(); } } } workbook.Close(); excel.Quit(); ``` 请注意,在使用完 Excel 后,您需要关闭工作簿和 Excel 应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值