概述
package com.project.test;
import java.io.*;
import java.util. *;
public class Guess {
private String name;
private int score;
public String getName()
{
return name;
}
public void setName(){
Scanner input=new Scanner(System.in);//输入用户名
System.out.print("请输入您的姓名:");
name=input.next();
}
public int star(){
Scanner input=new Scanner(System.in);
int count;//猜测的次数
int[]counts=new int[1];
score=0;//定义成绩
//捕获可能出现的异常
try{
for(int i=0;i<1;i++){
count=0;
int number =(int)(Math.random()*100+1);
long startTime=System.currentTimeMillis();//开始时间
System.out.print("请输入您猜的数:");
int guessnum=input.nextInt();
count++;counts[i]+=1;
while(guessnum!= number){
if(guessnum>100||guessnum<0)
{
System.out. println("输入错误,请重新输入!");
return 0;
}else if(guessnum>number){
System.out.print("猜大了,请重新猜测:");
guessnum=input.nextInt();
count++;counts[i]+=1;
}else{
System.out.print("猜小了,请重新猜测:");
guessnum=input.nextInt();
count++;counts[i]+=1;
}
}
long endTime= System.currentTimeMillis();
score=(int)((endTime-startTime )/1000)+ count;
counts[i] =score;
System.out.println("恭喜您,猜对了!正确答案是:"+number);
System.out. println("您一共猜测了"+count+"次猜数时长为:"+(endTime-startTime)/1000+"s");
System.out.print("评语:");
switch(count)
{
case 1:System.out.println("运气很好,一次就猜对了!");break;
case 2:
case 3:
case 4:
case 5:System.out.println("时间这么快!");break;
case 6:
case 7:
case 8:
case 9:
case 10:System.out. println("时间有点长哦!");break;
default:System.out.println("运气太糟糕了!");break;
}
System.out.println("您的成绩是:"+score+"分");
}
}catch(Exception e){
e.printStackTrace();}
return score;
}
}
package com.project.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Shujuku {
public static void main(String[] args){
Guess st=new Guess();
st.setName();
st.getName();
int score=st.star();
String name=st.getName();
Connection con;
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/suanfa";//与数据库中创建的数据库名字一致
String user="root";//账号
String password="root";//密码
try{
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
if(!con.isClosed())
System.out.println("Sucess");
Statement statement=con.createStatement();
System.out.println(name+","+score);
String sql = "insert into wangao values('"+name+"',"+score+")";
String sql1 = "select * from wangao";//向数据库中输入插入删除等命令
//statement.executeUpdate(sql);
int rs=statement.executeUpdate(sql);
ResultSet rs1=statement.executeQuery(sql1);
System.out.println("------------");
System.out.println("姓名"+"t"+"成绩");
System.out.println("------------");
//String no = null;
//name = null;
String name1;
int score1=0;
while(rs1.next()){
name1=rs1.getString("name");//数据库中表的对应名字
score1=rs1.getInt("score");
System.out.println(name1+ "t"+score1);
}
rs1.close();
con.close();
}catch(ClassNotFoundException e){
System.out.println("Sorry");
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("数据库成功获取");
}
}
}
最后
以上就是不安樱桃为你收集整理的java课程设计猜数字游戏的全部内容,希望文章能够帮你解决java课程设计猜数字游戏所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复