개인공부
1차원 배열문제
리승우
2022. 8. 9. 23:13
import java.util.Scanner;
public class q5_6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] words = { "television","computer","mouse","phone"};
Scanner scanner = new Scanner(System.in);
//toCharArray를 사용함으로써,
//question에는 {'t','e','l','e','v','i','s','i','o','n'}가 가게된다
for(int i=0; i<words.length; i++) {
char[] question = words[i].toCharArray();
//System.out.println("0 ~ 1 사이의 랜덤 숫자 : " + Math.random());
//System.out.println("0 ~ 100 사이의 랜덤 숫자 : " + (int)(Math.random() * 100));
//System.out.println("0 ~ 10000 사이의 랜덤 숫자 : " + (int)(Math.random() * 10000));
//question을 다시 for문으로 돌린 뒤,
//Math.random()*question.length로 0~길이만큼 랜덤으로 뽑은 숫자를 idx에 넣는다
//question[0]에 있는 것은, tmp에 넣고
//question[random]에 있는 것은, question[0]에 넣고
//tmp에 있는 것은 question[random]에 넣음으로써 서로 섞는다
for(int j=0; j<=question.length;j++) {
int idx = (int)(Math.random()*question.length);
char tmp = question[i];
question[i] = question[idx];
question[idx] = tmp;
}
System.out.printf("Q%d. %s의 정답을 입력하세요.>",
i+1, new String(question));
String answer = scanner.nextLine();
if(words[i].equals(answer.trim()))
System.out.printf("맞았습니다.%n%n");
else
System.out.printf("틀렸습니다.%n%n");
}
}
}