타자 연습 게임을 만들어보자
3개의 문장을 배열에 저장하고, 각 문장을 보여주고 사용자가 해당 문장을 입력하고 각 문장별 정확도와 타자시간을 출력해보자
정확도 = (맞춘 글자수 / 총 글자수) * 100
1. 배열에다가 문장을 추가
2. 사전에 저장된 문장을 하나씩 출력한다.
3. 출력된 문장과 같은 사용자의 입력을 받는다.
4. 입력 받은 문장과 배열에 저장된 문장을 검증
public class PracticeGame {
public static void main(String[] args) {
String[] sentences = new String[3];
sentences[0] = "안녕하세요";
sentences[1] = "나는 누구 입니다";
sentences[2] = "안녕히 가세요";
System.out.println("----------타자 연습 게임을 시작합니다----------");
System.out.println("아래에 나타나는 문장과 똑같이 정확하게 입력하세요.");
System.out.println();
System.out.println("============================= START =============================");
System.out.println();
Scanner sc = new Scanner(System.in);
for (var s = 0; s < sentences.length; s++){
System.out.println(sentences[s]);
var startTime = System.currentTimeMillis();
var input = sc.nextLine();
var endTime = System.currentTimeMillis();
var diffTime = (endTime-startTime)/1000;
System.out.println("소요시간은 "+diffTime+"초 입니다");
var accuracy = PracticeGame.calculator(sentences[s], input);
System.out.println("정확도는 "+ accuracy + "%");
}
}
public static double calculator(String sentence, String word){
int correctChar = 0;
for (int i = 0; i<Math.min(sentence.length(), word.length()); i++){
if (sentence.charAt(i) == word.charAt(i)) correctChar++;
}
var rate = ((double)correctChar / sentence.length()*100);
var formatStr = String.format("%2.2f",rate);
return Double.parseDouble(formatStr);
}
}
'Java 응용' 카테고리의 다른 글
Java 응용 (6. 랜덤한 숫자 맞추기) (0) | 2024.04.08 |
---|---|
Java 응용 (5. 이메일 검색 서비스) (0) | 2024.03.14 |
Java 응용 (4. 숫자 위치 맞추기) (0) | 2024.03.06 |
Java 응용 (3. 로또 번호 추첨하기) (0) | 2024.03.05 |
Java 응용 (1. 가위바위보 게임) (0) | 2024.03.04 |