导航:首页 > 编程语言 > 猜字游戏java

猜字游戏java

发布时间:2022-08-21 21:57:31

A. 如何用java语言实现猜数字游戏

java实现的简单猜数字游戏代码,通过随机数与逻辑判断来实现游戏功能 代码如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { // 产生一个随机数 int n

B. Java猜数字游戏

import java.util.Random;
import java.util.Scanner;

/*
* 游戏随即给出一个0~99(包括0和99)的数字,然后让你猜是什么数字。你可以随便猜一个数字,游戏会提示太大还是太小,从而缩小结果范围。经过几次猜测与提示后,最终退出答案。在游戏过程中。记录你最终猜对时所需要的次数。游戏结束后公布结果。见下
次数 结果
1 你太有才了!
2~6 这么快就猜出来了,很聪明么!
大于7 猜了半天才猜出来,小同志,尚需努力啊!
*/
public class guessGame {

/**
* @param args
*/
public static void main(String[] args) {
int gameValue = (int)(Math.random()*(100-1)+1);
System.out.println("Rand:"+gameValue);
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字");
int num = sc.nextInt();
int guessCorrectNum=1;

while(true){

if(num==gameValue){
if(guessCorrectNum == 1)
System.out.println("你太有才了!");
else if((guessCorrectNum >=2) && (guessCorrectNum<=6))
System.out.println("这么快就猜出来了,很聪明么");
else if(guessCorrectNum >7)
System.out.println("猜了半天才猜出来,小同志,尚需努力啊!");

break;
}
else{
if (guessCorrectNum <=20){

guessCorrectNum = guessCorrectNum + 1;
num = sc.nextInt();

}
else{
System.out.println("20次都猜不出来...,不让你猜了");
break;
}

}

}
}

}

C. java猜数字游戏

import java.util.Random;

import java.util.Scanner;

/**

* @Author: Cool_Wu

* @Date: 2020-12-01 23:39

*/

public class GuessNumberGame {

static int count = 0;

static int answer = new Random().nextInt(100);

public static void main(String[] args) throws Exception {

System.out.println("猜数字游戏开始,该数字是一个0~100之间的整数");

compareNum();

}

public static void compareNum() throws Exception {

if (count >= 10){

System.out.println("正确答案是:" + answer);

System.out.println("你太笨了,下次再来吧!");

return;

}

count++;

int n = receiveNum();

if (n < 0){

throw new Exception("您输入的数字不符合要求,请重新输入!");

}

if (n > 99){

throw new Exception("输入错误,请输入正确的数字!");

}

if (n < answer){

System.out.println("太小了,再大一点!");

compareNum();

}

if (n > answer){

System.out.println("太大了,再小一点!");

compareNum();

}

if (n == answer){

System.out.println("恭喜你,猜对了!");

}

}

public static int receiveNum() {

System.out.println("请输入您猜的数字:");

int n = new Scanner(System.in).nextInt();

return n;

}

}


运行结果

D. java猜字程序代码怎么写

import java.util.Scanner;
package Ly;
public class Yanshi {
public static void main(String[] args) {
char[] c={8, 4, 2, 1, 23, 344, 12};
Scanner input=new Scanner(System.in);
System.out.print("请输入一个整数: ");
int k= input.nextInt();
int f=0;
for(int i=0;i<=6;i++)
{
if(k==c[i])
{
f=1;
System.out.print("有此数!");}
}
if(f==0)
System.out.print("无此数!");

}
}

E. 用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是否是EXIT
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;
}
}

F. 用Java编写一个猜数字的游戏

新手炒沥青初期需要投入多少资金?
我这边是3W,最低没有门槛,你就是入两千都可以玩,但问题是两千只能操作1手,而且没有抗风险资金了,你买了之后只要点位瞬间波动一个点你就直接平仓了,没法玩的,所以我都是入金五万以上玩的。

G. 用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("大了");

}

}

