发布网友 发布时间:2022-05-26 17:50
共2个回答
热心网友 时间:2023-10-14 08:09
一句一句看,其实不难的
import java.util.Random;
import java.util.Scanner;
public class Test {
public static Random r = new Random();
public static void printMenu() { //自定义的方法
System.out.println("(1) play another round");
System.out.println("(2) exit the game");
System.out.print("Selection:");
}
public static void main(String[] args) throws Exception
{
String content = null;
Scanner scan = new Scanner(System.in); //
while (true) //true在这里使得程序一直循环
{
printMenu(); //调用方法,打印出三句话 ...
content = scan.nextLine(); //读取输入
System.out.println();
// 输入内容是2,则退出程序
if (content.equals("2"))
break; //break 跳出当前的循环,也就是跳出while循环,程序往下执行,若没有可执行则结束程序
if (content.equals("1")) {
System.out.print("Text to Match: ");
int count = r.nextInt(50) + 1; //得到1~50随机数,即[1,50]
char[] letters = new char[count]; //长度为count的char数组
for (int i = 0; i < count; i++) //i < count等价i < letters.length
{
letters[i] = (char) (r.nextInt(26) + 97); //给letters[i]赋值,字母a~z
System.out.print(letters[i]);
}
System.out.print("\n ");
long startTime = System.currentTimeMillis(); //得到开始时间
int correctCount = 0;
content = scan.nextLine(); //得到输入的字符串
for (int i = 0; i < count; i++) {
if (content.charAt(i) == letters[i]) //输入的字符串的每个字母和letters中的比较
correctCount++;
}
long endTime = System.currentTimeMillis(); //得到结束时间
int time = (int) ((endTime - startTime) / 1000); //计算所花的时间
if (correctCount == 0)
System.out
.println("INCORRECTLY TYPED, YOU GET A SPEED OF 0!");
else {
System.out.println("共花了" + time + "秒,正确率为:"
+ ((int) (((double) correctCount / count)) * 100)
+ "%");
}
}
System.out.println();
}
}
}
热心网友 时间:2023-10-14 08:10
package c.controller;