您的位置:

猜数字游戏有10次机会代码js(猜数字游戏的代码)

猜数字游戏有10次机会代码js(猜数字游戏的代码)

更新:

本文目录一览:

要编一个猜数字的程序 猜10次没猜到就失败 怎么编

Java代码:

public class Game{

public static void main(String[] args){

Game g = new Game();

g.gameTest(3);-----------3的位置输入你想让别人猜得数字

}

public void gameTest(int num){

for(int count = 0 ; count 10 ; count++){

Scanner scan = new Scanner(System.in);

int in = scan.nextInt();

if(in == num){

System.out.println("恭喜答对了!");

break;

}

else if(count9){

System.out.println("对不起,答错了!您还有"+(9-count)+"次机会");

continue;

}

else if(count == 9){

System.out.print("游戏失败!");

break;

}

}

}

}

求JAVA计算器和猜数的程序代码

计算器代码:package a;import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class SunnyCalculator implements ActionListener {

JFrame f;

JTextField tResult;

JButton bNumber;

JButton bOperator;

JButton bOther;

JButton bM;

boolean isDouble = false;

int opFlag = -1;

static double t1 = 0, t2 = 0, t3 = 0, result = 0;

static int opflag1 = -1, opflag2 = -1, flag = 0, resflag = 1;

int preOp, currentOp = 0;

double op1 = 0, op2 = 0;

double n3;

StringBuffer buf = new StringBuffer(20);

StringBuffer str = new StringBuffer();

public SunnyCalculator() {

f = new JFrame("计算器");

Container contentPane = f.getContentPane();

JMenuBar mBar = new JMenuBar();

mBar.setOpaque(true);

f.setJMenuBar(mBar);

contentPane.setLayout(new BorderLayout());

JPanel pTop = new JPanel();

tResult = new JTextField("0.", 26);

tResult.setHorizontalAlignment(JTextField.RIGHT);

tResult.setEditable(false);

pTop.add(tResult);

contentPane.add(pTop, BorderLayout.NORTH);

JPanel pBottom = new JPanel();

pBottom.setLayout(new BorderLayout());

JPanel pLeft = new JPanel();

pLeft.setLayout(new GridLayout(5, 1, 3, 3));

bM = new JButton(" ");

bM.setEnabled(false);

pLeft.add(bM); pBottom.add(pLeft, BorderLayout.WEST);

JPanel pRight = new JPanel();

pRight.setLayout(new BorderLayout());

JPanel pUp = new JPanel();

pUp.setLayout(new GridLayout(1, 3, 3, 0)); bOther = new JButton("BackSpace");

bOther.addActionListener(this);

bOther.setForeground(Color.red);

bOther.setMargin(new Insets(3, 0, 3, 5));

pUp.add(bOther); bOther = new JButton("CE");

bOther.addActionListener(this);

bOther.setForeground(Color.red);

pUp.add(bOther);

JPanel pDown = new JPanel();

pDown.setLayout(new GridLayout(4, 5, 3, 2));

bNumber = new JButton("7");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bNumber = new JButton("8");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bNumber = new JButton("9");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bOperator = new JButton("/");

bOperator.setForeground(Color.red);

bOperator.addActionListener(this);

bOperator.setMargin(new Insets(3, 0, 3, 0));

pDown.add(bOperator);

bOperator = new JButton("sqrt");

bOperator.addActionListener(this);

bOperator.setForeground(Color.red);

bOperator.setMargin(new Insets(3, 0, 3, 0));

pDown.add(bOperator);

bNumber = new JButton("4");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

bNumber.setHorizontalTextPosition(JButton.LEFT);

pDown.add(bNumber);

bNumber = new JButton("5");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bNumber = new JButton("6");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bOperator = new JButton("*");

bOperator.setForeground(Color.red);

bOperator.addActionListener(this);

bOperator.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bOperator);

bOperator = new JButton("%");

bOperator.setForeground(Color.blue);

bOperator.addActionListener(this);

bOperator.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bOperator);

bNumber = new JButton("1");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bNumber = new JButton("2");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bNumber = new JButton("3");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bOperator = new JButton("-");

bOperator.setForeground(Color.red);

bOperator.addActionListener(this);

bOperator.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bOperator);

bOperator = new JButton("1/x");

bOperator.setForeground(Color.blue);

bOperator.addActionListener(this);

pDown.add(bOperator);

bNumber = new JButton("0");

bNumber.setForeground(Color.blue);

bNumber.addActionListener(this);

bNumber.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bNumber);

bOperator = new JButton("+/-");

bOperator.setForeground(Color.blue);

bOperator.addActionListener(this);

bOperator.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bOperator);

bOperator = new JButton(".");

bOperator.setForeground(Color.blue);

bOperator.addActionListener(this);

