For Moon, please help me

Asked 2 years ago, Updated 2 years ago, 115 views

It's Java. I'm going to make a lotto number application simple.

In the For Moon, where you turn the order according to the number of spinners, If you take a picture of log, there are 3, 4, and so on The actual output is only the last array.

Please help me.

package com.example.lotto;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.Arrays;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {

    private final static String TAG = "[JYIM]";
    private Spinner mCountSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate");
        initializeUI();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initializeUI() {
        mCountSpinner = findViewById(R.id.spinner);
        Button mGetNumberButton = findViewById(R.id.btn_num);
        mGetNumberButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //lotto();
        makeLottoNumber();
    }

    private void makeLottoNumber() {
        EditText numberText1 = findViewById(R.id.number1);
        EditText numberText2 = findViewById(R.id.number2);
        EditText numberText3 = findViewById(R.id.number3);
        EditText numberText4 = findViewById(R.id.number4);
        EditText numberText5 = findViewById(R.id.number5);
        EditText numberText6 = findViewById(R.id.number6);

        int lottoCount = Integer.parseInt((String) mCountSpinner.getSelectedItem());
        String[] inputNumber = new String[]{numberText1.getText() + "", numberText2.getText() + "", numberText3.getText() + "", numberText4.getText() + "", numberText5.getText() + "", numberText6.getText() + ""};
        String[] resultNumber = new String[6];

        For (inti = 0; i < lottoCount; i++) {//10. Repeat 1 to 5 times as many as the number of passengers selected by the spitter.
            For (int j = 0; j < inputNumber.length; j++) {//1. Verify that 6 input spaces contain numbers.
                //2. Determine the existence of numbers in each compartment.
                // 2.1 If present, store numbers in the array.
                // 2.2 If not, create a random number and store the number in the array.
                resultNumber[j] = inputNumber[j].equals("") ? getRandomNumber(): inputNumber[j];//triomial operation
            }
            // 3.2 If there is no duplication, go to 4
            //4. Arrange the arrays in ascending order.
            If (hasDuplicateNumber) {//3. Check if there is a duplicate of the numbers stored in the array.
                // 3.1 If there is a duplicate, move to 1.
                //TODO: implement;
                i--;

            } } else {
                printResult(resultNumber);
            }
        }
    }

    private void printResult(String[] resultNumber) {
        TextView lottonumber1 = findViewById(R.id.lotto1);
        int[] result = new int[resultNumber.length];
        for (int i = 0; i < resultNumber.length; i++) {
            try {
                result[i] = Integer.parseInt(resultNumber[i]);
            } } catch (NumberFormatException e) {
                result[i] = 0;
            }
        }

        Arrays.sort(result);
        StringBuilder sb = new StringBuilder();

            sb.append("{");
            for (int s : result) {
                sb.append(" ").append(s).append(" ");
            }
            sb.append("}");
            lottonumber1.setText(sb.toString());
            sb.append("\n");
            Log.e(TAG, sb.toString());
        }

    private boolean hasDuplicateNumber(String[] resultNumber) {
        //TODO: implement
        for (int i = 0; i < resultNumber.length; i++) {
            for (int j = 0; j < i; j++) {
                if (resultNumber[i].equals(resultNumber[j])) {
                    return true;
                }
            }
        }
        return false;
    }

    private String getRandomNumber() {
        //TODO: implement random function
        int result = (int) (Math.random() * 45 + 1);
        return String.valueOf(result);
    }

}

java android-studio for array

2022-09-22 20:42

1 Answers

for (int s : result) {
    sb.append(" ").append(s).append(" ");
 }
sb.append("}");
lottonumber1.setText(sb.toString());
sb.append("\n");
Log.e(TAG, sb.toString());

Here I think lotonumber1 is textview I think they're printing it here.

Since you keep doing setText on lotonumber1, Of course, only the last sequence is printed.

If you think about it easily, you keep overwriting the result, so you only see the last result.


2022-09-22 20:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.