(7)猜字游戏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() 括号中的条件表达式,当用户猜测的数和系统生成的数字不相等时,就需要继续循环。

H. 用JAVA编猜数字游戏

1)
程序随机分配给客户一个1—100之间的整数
Random
gen
=
new
Random();
int
a
=
gen.nextInt(100)+1;
//不加1是0到99
2)
用户在输入对话框中输入自己的猜测
Scanner
in
=
new
Scanner(System.in);
System.out.println("give
me
a
number")
int
b
=
in.nextInt();
3)
程序返回提示信息,提示信息分别是:“猜大了”、“猜小了”和“猜对了”。
if(b>a)
System.out.println("big");
else
if(b<a)
System.out.println("small");
else
System.out.println("right");
4)
用户可根据提示信息再次输入猜测,直到提示信息是“猜对了”。
while(b!=a){
if(b>a)
System.out.println("big");
else
if(b<a)
System.out.println("small");
System.out.println("give
me
a
new
number")
int
b
=
in.nextInt();
}
System.out.println("right");
3看懂以后,3,4可以合在一起,用4的回答.

I. 用java来写一个猜数字游戏,要用到界面

packagep1;

importjava.awt.Color;
importjava.awt.Container;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.BoxLayout;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.WindowConstants;


{
=1L;
intrandom=0;

publicstaticvoidmain(String[]args)
{
newMyFrame();
}

publicMyFrame()
{
Containercontainer=getContentPane();
container.setLayout(newBoxLayout(container,BoxLayout.Y_AXIS|BoxLayout.LINE_AXIS));
container.setBackground(Color.pink);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
setResizable(false);
setTitle("输入1到10的数字猜");
JPanelp=newJPanel();
finalJTextFieldjtf=newJTextField(20);
jtf.setToolTipText("输入1到10的数字猜");
p.add(jtf);
container.add(p);
JPanelpanel=newJPanel();
finalJButtonbl=newJButton("开始");
bl.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
random=(int)(Math.random()*10+1);
bl.setEnabled(false);
}
});
panel.add(bl);
JButtonbl1=newJButton("确定");
bl1.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
Stringnum=jtf.getText().trim();
if(!num.matches("^[1-9]|10$"))
{
JOptionPane.showConfirmDialog(null,"请填入一个1~10的整数!","友情提示",JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE);
}
else
{
if(random==Integer.parseInt(num))
{
JOptionPane.showConfirmDialog(null,"恭喜你,猜对了!","友情提示",JOptionPane.CLOSED_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showConfirmDialog(null,"很遗憾,你猜错了!随机数是:"+random,"友情提示",
JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
}
}
jtf.requestFocus();
bl.setEnabled(true);
}
});
panel.add(bl1);
JButtonbl2=newJButton("退出");
bl2.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
System.exit(0);
}
});
panel.add(bl2);
container.add(panel);
JLabeljl=newJLabel();
container.add(jl);
setVisible(true);
}
}

阅读全文

与猜字游戏java相关的资料

热点内容
爱上北斗星男友在哪个app上看 浏览:413
主力散户派发源码 浏览:663
linux如何修复服务器时间 浏览:55
荣县优途网约车app叫什么 浏览:472
百姓网app截图是什么意思 浏览:222
php如何嵌入html 浏览:809
解压专家怎么传输 浏览:743
如何共享服务器的网络连接 浏览:132
程序员简易表白代码 浏览:166
什么是无线加密狗 浏览:62
国家反诈中心app为什么会弹出 浏览:67
cad压缩图打印 浏览:102
网页打开速度与服务器有什么关系 浏览:863
android开发技术文档 浏览:65
32单片机写程序 浏览:51
三星双清无命令 浏览:838
汉寿小程序源码 浏览:344
易助erp云服务器 浏览:533
修改本地账户管理员文件夹 浏览:419
python爬虫工程师招聘 浏览:285