当前位置:首页 » 网页前端 » web计算机猜数游戏代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

web计算机猜数游戏代码

发布时间: 2022-05-25 17:30:30

1. 设计一个猜数游戏,电脑从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; } } } }}

2. JAVAWEB猜数字游戏1到100(这是老师给的思路最好按照老师给的思路来)

public class Test {

public static void main(String[] args) {

int b = (int) (Math.random()*100 + 1);
while (true) {
try {
Scanner in=new Scanner(System.in);
int a=in.nextInt();
if(a < 1 || a > 100) {
System.out.println("范围不合法");
} else if(a > b) {
System.out.println(a + "太大");
} else if (a < b) {
System.out.println(a + "太小");
} else {
System.out.println("你终于猜对了");
break;
}
} catch(Exception e) {
System.out.println("数字格式不合法");
continue;
}
}
}

}

3. 求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();
}
}

4. c语言猜数字游戏源代码

小游戏2048:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<time.h>

#include<windows.h>

int jsk( ); //计算空格数

void rsgm( ); //重置游戏

void inkey( ); //按键输入

void left( ); //向左移动

void right( ); //向右移动

void up( ); //向上移动

void down( ); //向下移动

void show( ); //输出界面

void adnum( ); //添加随机数

void yes( ); //游戏是否结束(1是0否)

void gtxy(int x, int y); //控制光标位置的函数

int a[4][4]; //存储16个格子中的数字

int score = 0; //每局得分

int best = 0; //最高得分

int ifnum; //是否需要添加数字(1是0否)

int over; //游戏结束标志(1是0否)

int i,j,k;

int main( )

{ rsgm( ); //重置游戏

inkey( ); //按键输入

return 0;

}

void Color(int a) //设定字符颜色的函数(a应为1-15)

{ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a); }

void rsgm( ) //重置游戏

{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //启动随机数发生器

int n = rand( ) % 16; //随机函数产生0-15的数字

for (i = 0; i < 4; i++)

{for (j = 0; j < 4; j++)

{ if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; }

else { a[i][j] = 4; } n--; }

else { a[i][j] = 0; n--; }

}

}

adnum( );

system("cls");

CONSOLE_CURSOR_INFO gb={1,0}; //以下两行是隐藏光标的设置,gb代指光标

SetConsoleCursorInfo( GetStdHandle(STD_OUTPUT_HANDLE), &gb );

Color(14); //设置字体淡黄色

printf(" 2048小游戏"); Color(7); //恢复白字黑底

printf(" ┌──────┬──────┬──────┬──────┐");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" └──────┴──────┴──────┴──────┘");

show( );

}

void show( ) //输出界面

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

for(j=0;j<4;j++)

{ gtxy(7*j+9,2*i+4); //gtxy(7*j+9, 2*i+4)是光标到指定位置输出数字

if(a[i][j]==0){printf(" "); Color(7); printf("│");}

else if(a[i][j]<10){ if (a[i][j] == 2) { Color(14); }

else if (a[i][j] == 4) { Color(13); }

else if (a[i][j] == 8) { Color(12); }

printf(" %d ", a[i][j]); Color(7 ); printf("│");

}

else if (a[i][j] < 100){if (a[i][j] == 16) { Color(12); }

else if (a[i][j] == 32) { Color(10); }

else if (a[i][j] == 64) { Color(2 ); }

printf(" %d ", a[i][j]); Color(7); printf("│");

}

else if (a[i][j] < 1000) {if (a[i][j] == 128) { Color(9); }

else if (a[i][j] == 256) { Color(1); }

else if (a[i][j] == 512) { Color(13); }

printf(" %d ", a[i][j]); Color(7); printf("│");

}

else if (a[i][j] < 10000) {if (a[i][j] == 1024) { Color(5); }

else { Color(15); }

printf(" %d ", a[i][j]); Color(7); printf("│");

}

}

if (jsk( ) == 0)

{ yes( ); if (over) { gtxy(9,12); Color(10);

printf(" 游戏结束!是否继续? [ Y/N ]:"); }

}

}

