本文目录一览:
- 1、要编一个猜数字的程序 猜10次没猜到就失败 怎么编
- 2、求JAVA计算器和猜数的程序代码
- 3、js数组:有一个4个长度的数组,每个位置装有0-9之间的随机数字,有10次输入的机会来猜每
- 4、用JavaScript写猜数字游戏?
- 5、设计一个猜数游戏,电脑从0-9之间随机产生四个数组成一个四位整数,由玩家猜测,共有十次机会,求源代码
要编一个猜数字的程序 猜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; } } } }}