bOperator.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bOperator);

bOperator = new JButton("+");

bOperator.setForeground(Color.blue);

bOperator.addActionListener(this);

bOperator.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bOperator);

bOperator = new JButton("=");

bOperator.setForeground(Color.blue);

bOperator.addActionListener(this);

bOperator.setMargin(new Insets(3, 3, 3, 3));

pDown.add(bOperator);

pRight.add(pUp, BorderLayout.NORTH);

pRight.add(pDown, BorderLayout.SOUTH);

pBottom.add(pRight, BorderLayout.EAST);

contentPane.add(pBottom, BorderLayout.SOUTH);

f.setSize(new Dimension(320, 256));

f.setResizable(false);

f.setVisible(true);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e) {

String s = e.getActionCommand();

if (s.equals("CE")) {

tResult.setText("0.");

} else if (s.equals("BackSpace")) {

if (!tResult.getText().trim().equals("0.")) {

if (str.length() != 1 str.length() != 0) {

tResult.setText(str.delete(str.length() - 1, str.length())

.toString());

} else {

tResult.setText("0.");

str.setLength(0);

}

}

op2 = Double.parseDouble(tResult.getText().trim());

} else if (s.equals("1/x")) {

String temp = tResult.getText().trim();

double dtemp = Double.parseDouble(temp);

tResult.setText("" + 1 / dtemp);

} else if (s.equals("sqrt")) {

String temp = tResult.getText().trim();

double dtemp = Double.parseDouble(temp);

tResult.setText("" + Math.sqrt(dtemp));

} else if (s.equals("+")) {

str.setLength(0);

if (currentOp == 0) {

preOp = currentOp = 1;

op2 = 0;

tResult.setText("" + op1);

} else {

currentOp = preOp;

preOp = 1;

switch (currentOp) {

case 1:

op1 += op2;

tResult.setText("" + op1);

break;

case 2:

op1 -= op2;

tResult.setText("" + op1);

break;

case 3:

op1 *= op2;

tResult.setText("" + op1);

break;

case 4:

op1 /= op2;

tResult.setText("" + op1);

break;

}

}

} else if (s.equals("-")) {

str.setLength(0);

if (currentOp == 0) {

preOp = currentOp = 2;// op1=op2;op2=0;

tResult.setText("" + op1);

} else {

currentOp = preOp;

preOp = 2;

switch (currentOp) {

case 1:

op1 = op1 + op2;

tResult.setText("" + op1);

break;

case 2:

op1 = op1 - op2;

tResult.setText("" + op1);

break;

case 3:

op1 = op1 * op2;

tResult.setText("" + op1);

break;

case 4:

op1 = op1 / op2;

tResult.setText("" + op1);

break;

}

}

} else if (s.equals("*"))// *

{

str.setLength(0);

if (currentOp == 0) {

preOp = currentOp = 3;// op1=op2;op2=1;

tResult.setText("" + op1);// op1=op2;

} else {

currentOp = preOp;

preOp = 3;

switch (currentOp) {

case 1:

op1 = op1 + op2;

tResult.setText("" + op1);

break;

case 2:

op1 = op1 - op2;

tResult.setText("" + op1);

break;

case 3:

op1 = op1 * op2;

tResult.setText("" + op1);

break;

case 4:

op1 = op1 / op2;

tResult.setText("" + op1);

break;

}

}

} else if (s.equals("/"))

{

str.setLength(0);

if (currentOp == 0) {

preOp = currentOp = 4;// op2=1;

tResult.setText("" + op1);// op1=op2;

} else {

currentOp = preOp;

preOp = 4;

switch (currentOp) {

case 1:

op1 = op1 + op2;

tResult.setText("" + op1);

break;

case 2:

op1 = op1 - op2;

tResult.setText("" + op1);

break;

case 3:

op1 = op1 * op2;

tResult.setText("" + op1);

break;

case 4:

op1 = op1 / op2;

tResult.setText("" + op1);

break;

}

}

} else if (s.equals("="))// =

{

if (currentOp == 0) {

str.setLength(0);

tResult.setText("" + op2);

} else {

str.setLength(0);

currentOp = preOp;

switch (currentOp) {

case 1:

op1 = op1 + op2;

tResult.setText("" + op1);

break;

case 2:

op1 = op1 - op2;

tResult.setText("" + op1);

break;

case 3:

op1 = op1 * op2;

tResult.setText("" + op1);

break;

case 4:

op1 = op1 / op2;

tResult.setText("" + op1);

break;

}

currentOp = 0;

op2 = 0;

}

} else if (s.equals(".")) {

isDouble = true;

if (tResult.getText().trim().indexOf('.') != -1)

;

else {

if (tResult.getText().trim().equals("0")) {

str.setLength(0);

tResult.setText((str.append("0" + s)).toString());

}

else {

tResult.setText((str.append(s)).toString());

}

}

} else if (s.equals("0"))

{

if (tResult.getText().trim().equals("0.")) {

} else {

tResult.setText(str.append(s).toString());

op2 = Double.parseDouble(tResult.getText().trim());

}

} else {

tResult.setText(str.append(s).toString());

op2 = Double.parseDouble(tResult.getText().trim());

if (currentOp == 0)

op1 = op2;

}

} public static void main(String[] args) {

new SunnyCalculator();

}

}

