c/c++程序之_猜数游戏

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/* 本猜数游戏的规则是:电脑在1--9这9个数字中,
     任意不重复地选择4个数字排列成一个4位数,然后让玩的人猜
     这个数。每当输入一个数,电脑输出判断结果:*A*B  。*A代表
  输入的数有*个数位与答案数数位一致,*B代表输入的数有*个数位在
     答案数中可以找到,但位置不对。
*/
void   answerNumber(int a[])
{
 srand(time(NULL));
 for(int i=0;i<4;++i)
 {
RandA: int aa=rand()%9+1;
    for(int ii=0;ii<=i;++ii)
    {
     if(aa==a[ii])
      goto RandA;
    }
    a[i]=aa;
 }
 
 printf("电脑给出了四位数让你猜:****/n");
}

void guessNumber(int b[])
{
 int bb;
 printf("/n请输入你猜测的四位整数,各数位不相同(0-退出):");
 scanf("%d",&bb);//3698 
 if(0==bb) exit(0);
 b[0]=bb/1000;  //3
 b[1]=(bb%1000)/100;//6
 b[2]=(bb%100)/10;//9
 b[3]=bb%10;//8
}

void compare( int a[],int b[],int &A,int &B)
{
 A=0;
 B=0;
 for(int i=0;i<4;i++)
  for(int jj=0;jj<4;jj++)
  {
   if(a[i]==b[jj])
   {
    if(i==jj)
     ++A;
    else
     ++B;
   }
  }
  printf("%dA%dB",A,B);
}
int main()
{
 while(1)
 {
        int a[4]={0};
     int b[4]={0};
     int A=0,B=0;
  answerNumber(a);
  while(A<4)
  {
   guessNumber(b);
   compare(a,b,A,B);
  }
  printf("/n哇,猜中了耶!/n================/n/n/n");

 }
 return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java猜数游戏的图形界面实现代码,您可以参考一下: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GuessNumberGame extends JFrame implements ActionListener { private JTextField textField; private JTextArea textArea; private int randomNum; private int guessCount; public GuessNumberGame() { super("Guess Number Game"); randomNum = (int) (Math.random() * 100) + 1; // 生成随机数 guessCount = 0; // 设置布局 setLayout(new BorderLayout()); // 添加文本框 JPanel topPanel = new JPanel(new FlowLayout()); topPanel.add(new JLabel("Please guess a number between 1 and 100: ")); textField = new JTextField(10); topPanel.add(textField); add(topPanel, BorderLayout.NORTH); // 添加文本区域 textArea = new JTextArea(10, 30); textArea.setEditable(false); add(new JScrollPane(textArea), BorderLayout.CENTER); // 添加按钮 JPanel buttonPanel = new JPanel(new FlowLayout()); JButton guessButton = new JButton("Guess"); guessButton.addActionListener(this); buttonPanel.add(guessButton); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(this); buttonPanel.add(resetButton); add(buttonPanel, BorderLayout.SOUTH); // 设置窗口属性 setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("Guess")) { // 点击“Guess”按钮 int guessNum = Integer.parseInt(textField.getText()); if (guessNum < 1 || guessNum > 100) { JOptionPane.showMessageDialog(this, "Please enter a number between 1 and 100!"); } else { guessCount++; if (guessNum == randomNum) { // 猜对了 textArea.append("Congratulations! You guessed the number " + randomNum + " in " + guessCount + " guesses!\n"); textField.setEnabled(false); } else if (guessNum < randomNum) { // 猜小了 textArea.append("Your guess " + guessNum + " is too low. Please try again.\n"); } else { // 猜大了 textArea.append("Your guess " + guessNum + " is too high. Please try again.\n"); } textField.setText(""); textField.requestFocus(); } } else { // 点击“Reset”按钮 randomNum = (int) (Math.random() * 100) + 1; // 重新生成随机数 guessCount = 0; textArea.setText(""); textField.setEnabled(true); textField.setText(""); textField.requestFocus(); } } public static void main(String[] args) { new GuessNumberGame(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值