用C语言编写一个“猜数字游戏”的程序

用C语言编写一个“猜数字游戏”的程序,第1张

#include<stdio.h>

#include<stdlib.h>

void main()

{

int Win = rand() % 90 + 10 //随机赋值

int i = 0

int n

char ch

printf("please input n\n")

scanf("%d",&n)

while(1)

{

if(n >Win) //猜的数字大了

{

printf("you guess number big\n")

i++

scanf("%d",&n)

}

if(n <Win) //猜的数字小了

{

i++

printf("you guess number small\n")

scanf("%d",&n)

}

if(n == Win)// 正确

{

i++

printf("you guees is right\n")

printf("你现在想退出吗? 输入a结束\n")

scanf("%c",&ch)

if(ch == 'a')

break

continue

}

if(i >10) //机会用完了

{

printf("你的机会已经用完,没机会再猜了\n")

break

}

}

if(i <3)

printf("太棒了\n") // 对游戏者的评价

else if(i <5)

printf("非常好\n")

else if(i <8)

printf("很好\n")

else if(i <10)

printf("不错\n")

else

printf("失败\n")

}

我自己编写的一个小程序,没有用到什么高深的技术,都是些基础的,希望对你有帮助

public class GuessNum {

public static void main(String[] args) {

GuessNum.play()

}

static Scanner sc = new Scanner(System.in)

static int guessCount = 0

// 生成不重复的随机数

public static String[] generationNum(int count){

String [] sNum = new String[count]

int n = 10

Random ran = new Random()

boolean[] bool = new boolean[n]

int num = 0

for(int i=0i<counti++){

do{

num = ran.nextInt(n)

}while(bool[num])

bool[num] = true

sNum[i] = String.valueOf(num)

}

return sNum

}

// 获取猜测结果

public static String getResult(String[] num, String inputNum){

String resultMessage = ""

int rightInAll = 0

int rightInNum = 0

int numLength = inputNum.length()

// 根据输入的数与生成的数作比较(这里判断输入数有生成的数,且位置相同)

for(int i=0i<num.lengthi++){

// 判断输入数与生成数各位数是否相同

if(num[(num.length-1) - i].equals(inputNum.substring(numLength - 1, numLength))){

rightInAll++

}

numLength--

}

// 这里判断输入数有生成的数,但位置不同

for(int j=0j<num.lengthj++){

if(inputNum.indexOf(num[j]) >-1){

rightInNum++

}

}

resultMessage = rightInAll + "A" + (rightInNum - rightInAll) + "B"

return resultMessage

}

public static void startGame(int count){

boolean continueOrNot = false

String resultNum = ""

String [] num = GuessNum.generationNum(count)

for(int i=0i<num.lengthi++){

resultNum += num[i]

}

do{

System.out.print("input:")

boolean maxLengthError = false

String inputNum = sc.next()

if(inputNum.length() >count){

System.out.println("The max length is " + count)

maxLengthError = true

}

if(inputNum.length() != count){

System.out.println("Enter the " + count + " digit.")

maxLengthError = true

}

try{

Integer.valueOf(inputNum)

String message = GuessNum.getResult(num, inputNum)

System.out.println("Guess:" + inputNum + "\tResult:" + message)

guessCount++

if(guessCount >10){

break

}

continueOrNot = winOrNot(message,resultNum,count)

}catch(Exception e){

String message = ""

if(maxLengthError){

message += "and "

}

System.out.println(message + "It is not Number.")

System.out.println("Input again.")

}

}while(continueOrNot == false)

}

public static boolean winOrNot(String message,String resultNum,int count){

if((count + "A0B").equals(message)){

System.out.println("ResultNum:" + resultNum + "\tguessCount:" + guessCount)

System.out.println("You Win.")

return true

}

return false

}

public static void play(){

boolean again = false

int count = 4

do{

GuessNum.startGame(count)

System.out.println("continue?Enter y to continue.")

if("y".equals(sc.next().toLowerCase())){

again = true

}

}while(again)

System.out.println("Game Over.")

}

}

按这这个步骤走,里面自动生成的代码我有改过,

这样自己比较好去认识.

import java.awt.*

import javax.swing.*

import com.borland.jbcl.layout.XYLayout

import com.borland.jbcl.layout.*

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

public class Frame1

extends JFrame {

JPanel contentPane

XYLayout xYLayout1 = new XYLayout()

JTextField txtNum = new JTextField()

JLabel jLabel1 = new JLabel()

JButton btnSC = new JButton()

JButton btnC = new JButton()

int num//存放生成的数

int count//存放猜了几次

JPasswordField txtPw = new JPasswordField()

public Frame1() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE)

jbInit()

cleanTxt()

this.txtNum.setEnabled(false)

}

catch (Exception exception) {

exception.printStackTrace()

}

}

/**

* Component initialization.

*

* @throws java.lang.Exception

*/

private void jbInit() throws Exception {

contentPane = (JPanel) getContentPane()

contentPane.setLayout(xYLayout1)

setSize(new Dimension(400, 300))

setTitle("Frame Title")

txtNum.setText("jTextField1")

jLabel1.setText("Input:")

btnSC.setToolTipText("")

btnSC.setText("生成一个数")

btnSC.addActionListener(new Frame1_btnSC_actionAdapter(this))

btnC.setEnabled(false)

btnC.setText("猜")

btnC.addActionListener(new Frame1_btnC_actionAdapter(this))

txtPw.setText("jPasswordField1")

contentPane.add(txtNum, new XYConstraints(139, 72, 121, 29))

contentPane.add(btnSC, new XYConstraints(211, 183, 100, 35))

contentPane.add(btnC, new XYConstraints(94, 183, 100, 35))

contentPane.add(jLabel1, new XYConstraints(77, 79, 83, 24))

contentPane.add(txtPw, new XYConstraints(141, 126, 120, -1))

}

public void btnSC_actionPerformed(ActionEvent e) {

this.count=0

this.txtNum.setEnabled(true)

this.num=(int)(Math.random()*100)

this.btnSC.setEnabled(false)

this.btnC.setEnabled(true)

JOptionPane.showMessageDialog(this,"已经生成的随机数,可以开始猜了!!")

}

private void cleanTxt(){

this.txtNum.setText("")

this.txtNum.setText("")

this.txtNum.requestFocus()

}

public void btnC_actionPerformed(ActionEvent e) {

//JOptionPane.showMessageDialog(this,this.txtPw.getText())

String pw=String.valueOf( this.txtPw.getPassword() )

JOptionPane.showMessageDialog(this,pw)

try {

int cai = Integer.parseInt(this.txtNum.getText())

if(cai>this.num){

JOptionPane.showMessageDialog(this,"大了")

}else if(cai<this.num){

JOptionPane.showMessageDialog(this,"小了")

}else{

JOptionPane.showMessageDialog(this,"恭喜你猜对了!!"+this.count)

this.btnC.setEnabled(false)

this.btnSC.setEnabled(true)

return

}

count++

cleanTxt()

}

catch (NumberFormatException ex) {

JOptionPane.showMessageDialog(this,"输入的不是数字,请重新输入")

cleanTxt()

}

}

}

class Frame1_btnC_actionAdapter

implements ActionListener {

private Frame1 adaptee

Frame1_btnC_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee

}

public void actionPerformed(ActionEvent e) {

adaptee.btnC_actionPerformed(e)

}

}

class Frame1_btnSC_actionAdapter

implements ActionListener {

private Frame1 adaptee

Frame1_btnSC_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee

}

public void actionPerformed(ActionEvent e) {

adaptee.btnSC_actionPerformed(e)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11180391.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存