當前位置:首頁 » 網頁前端 » 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程序第二次被運行,那麼這種翻譯不需要進行第二次,速度明顯加快。