gpt4 book ai didi

java - 如何为我拥有的字符串设置最小长度?

转载 作者:搜寻专家 更新时间:2023-11-01 08:32:23 24 4
gpt4 key购买 nike

我有一个 Editable,我在 if 语句中将其变成了一个 String。有没有办法让 if 语句仅在给定字符串少于 9 个字符时才适用?请看下面:

        if (answerSix.toString().equals("")) {


} else {

score = score + 20;

}

return score;

我将 boolean 值 answerSix 设置为一个字符串,但它仅适用于给定字段中没有文本的情况。如果字符串少于 9 个字符,我希望它适用。请让我知道,如果你有任何问题。我感谢所有的帮助。

这是我的代码:

package com.example.android.quantummechanicsquiz;

import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

int score = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
score = 0;
}


public void radioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();

switch (view.getId()) {
case R.id.radio_one_correct:
if (checked) break;

case R.id.radio_one_b:
if (checked) break;

case R.id.radio_one_c:
if (checked) break;

case R.id.radio_one_d:
if (checked) break;

case R.id.radio_two_a:
if (checked) break;

case R.id.radio_two_b:
if (checked) break;

case R.id.radio_two_correct:
if (checked) break;

case R.id.radio_two_d:
if (checked) break;

case R.id.radio_four_a:
if (checked) break;

case R.id.radio_four_b:
if (checked) break;

case R.id.radio_four_correct:
if (checked) break;

case R.id.radio_four_d:
if (checked) break;

case R.id.radio_five_a:
if (checked) break;

case R.id.radio_five_b:
if (checked) break;

case R.id.radio_five_c:
if (checked) break;

case R.id.radio_five_correct:
if (checked) break;


}

}


private int calculateScore(boolean answerOne, boolean answerTwo, boolean answerThreeA,
boolean answerThreeB, boolean answerThreeC, boolean answerThreeD,
boolean answerFour, boolean answerFive, Editable answerSix) {
score = 0;

if (answerOne) {

score = score + 100 / 6;


} else {


}

if (answerTwo) {

score = score + 100 / 6;

} else {


}


if (answerThreeA || answerThreeD) {



} else if (answerThreeB && answerThreeC){

score = score + 100 / 6;
}


if (answerFour) {

score = score + 100 / 6;

} else {


}

if (answerFive) {

score = score + 100 / 6;

} else {


}


if (answerSix.toString().equals("")) {


} else {

score = score + 20;

}

return score;

}


public void submitScore(View view) {
RadioButton answerOne = (RadioButton) findViewById(R.id.radio_one_correct);
boolean correctAnswerOne = answerOne.isChecked();

RadioButton answerTwo = (RadioButton) findViewById(R.id.radio_two_correct);
boolean correctAnswerTwo = answerTwo.isChecked();

CheckBox answerThreeA = (CheckBox) findViewById(R.id.checkbox_three_a);
boolean incorrectAnswerThreeA = answerThreeA.isChecked();

CheckBox answerThreeB = (CheckBox) findViewById(R.id.checkbox_three_b_correct);
boolean correctAnswerThreeB = answerThreeB.isChecked();

CheckBox answerThreeC = (CheckBox) findViewById(R.id.checkbox_three_c_correct);
boolean correctAnswerThreeC = answerThreeC.isChecked();

CheckBox answerThreeD = (CheckBox) findViewById(R.id.checkbox_three_d);
boolean incorrectAnswerThreeD = answerThreeD.isChecked();

RadioButton answerFour = (RadioButton) findViewById(R.id.radio_four_correct);
boolean correctAnswerFour = answerFour.isChecked();

RadioButton answerFive = (RadioButton) findViewById(R.id.radio_five_correct);
boolean correctAnswerFive = answerFive.isChecked();

EditText answerSix = (EditText) findViewById(R.id.question_six_edit_text);
Editable correctAnswerSix = answerSix.getEditableText();

int finalScore = calculateScore(correctAnswerOne, correctAnswerTwo,
incorrectAnswerThreeA, correctAnswerThreeB, correctAnswerThreeC,
incorrectAnswerThreeD, correctAnswerFour, correctAnswerFive, correctAnswerSix);

Toast.makeText(this, "Congratulations! You have a score of " + finalScore + " out of " +
"100", Toast.LENGTH_LONG).show();

}

}

最佳答案

你能做到以下几点吗?

    if (answerSix.toString().length()<9) {

关于java - 如何为我拥有的字符串设置最小长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39650464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com