void inkey( ) //按键输入

{ int key;

while (1)

{ key = getch( );

if (over) { if (key == 89|| key == 121) { rsgm( ); continue; }

else if (key == 78|| key == 110) { return; }

else continue; }

ifnum = 0;

if(key==224)key=getch( );

switch (key)

{ case 75: left( ); break;

case 77: right( ); break;

case 72: up( ); break;

case 80: down( );break;

}

if (score > best) { best = score; }

if (ifnum) { adnum( ); show( ); }

}

}

int jsk( ) //计算空格数

{ int n = 0;

for (i = 0; i < 4; i++)

{ for (j = 0; j < 4; j++) { if ( a[i][j] == 0) {n++;} } }

return n;

}

void left( ) //向左移动

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

{for (j = 1, k = 0; j < 4; j++)

{ if (a[i][j] > 0)

{ if ( a[i][k] == a[i][j])

{ a[i][k] *= 2; k++;

score = score + 2 * a[i][j];

a[i][j] = 0; ifnum = 1; }

else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }

else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; }

k++; }

}

}

}

}

void right( ) //向右移动

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

{for (j = 2, k = 3; j >= 0; j--)

{if (a[i][j] > 0)

{ if (a[i][k] == a[i][j])

{a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; }

else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }

else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; }

}

}

}

}

void up( ) //向上移动

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

{for (j = 1, k = 0; j < 4; j++)

{if (a[j][i] > 0)

{if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i];

a[j][i] = 0; ifnum = 1; }

else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }

else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; }

k++; }

}

}

}

}

void down( ) //向下移动

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

{for (j = 2, k = 3; j >= 0; j--)

{if (a[j][i] > 0)

{if (a[k][i] == a[j][i])

{a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; }

else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }

else {a[k - 1][i] = a[j][i];

if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; }

}

}

}

}

void adnum( ) //添加随机数

{ srand(time(0)); int n = rand( ) % jsk( );

for (int i = 0; i < 4; i++)

{for (int j = 0; j < 4; j++)

{ if (a[i][j] == 0) {if (n != 0) { n--; }

else {int k = rand( ) % 3;

if (k == 0 || k == 1) {a[i][j] = 2; return; }

else {a[i][j] = 4; return; } }

}

}

}

}

void yes( ) //游戏是否结束

{ for (int i = 0; i < 4; i++)

{for (int j = 0; j < 3; j++)

{if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; }}

}

over = 1;

}

void gtxy(int x, int y) //控制光标位置的函数

{ COORD zb; //zb代指坐标

zb.X = x;

zb.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), zb);

}

5. 猜数游戏的C语言编译代码

