猜字游戏java课程设计
A. java课程设计程序-猜数游戏
Newload()
{
jf1=new JFrame("猜数游戏");
jf2=new JFrame("猜数游戏");
jf3=new JFrame("猜数游戏");
jf1_title=new JLabel("猜数游戏-欢迎进入");
jf1_title.setFont(new Font("仿宋体",Font.BOLD,40));//设置字体大小,及文字字体
jf1_title.setHorizontalAlignment(JLabel.CENTER);
JLabel jf2title=new JLabel("猜数游戏");
jf2title.setFont(new Font("仿宋体",Font.BOLD,40));//设置字体大小,及文字字体
jf2title.setHorizontalAlignment(JLabel.CENTER);
jf1_username=new JLabel("用户名");
jf1_userpass=new JLabel("密码");
jf2_question=new JLabel("There is question which needs you to guess!");
jf2_question.setFont(new Font("仿宋体",Font.BOLD,20));//设置字体大小,及文字字体
jf2_question.setHorizontalAlignment(JLabel.CENTER);
jf2_rightface=new JLabel(iron1);
jf2_wrongface=new JLabel(iron2);
jf2_rightface.setVisible(false);
jf2_wrongface.setVisible(false);
jf2_reelresult=new JLabel();
jf3_pinyu=new JLabel("your result is");
jf1_usernameT=new JTextField(6);
jf2_anwser=new JTextField(6);
jf2_anwser.addActionListener(this);
jf1_password=new JPasswordField(6);
jf1_password.addActionListener(this);
jf1_ok=new JButton("确定");
jf1_ok.addActionListener(this);
jf1_quit=new JButton("退出");
jf1_quit.addActionListener(this);
jf2_newgame=new JButton("新游戏(k)");
jf2_newgame.setMnemonic(KeyEvent.VK_K);
jf2_newgame.addActionListener(this);
jf2_ok=new JButton("确定");
jf2_ok.addActionListener(this);
jf1.setLayout(new BorderLayout());
jf2.setLayout(new BorderLayout());
JPanel jf1p1=new JPanel(),jf2p1=new JPanel(),jf2p2=new JPanel(),jf2p3=new JPanel();
jf2p1.setLayout(new BorderLayout());
jf1p1.setLayout(new FlowLayout());
jf2p2.setLayout(new FlowLayout());
jf2p3.setLayout(new FlowLayout());
jf1.add(jf1_title,"Center");
jf1p1.add(jf1_username);jf1p1.add(jf1_usernameT);
jf1p1.add(jf1_userpass);jf1p1.add(jf1_password);
jf1p1.add(jf1_ok);jf1p1.add(jf1_quit);
jf1.add(jf1p1,"South");
jf2p2.add(jf2_rightface);
jf2p2.add(jf2_wrongface);
jf2p2.add(jf2_reelresult);
jf2p1.add(jf2p2,"South");
jf2p1.add(jf2_question);
jf2.add(jf2title,"North");
jf2.add(jf2p1,"Center");
jf2p3.add(jf2_ans);jf2p3.add(jf2_anwser);jf2p3.add(jf2_ok);jf2p3.add(jf2_newgame);
jf2.add(jf2p3,"South");
jf3.add(jf3_pinyu);
jf1.setSize(700,400);
jf2.setSize(700,400);
jf3.setSize(700,400);
jf1.setLocation(300,150);
jf2.setLocation(300,150);
jf3.setLocation(300,150);
jf1.setVisible(true);
jf2.setVisible(false);
jf3.setVisible(false);
jf1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf2.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jf1_ok||e.getSource()==jf1_password)
{char[] a=jf1_password.getPassword();String paas="";
for(int i=0;i<a.length;i++)//JPasswordField是一种特殊的类只能得到char数组,将其转成String
paas=paas+a[i];
if(jf1_usernameT.getText().equals("user")&&paas.equals("pass"))
{jf2.setVisible(true);jf1.setVisible(false);
number=returnquestion();
jf2_anwser.requestFocus();
}
else
JOptionPane.showMessageDialog(null,"用户名不正确或密码错误!");
}
if(e.getSource()==jf1_quit)
{
System.exit(0);
}
if(e.getSource()==jf2_ok||e.getSource()==jf2_anwser)
{
if(times<=5){
if(Integer.parseInt(jf2_anwser.getText())==number)
{
jf2_rightface.setVisible(true);
jf2_wrongface.setVisible(false);
jf2_reelresult.setText("you are right! and your have used "+times+" times!"
+((times<=3)?"very good!":"pleas do more work for it"));
}
else
if(Integer.parseInt(jf2_anwser.getText())>number)
{times++;
jf2_wrongface.setVisible(true);
jf2_rightface.setVisible(false);
jf2_reelresult.setText("your answer is bigger than the one proced by computer!"
+"and your have used "+times+" times!");
}
else
if(Integer.parseInt(jf2_anwser.getText())<number)
{times++;
jf2_wrongface.setVisible(true);
jf2_rightface.setVisible(false);
jf2_reelresult.setText("your answer is smaller than the one proced by computer!"
+"and your have used "+times+" times!");
}
}
else
{JOptionPane.showMessageDialog(null,"你已经超过六次了,请重新开始吧!");}
jf2_anwser.requestFocus();
jf2_anwser.setText("");
}
if(e.getSource()==jf2_newgame)
{
number=returnquestion();
times=0;
jf2_rightface.setVisible(false);
jf2_wrongface.setVisible(false);
jf2_anwser.setText("");
jf2_reelresult.setText("");
jf2_anwser.requestFocus();
}
}
public static void main(String args[])
{
new Newload();
}
int returnquestion()
{
double db=Math.random()*100;
return (int)db;
}
}
B. 用java编写一个猜数字游戏,
packageday06;
importjava.util.Scanner;
//猜字符游戏
publicclassGuessingGame{
//主方法
publicstaticvoidmain(String[]args){
Scannerscan=newScanner(System.in);
intcount=0;//猜错的次数
char[]chs=generate();//随机生成的字符数组
System.out.println(chs);//作弊
while(true){//自造死循环
System.out.println("猜吧!");
Stringstr=scan.next().toUpperCase();//获取用户输入的字符串
if(str.equals("EXIT")){//判断str是否是
System.out.println("下次再来吧!");
break;
}
char[]input=str.toCharArray();//将字符串转换为字符数组
int[]result=check(chs,input);//对比
if(result[0]==chs.length){//位置对为5
intscore=chs.length*100-count*10;//一个字符100分,错一次减10分
System.out.println("恭喜你猜对了,得分:"+score);
break;//猜对时跳出循环
}else{//没猜对
count++;//猜错次数增1
System.out.println("字符对:"+result[1]+"个,位置对:"+result[0]+"个");
}
}
}
//随机生成5个字符数组
publicstaticchar[]generate(){
char[]chs=newchar[5];
char[]letters={'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V',
'W','X','Y','Z'};
boolean[]flags=newboolean[letters.length];//1.
for(inti=0;i<chs.length;i++){
intindex;
do{
index=(int)(Math.random()*letters.length);//0到25
}while(flags[index]==true);//2.
chs[i]=letters[index];
flags[index]=true;//3.
}
returnchs;
}
//对比随机数组与用户输入的数组
publicstaticint[]check(char[]chs,char[]input){
int[]result=newint[2];
for(inti=0;i<chs.length;i++){
for(intj=0;j<input.length;j++){
if(chs[i]==input[j]){//字符对
result[1]++;//字符对个数增1
if(i==j){//位置对
result[0]++;//位置对个数增1
}
break;
}
}
}
returnresult;
}
}
C. java猜字游戏课程设计报告
你给多少分?
D. 用JAVA语言编写一个“猜数字游戏”的程序
int num = (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);
int guessNum = -1;
while (guessNum != num) {
System.out.println("请输入1-100之间整数");
guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} elseif (guessNum < num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
(4)猜字游戏java课程设计扩展阅读:
编写思路
1、成1-100之间随机数
(int)(Math.random()*100)+1;
提示用户输入数字,
Scannersc=newScanner(System.in);
intguessNum= sc.nextInt();
需要将随机数和用户输入的数字进行比较。
猜一次:
Scanner sc = new Scanner(System.in);
int num = (int)(Math.random()*100)+1;
System.out.println("请输入0-100之间整数");
int guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
}elseif(guessNum < num) {
System.out.println("小啦");
}else{
System.out.println("大了");
}
二、使用while循环
publicstaticvoid main(String[] args) {
int num = (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入1-100之间整数");
int guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} elseif (guessNum < num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
}
三、最后用while() 括号中的条件表达式,当用户猜测的数和系统生成的数字不相等时,就需要继续循环。
E. 急求.......用java编写一个猜字游戏 要代码
随机数函数用来生成数。
条件判断方法直接放到按钮事件里就可以了
F. 用java怎么编写猜字游戏
用随机函数再加上一点逻辑应该可以实现,至于存储,可以用Reader..不懂的慢慢查api吧...
G. 用java编写一个猜字游戏程序,答案不限。急!!!
publicstaticvoidmain(String[]args){
Randomr=newRandom();
Scannersc=newScanner(System.in);
//100以内
intnum=r.nextInt(100);
System.out.println(num);
intgsNum=-1;
do{
System.out.println("请输入要猜的数字:");
gsNum=sc.nextInt();
if(num>gsNum){
System.out.println("太小啦");
}elseif(num<gsNum){
System.out.println("太大啦");
}
}while(num!=gsNum);
System.out.println("正确");
}
H. java课程设计——猜数字游戏
代码如下:
importjava.util.Scanner;
publicclassRandomT{
intsum=0;
publicstaticvoidmain(String[]args){
intnumber=(int)(Math.random()*100+1);
inttemp=number;
System.out.println("请猜一个100以内的数:");
RandomTrt=newRandomT();
intin=rt.Sn();
Booleanyn=rt.Compare(temp,in);
while(!yn){
System.out.println("请重新输入:");
intin1=rt.Sn();
yn=rt.Compare(temp,in1);
}
System.out.println("正确数字是:"+number);
}
publicBooleanCompare(inttemp,intnumber){
Booleanyesno=null;
if(temp==number){
if(sum==1){
System.out.println("Beautiful");
}elseif(2<=sum||sum<=4){
System.out.println("还是不错的!");
}else{
System.out.println("唉,总算对了!");
}
yesno=true;
}elseif(number<temp){
System.out.println("猜小了");
yesno=false;
}elseif(number>temp){
System.out.println("猜大了");
yesno=false;
}
returnyesno;
}
publicintSn(){
Scannersc=newScanner(System.in);
intin=sc.nextInt();
sum+=1;
returnin;
}
}
如果满意请采纳!
I. 用java编写一个猜字游戏程序,答案不限
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int guessTime = 8;
boolean restart = true;
while (restart) {
System.out.println("请选择下列选项!");
System.out.println("1 ----开始猜数");
System.out.println("2 ----游戏参数设置");
System.out.println("9 ----退出");
boolean flag = true;
String choose = null;
Scanner in = null;
while (flag) {
in = new Scanner(System.in);
choose = in.next();
if (!choose.matches("[129]")) {
System.out.println("输入错误,请重新输入");
continue;
} else {
break;
}
}
if ("2".equals(choose)) {
boolean chooseLevel = false;
System.out.println("请选择游戏难度!");
System.out.println("1 ----难(4次)");
System.out.println("2 ----一般(6次)");
System.out.println("3 ----容易(8次)");
while (!chooseLevel) {
String s = in.next();
if (s.matches("[123]")) {
switch (Integer.parseInt(s)) {
case 1:
guessTime = 4;
chooseLevel = true;
break;
case 2:
guessTime = 6;
chooseLevel = true;
break;
case 3:
guessTime = 8;
chooseLevel = true;
break;
default:
System.out.println("你输入的选择不存在(请输入1、2、3)");
break;
}
} else {
System.out.println("你输入的选择不存在(请输入1、2、3)");
}
}
continue;
}
if ("9".equals(choose)) {
System.exit(0);
}
if ("1".equals(choose)) {
String number = String.valueOf(Math.round(Math.random() * 100) + 1);
System.out.println(number);
boolean getIt = false;
int count = 0;
while (!getIt && ++count <= guessTime) {
System.out.println("请输入你猜的值(1-100)");
String s = in.next();
if (s.matches("\\d+")) {
try {
if (Integer.parseInt(s) == Integer.parseInt(number)) {
getIt = true;
break;
} else if (Integer.parseInt(s) > Integer.parseInt(number)) {
System.out.println("你输入的数字大了!");
} else {
System.out.println("你输入的数字小了!");
}
} catch (NumberFormatException e) {
System.out.println("你输入的数据超过Integer的最大范围!");
continue;
}
} else {
System.out.println("你输入的数据不合法!");
continue;
}
}
if (getIt) {
System.out.println("恭喜你猜对了,你的战斗力是" + Math.round((1 - count * 1.0 / guessTime) * 100) + "%");
} else {
System.out.println("超过次数,尚需努力");
}
in.nextLine();
in.nextLine();
}
}
}
}
J. 用java编写猜字游戏
关键是给提示,
首先你给定一个值,
逻辑是输入的值,如果大了,提示猜大了;
如果输入的值是小了就提示猜小了,