数组的最小值始终等于零

时间:2015-08-29 21:42:52

标签: java android

我正在尝试创建延迟计时器,我正在使用System.nanotime来执行此操作。我的问题是我用来保持时间的数组的最小数量总是等于零。我遇到的另一个问题是我打印出Time和grayValGlobal的值,它们并不是一个接一个地出现,我觉得这与我的第一个问题有关。

public class startTestAsync extends AsyncTask<Double, Void, Void> {
    long [] timeArray = new long[500]; // set array
    long rating;
    long lagStartTime;
    long lagEndTime;
    long lagTimeResult;
    int testAmount;
    int cnt;
    @Override
    protected Void doInBackground(Double... grayVal) {
        for (testAmount = 0; testAmount < 500; testAmount++) {
            lagStartTime = System.nanoTime(); //start lagTimer start
            runOnUiThread(new Runnable() {
                public void run() {
                    lagSquare.setBackgroundColor(Color.rgb(255, 255, 255));
                }
            });
            while (grayValGlobal >= 100.0) {
                Log.i("Mat Value Async", String.valueOf(grayValGlobal));
                runOnUiThread(new Runnable() {
                    public void run() {
                        lagSquare.setBackgroundColor(Color.rgb(000, 000, 000));//set lagSquare black
                    }
                });
lagEndTime = System.nanoTime();
lagTimeResult = (lagEndTime - lagStartTime) / 1000000;
timeArray[testAmount] = lagTimeResult;
              Log.i("Time", String.valueOf(timeArray[testAmount]));
            }
        }
        //min scan
        long minTimeArray = timeArray[0];
        for (cnt = 0; cnt < timeArray.length; cnt++) {
            if (timeArray[cnt] < minTimeArray) {
                minTimeArray = timeArray[cnt];
            }
        }
        Log.i("MinTimeArray", String.valueOf(minTimeArray));
        final String minTimeArrayString = String.valueOf(minTimeArray);
        rating = minTimeArray;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                lagTime.setText(minTimeArrayString);
                if (rating <= 17.0) {
                    lagRating.setText("Excellent");
                }
                else if (rating <= 34.0){
                    lagRating.setText("Great");
                }
                else if (rating <= 51.0){
                    lagRating.setText("Average");
                }
                else {
                    lagRating.setText("Bad");
                }
            }
        });
        return null;
    }
}

logcat的

08-29 17:10:27.849   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4102.355956
08-29 17:10:27.850   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4102.569579
08-29 17:10:27.850   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4102.966307
08-29 17:10:27.850   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.851   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4103.332518
08-29 17:10:27.851   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.851   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4103.668212
08-29 17:10:27.851   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4103.912352
08-29 17:10:27.851   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.851   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4104.339598
08-29 17:10:27.852   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4104.980467
08-29 17:10:27.852   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4105.163573
08-29 17:10:27.853   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4105.438232
08-29 17:10:27.853   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4105.651855
08-29 17:10:27.853   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.853   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.853   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.854   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4106.445312
08-29 17:10:27.854   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4106.689453
08-29 17:10:27.854   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4106.903076
08-29 17:10:27.854   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4107.116699
08-29 17:10:27.854   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4107.330322
08-29 17:10:27.855   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4107.543945
08-29 17:10:27.855   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.855   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4107.910156
08-29 17:10:27.855   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.856   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.856   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.856   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.856   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.857   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4109.49707
08-29 17:10:27.857   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.857   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0
08-29 17:10:27.857   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4110.290527
08-29 17:10:27.858   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4110.50415
08-29 17:10:27.858   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4110.74829
08-29 17:10:27.858   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4110.961913
08-29 17:10:27.858   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4111.206054
08-29 17:10:27.859   9897-11295/bluemage.systems.displaylatencytester I/Mat Value Async﹕ 131.0 
08-29 17:10:27.859   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4111.877441
08-29 17:10:27.859   9897-10042/bluemage.systems.displaylatencytester I/Mat Value﹕ 95.0
08-29 17:10:27.859   9897-11295/bluemage.systems.displaylatencytester I/Time﹕ 4112.213134
08-29 17:10:27.862   9897-11295/bluemage.systems.displaylatencytester I/MinTimeArray﹕ 0.0

2 个答案:

答案 0 :(得分:1)

您只执行了五次滞后测试,但您的timearray有500个元素。一旦最小扫描仪到达迭代6,您将获得最小值0,此后将永远不会改变。

答案 1 :(得分:0)

尝试检查数组中的所有变量

    //min scan
    double minTimeArray = timeArray[0];
    for (cnt = 0; cnt < timeArray.length; cnt++) {
        if (timeArray[cnt] < minTimeArray) {
            minTimeArray = timeArray[cnt];
        }
    Log.i("Array", "value: " + String.valueOf(minTimeArray) + "  at: " + cnt);
    }

或者你可以添加这个

if (timeArray[cnt] < minTimeArray && timeArray[cnt] != 0) 

to&#34; if&#34;声明