js数组:有一个4个长度的数组,每个位置装有0-9之间的随机数字,有10次输入的机会来猜每

function gameStart() {   

    var randomArray = getRandomArray();

    var chance = 10;

    while (chance--) {  

        var num = prompt("请输入一个四位数:");

        var result = judge(num, randomArray)

        if (result.status == true) {  

            alert("正确!游戏结束!");

            break;

        } else {    

            alert("错误!(正确" + result.correct + "个,错误" + result.wrong + "个)\n你还有" + chance + "次机会!");

        }

    }

}

function getRandomArray() { 

    var arr = []

    for (var i = 0 ; i  4 ; i++) { 

        arr.push(parseInt(Math.random() * 10));

    }

    // console.log(arr);

    return arr;

}

function judge(num, randomArray) {  

    var correct = 0;

    var wrong = 0;

    for (var i = 0 ; i  4 ; i++) { 

        if (parseInt(num[i]) == randomArray[i]) {   

            correct++;

        } else {    

            wrong++;

        }

    }

    if (wrong == 0) {   

        return {status: true};

    } else {    

        return {status: false, correct: correct, wrong: wrong};

    }

}

用JavaScript写猜数字游戏?

document.write("input type='text' id='inval' name='inval'/"); //数字框

document.write("input type='button' onclick='Guess();' value='提交'"); //按钮

function Guess() {

var inputVal=document.getElementById("inval").value; //获取文本框的值

var curRand=parseInt(100*Math.random()); //获取随机数

if(!isNum(inputVal))

{

alert("不是数字,请重新输入");

return;

}

if(inputVal){

if(inputValcurRand){

alert("胜利");

} else if (inputValcurRand){

alert("输了");

} else {

alert("平局");

}

}

}

//判断是否是数字,true表示是数字

function isNum(value) {

return !isNaN(value);

}

设计一个猜数游戏,电脑从0-9之间随机产生四个数组成一个四位整数,由玩家猜测,共有十次机会,求源代码

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; } } } }}

猜数字游戏有10次机会代码js(猜数字游戏的代码)

本文目录一览: 1、要编一个猜数字的程序 猜10次没猜到就失败 怎么编 2、求JAVA计算器和猜数的程序代码 3、js数组:有一个4个长度的数组,每个位置装有0-9之间的随机数字,有10次输入的机会来

2023-12-08
java猜数字游戏,JAVA猜数字游戏实验报告

2023-01-09
java猜数字,java猜数字游戏实验报告

2023-01-09
python猜数字游戏脚本(python怎么做猜数字游戏)

2022-11-15
python猜成语游戏代码,python猜谜游戏

2022-11-20
java猜拳,java猜拳小游戏

2023-01-08
php实现简单的猜数字游戏(编写一个猜数字的小游戏)

2022-11-10
java猜拳游戏,java猜拳游戏编程代码简单刘备

2022-12-02
php猜单词游戏,玩猜字游戏英文单词

2022-11-29
java小游戏,java小游戏编程源代码连连看

2022-11-17
java小游戏,java小游戏代码简单

2023-01-09
java游戏代码(java游戏代码分享)

2022-11-12
jsp编程猜数(用jsp编写猜数字游戏)

本文目录一览: 1、用JSP编写一个猜数游戏程序 2、应用jsp编译猜数字游戏 1到100内 使用者想数字由电脑来猜 使用者告诉电 3、jsp中写猜数字的 用JSP编写一个猜数游戏程序 在一个页面上帮

2023-12-08
java小游戏代码,java小游戏代码大全可复制免费

2023-01-10
Python猜词游戏

Python猜词游戏是一种有趣的游戏,通过编写代码实现了猜词的功能。下面将从多个方面介绍Python猜词游戏的实现。 一、游戏规则 1、游戏开始时,程序会随机选择一个单词作为答案。 2、玩家需要根据程

2023-12-08
Python 程序:猜字游戏

2022-07-24
c语言电脑猜数,猜数字游戏c语言编程

2022-11-24
Python小游戏代码简单

2023-05-16
c语言输出猜拳,猜拳游戏c程序设计

2023-01-06
c语言设计游戏代码,c语言2048游戏代码

2022-11-22