#include
#include
//随机数生成函数srand()与rand()
所需的头文件
#include
//time()所需的头文件
int
main()
{
int
sysdata;//系统生成的数据
int
n;
//所猜的数据
int
sum
=
0;//记录猜的次数
srand((unsigned)time(null));
//随机数发生器初始化函数,以时间为种子
sysdata
=
rand()%100+1;
//随机生成1到100的随机数
printf("退出程序请按:ctrl
+
c!\n");
while(1)
{
printf("请输入你猜的数据(1-100):
");
if(scanf("%d",&n)
!=
1)
//用于判断用户是否结束游戏
{
break;
//跳出while循环,结束游戏
}
sum++;
//每输入一次数据,猜的次数加1
if(sum
==
10)
//
当猜的次数大于10次的时候重新生成新的随机数
{
if(n
==
sysdata)
//最后一次猜数正确,输出结果,结束游戏
{
printf("猜数正确,总共猜了%d次!\n",sum);
break;
//跳出while循环,结束游戏
}
else
//最后一次猜数不正确则重新开始游戏
{
printf("猜数次数超过%d次,重新开始游戏!\n",sum);
sysdata
=
rand()%100+1;
//随机生成1到100的随机数
sum
=
0;
}
}
else
{
if(n
==
sysdata)
{
printf("猜数正确,总共猜了%d次!\n",sum);//猜数正确,结束游戏
break;
//跳出while循环,结束游戏
}
else
if(n
>
sysdata)
{
printf("你猜的数据太大!\n");
}
else
{
printf("你猜的数据太小!\n");
}
}
}
printf("猜数游戏结束!\n");
return
0;
}
测试结果:
退出程序请按:ctrl
+
c!
请输入你猜的数据(1-100):
50
你猜的数据太小!
请输入你猜的数据(1-100):
75
你猜的数据太小!
请输入你猜的数据(1-100):
85
你猜的数据太大!
请输入你猜的数据(1-100):
80
你猜的数据太小!
请输入你猜的数据(1-100):
82
你猜的数据太小!
请输入你猜的数据(1-100):
84
猜数正确,总共猜了6次!
猜数游戏结束!
press
any
key
to
continue

6. 设计一个猜数游戏程序

猜数字游戏

程序设计的逻辑思维
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void Guess(const char src[])
{
int i;
int count = 1;//猜次数的次数
int A_num;//猜中的A的个数
int B_num;//猜中的B的个数
char one, two, three, four;//分别表示我们所输入的四个数字
while(1)
{
A_num = B_num = 0;
fflush(stdin);
printf("请输入你第%d次猜测的数字(4位不重复):", count++);
scanf(" %c %c %c %c", &one, &two, &three, &four);
for(i = 0; i < 4; ++i)
{
if(src[i] == one)
{
if(i == 0)
++A_num;
else
++B_num;
}
else if(src[i] == two)
{
if(i == 1)
else
}
else if(src[i] == three)
{
if(i == 2)
else
}
else if(src[i] == four)
{
if(i == 3)
else
}
}
printf("------------------------------------------ ");
if(A_num == 4)
{
printf("恭喜你,全部猜对了! ");
break;
}
else
{
printf("你猜测的结果是:%dA%dB ", A_num, B_num);
}
}
}
int main()
{
char chNum[4];//用于存放系统产生的随机4位数字
int i, index;
srand((unsigned)time(0));//初始化随机种子
index = 0;
//system("color F0");
//使用do...while循环让系统产生一个随机的4位数字
do
{
chNum[index] = rand() % 10 + '0';
//判断一下这个数字是否和前面产生的字符相同
for(i = 0; i < index; ++i)
{
if(chNum[i] == chNum[index])
--index;
}
++index;
}while(index < 4);
Guess(chNum);
return 0;
}

7. 编写一个猜数的游戏程序。(数字由机器随机产生,限制为1~100之间的整数,用户输入猜测,程序给出大小提示

static void Main(string[] args)
{
string i = null;
do
{
Console.Write("请输入一个整数(范围为1~100)\n如果要退出,请输入0!否则输入1!");
i = Console.ReadLine();
if (i.Trim().Equals("0"))
{
return;
}
} while (!i.Trim().Equals("1"));
start:
Random ra = new Random();
int rndInt = ra.Next(1, 100);
int input = 0;
do
{
Console.Write("输入你猜的数值:");
i = Console.ReadLine();
if (!int.TryParse(i, out input))
{
continue;
}
if (input > rndInt)
{
Console.Write("猜大了\n\n");
}
else if (input < rndInt)
{
Console.Write("猜小了\n\n");
}
} while (input != rndInt);
Console.Write("恭喜你,猜对了!\n\n");
do
{
Console.Write("若继续猜测输入Y,若退出则输入N!\n请输入:");
i = Console.ReadLine();
if (i.Trim().Equals("n", StringComparison.OrdinalIgnoreCase))
{
return;
}
if (i.Trim().Equals("y", StringComparison.OrdinalIgnoreCase))
{
goto start;
}
} while (!i.Trim().Equals("1"));
}

8. 猜数字游戏 C语言简单程序代码

#include
#include
#include
#include
int
i,j=1;
int
scores[6];
void
main()
{
char
control='\0';
int
rand1,guess,score;
printf("开始游戏吗
?(y?n)");
control=getchar();
while(control!='y'&&control!='y'&&control!='n'&&control!='n')//屏蔽其他按键
{
printf("无效字符!开始游戏吗
?(y?n)");
fflush(stdin);
control=getchar();
printf("%c",control);
}
while((control=='y')||(control=='y'))
{
system("cls");
srand((unsigned)time(null));
rand1=rand()%10+1;
//printf("%d",rand1);
for(i=0;i<20;i++)
{
printf("请输入你猜的数:");
scanf("%d",&guess);
if(guess>rand1)printf("大啦!\n");
else
if(guess
:猜对了\a\n");//响铃\a
break;
}
i++;
}
i+=1;
if(i==1)
{
score=100;
scores[0]++;
}
else
if(i>=2&&i<=3)
{
score=90;
scores[1]++;
}
else
if(i>=4&&i<=6)
{
score=80;
scores[2]++;
}
else
if(i>=7&&i<=10)
{
score=70;
scores[3]++;
}
else
if(i>=11&&i<=15)
{
score=60;
scores[4]++;
}
else
{
score=0;
scores[5]++;
}
printf("第%d次得分是:%d\n",j,score);
scores[7]+=score;
j++;
printf("是否继续(y?n)\n");
fflush(stdin);//请输入缓冲区
control=getchar();
while(control!='y'&&control!='y'&&control!='n'&&control!='n')
{
printf("无效字符!只能按y或y,n或n是否继续(y?n)\n");
fflush(stdin);
control=getchar();
printf("%c",control);
}
}
system("cls");//清屏
printf("+++++++++++++++-----------------以下是得分情况:---------------*************\n");
for(i=0;i<5;i++)
{
printf("+++++++++++++++------------------得%d分%d次:------------------*************\n",10*(10-i),scores[i]);
}
printf("+++++++++++++++------------------得%d分%d次:------------------*************\n",
0,scores[5]);
}

9. C++猜数字游戏源代码

#include "stdio.h"
#include "stdlib.h"
void main()
{
int num=rand()%100;
int guess;
int i=0;
printf("Guess a number (1-100):");
while(1)
{
scanf("%d",&guess);
i++;
if(i>=10)
{
printf("Sorry, game over!\n");
break;
}
if(guess==num)
{
printf("You are right, guess %d times.\n",i);
break;
}
else if(guess>num)
printf("Guess too big, try again:");
else
printf("Guess too small, try again:");

}
}

10. C# 窗体应用程序 猜数字游戏 代码

public static void main()
{
console.writeline("请输入一个0~100的数");
random ran = new random();
int y = ran.next(101);
int a = 0;
while (true)
{
a++;
int x = int.parse(console.readline());
if (x &gt; y)
{
console.writeline("你猜的数大了");
}
else if (x &lt; y)
{
console.writeline("你猜的数小了");
}
else
{
console.writeline("你猜对了!");
break;
}
}
console.writeline("你一共猜了{0}次", a);
console.readline();
}
}

(10)web计算机猜数游戏代码扩展阅读:

C#所开发的程序源代码并不是编译成能够直接在操作系统上执行的二进制本地代码。与Java类似,它被编译成为中间代码,然后通过.NETFramework的虚拟机——被称之为通用语言运行库(CLR)——执行。

所有的.Net编程语言都被编译成这种被称为MSIL(Microsoft Intermediate Language )的中间代码。因此虽然最终的程序在表面上仍然与传统意义上的可执行文件都具有“.exe”的后缀名。但是实际上,如果计算机上没有安装.Net Framework,那么这些程序将不能够被执行。

在程序执行时,.Net Framework将中间代码翻译成为二进制机器码,从而使它得到正确的运行。最终的二进制代码被存储在一个缓冲区中。所以一旦程序使用了相同的代码,那么将会调用缓冲区中的版本。这样如果一个.Net程序第二次被运行,那么这种翻译不需要进行第二次,速度明显加快。