JAVA大作业
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)
{
printMenu();
content = scan.nextLine();
System.out.println();
//输入内容是2,则退出程序
if(content.equals("2")) break;
if(content.equals("1"))
{
System.out.print("Text to Match: ");
int count = r.nextInt(50) + 1;
char[] letters = new char[count];
for(int i=0;i<count;i++)
{
letters[i] = (char)(r.nextInt(26) + 97);
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])
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();
}
}
}
求指导这代码的详细作用 求帮助真心的 展开
一句一句看,其实不难的
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();
}
}
}
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; //定义一个变量content 类型是String
Scanner scan = new Scanner(System.in);//读取用户输入
while(true) //表示这个循环是一直存在的
{
printMenu();//调用此方法后台代码显示此方法的2个输出语句(1)play ...,(2)exit ...
content = scan.nextLine();//获取你输入的值
System.out.println("你输入的值是:"+content);//打印你输出的值
//输入内容是2,则退出程序
if(content.equals("2")) break;
if(content.equals("1"))//如果输入的是1则执行此模块
{
System.out.print("Text to Match: ");//打印输出语句
int count = r.nextInt(50) + 1;//表示count在1-50之间的一个随机数 nexInt(50)=[1,50)吧建议上网找找太久了我忘了...
char[] letters = new char[count];//创建一个字符数组长度为随机产生的count
for(int i=0;i<count;i++)
{
letters[i] = (char)(r.nextInt(26) + 97);//这一步应该是小写转换 在ascii码中a=97
System.out.print(letters[i]);
}
System.out.print("\n ");//换行其实代码=system.out.println();
long startTime = System.currentTimeMillis();//声明开始时间且赋值
int correctCount = 0;
content = scan.nextLine();//这里面应该写的不对,因为你前面已经执行了while(true)里面的如果还想执行再声明一个变量就好了,你这些代码写的不好后面我懒得看了
for(int i=0;i<count;i++)
{
if(content.charAt(i) == letters[i])
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();
}
}
}