java课程设计程序-猜数游戏 用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 produced 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 produced 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;
}
}

1+1=?

java课程设计——猜数字游戏~

代码如下:
import java.util.Scanner;public class RandomT {int sum=0;public static void main(String[] args) {int number=(int)(Math.random()*100+1);int temp=number;System.out.println("请猜一个100以内的数:");RandomT rt=new RandomT();int in = rt.Sn();Boolean yn = rt.Compare(temp, in);while(!yn){System.out.println("请重新输入:");int in1 = rt.Sn();yn = rt.Compare(temp, in1);}System.out.println("正确数字是:"+number);}public Boolean Compare(int temp,int number){Boolean yesno = null;if(temp==number){if(sum==1){System.out.println("Beautiful");}else if(2temp){System.out.println("猜大了");yesno=false;}return yesno;}public int Sn(){Scanner sc = new Scanner(System.in);int in=sc.nextInt();sum+=1;return in;}}如果满意请采纳!

刚写的
import java.util.Scanner;public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);double dNum = 100 * Math.random();int iNum = (int) dNum, inNum, count = 0;while(true) {inNum = sc.nextInt();if(inNum == iNum) {System.out.print("猜对了.
");break;} else if(++count == 10) {System.out.print("猜测次数已达10次,猜数失败.
");break;} else if(inNum iNum) {System.out.print("输入的值大,请输入更小的数.");}}sc.close();}}

c语言编程:猜数字游戏?
答:include include <stdio.h> include <stdlib.h> define UI unsigned short int void game(){ UI answer;UI input;UI lower=1;UI upper=100;UI count=0;srand(time(NULL));do{answer=rand()%101;} while(answer==0);puts("Welcome to the number guessing game!");do { puts("Please ...

C语言程序设计:用电脑设计一个猜数游戏,电脑从1-100中随机选一个数让...
答:曾经帮别人答过,代码如下:include <stdio.h>#include <stdlib.h>#include int main(){ int nAppValue, nUserValue, iCount = 0; srand( time( 0 ) ); nAppValue = rand() % 100 + 1; printf( "请猜我选中的是哪个数字(1-100)?\n" ); do { scanf( "%d...

编写一个猜数字的程序,当客户访问服务器上的Guess.jsp时,随机分配给...
答:很简单。在JSP中是可以嵌套JAVA代码的,如 < Random r = new Random();int answer= r.nextInt(100);request.getSession.setAttribute("answer",answer);> 保存随机数到session,判断,request.getParameter("guessNumber").equals(request.getSession.getAttribute("answer"))根据结果跳转到不同的页面,...

C语言设计一个简单的猜数游戏:先由计算机“想”一个数请用户猜,如果用...
答://希望我的回答对你的学习有帮助#include <stdio.h>#include <stdlib.h>#include int main(){int x, magic, a = 0, c = 1;char b;do{srand(unsigned(time(NULL)));//为函数rand()设置随机数种子magic = rand() % 100 + 1;//magic为1~100间的一个数printf("您每次游戏中,只有十...

c语言编写一个猜数字游戏
答:老师要求我们用c语言编写一个程序,是一个猜数字的游戏,随机出一个数字,然后猜,可以无限制的往下猜,如果你所猜数大于随机数,会提示你too high,相反,会提示你too low。如果你猜对,游戏结束,会显示你猜了多少次,然后“1”继续玩,“0”退出 小弟刚... 展开 sunshine...

悬赏,C语言猜数字游戏程序编写
答:int total_guesses=0,best=0,now=0,num_of_game=0;srand(time(NULL));do { ++num_of_game; //统计玩游戏的盘数 total_guesses+=now=game(); //统计总共猜数字的次数 if (!best) best=now; //统计最佳成绩 else if (now<best) best=now;printf("Play again (y or n)?")...

猜数字C语言
答:其实这个程序很简单,思路很清晰 首先产生一个随机的四位数,且每位都不相同 int a[10]={0,1,2,3,4,5,6,7,8,9};int b,c,i,t;for(i=0;i<100;i++) //思路就是产生两个10以内的随机数,交换a中这两个下标的数值,{ //经过多次交换后,着a中的这10个数就打乱了次序,就像...

C语言 猜数字游戏
答:include<stdio.h>#include<stdlib.h>void main(){srand(time(0));//指定随机数种子int result = rand()%1000+1;//产生1~1000的随机数int guess;//储存猜测的数字 //循环提示输入数字,直到猜测结果正确 do{printf("请输入猜测的数字\n");scanf("%d", &guess);if(guess < result){printf...

编程先由计算机“想”一个1~100之间的数请人猜,如果人猜对了,则计算机...
答:仅提供思路,自己实现比拿源码照抄好;1,设置随机数种子,一般使用当前时间, srand;2,产生随机数,rand,将产生的随机数调整到1~100之间;3,此处开始while循环;4,让人开始猜数,接收输入,判断大小,显示对应提示,如果大小相同,退出循环;end ...

c语言编程 编一个猜数字游戏
答:源码如下:/* File: guess.c */ include <stdio.h> /* standard input & output support */ include <stdlib.h> /* srand() rand() */ include /* time() */ /* 宏定义 */ define NUMBER_LENGTH 5 /* 随机数长度 */ define NUMBER_LIMIT 10 /* 随机数限制, 每...