아래는 덧셈 연산 퀴즈 문제를 랜덤함수를 이용하여 문제를 만들고 , 정답과 오답을 버튼에 뿌려주는 소스입니다.
public void play(){
setContentView(R.layout.start);
num = Rnd.nextInt(4);
i = Rnd.nextInt(100) + 1;
j = Rnd.nextInt(100) + 1;
sum = i+j;
TextView info=(TextView)findViewById(R.id.test_info);
info.setText(String.valueOf("문제수:"+testNumber+ " 정답수:"+passNumber+" 오답수:"+nopassNumber));
TextView s=(TextView)findViewById(R.id.test);
s.setText(String.valueOf(i+"+"+j));
numButton[0]=(Button)findViewById(R.id.num1_button);
numButton[1]=(Button)findViewById(R.id.num2_button);
numButton[2]=(Button)findViewById(R.id.num3_button);
numButton[3]=(Button)findViewById(R.id.num4_button);
numButton[num].setText(String.valueOf(sum));
for(num=0;num<4;num++)
{
if(numButton[num].getText() == ""){
x = Rnd.nextInt(100) ;
numButton[num].setText(String.valueOf(x));
}
}
View numButton1 = this.findViewById(R.id.num1_button);
numButton1.setOnClickListener(this);
View numButton2 = this.findViewById(R.id.num2_button);
numButton2.setOnClickListener(this);
View numButton3 = this.findViewById(R.id.num3_button);
numButton3.setOnClickListener(this);
View numButton4 = this.findViewById(R.id.num4_button);
numButton4.setOnClickListener(this);
}
아래의 소스는 버튼을 클릭하였을 경우 정답인지, 오답인지 체크를 해주는 부분인데요 ..
if문에서 버튼의 텍스트가 sum에 해당하는지를 판단하여 정답수를 올려줘야 하는데 ..
오류가 자꾸 나오네요 ....... 어떤식으로 풀어야하는지 ..
고수님들 좀 알려주세여T_T
public void onClick(View v){
testNumber++;
switch (v.getId()){
case R.id.num1_button:
if(numButton[0].getText() == getString(sum)){
passNumber++;
}
else {
nopassNumber++;
}
play();
break;
case R.id.num2_button:
play();
break;
case R.id.num3_button:
play();
break;
case R.id.num4_button:
play();
break;
}
}
얼핏 봐서 정확치는 않습니다만, 위의 소스에서 정답수인지 오답수인지를 확인하는 if 구문에 문제가 있는 듯 싶습니다.
sum은 Int형인것 같구요, 오류는 Resouce Not Found Exception이 아닌지요? 안드로이드 activity의 getString(int resId)은 R.java에 적재된(Context상에? - 저는 아직 context가 헤깔려서...) 리소스의 값(return String)을 불러오기 위한 함수입니다. int를 string으로 형변환하는 함수는 아닙니다... 다음과 같이 변경하면 되지 않을까요...
try{
String str = ""+sum;
integer.parseint(str);
if( 정답수 인가 아닌가 ){
}else{
}
}catch(NumberFormatException e){;}




구체적으로 어떤 오류가 나신다는 건지?
참고로 문자열은 == 로 비교하면 문자열의 주소가 같은지 비교하게 됩니다.
문자열의 내용이 같은지 비교하시려면, .equals() 메서드를 사용하세요.