首页 > 试题广场 >

【编程】编写猜数游戏程序,语言不限。给定某个整数,从键盘上反

[问答题]

【编程】编写猜数游戏程序,语言不限。给定某个整数,从键盘上反复输入数据进行猜测。如果未猜中,程序提示输入过大或者过小;如果猜中,则输出猜的次数,最多允许猜10次。


import java.util.*;
public class Guessnum{
    
    public static void main(String[] args){
        
     
            Guessnum h=new Guessnum();
            h.guess();
        }

  public void guess(){
      Random demo= new Random();
        int hico1=demo.nextInt(100);
        int s=hico1;
        int k;
        for(k=0;k<10;k++){
            Scanner t=new Scanner(System.in);
            int i=t.nextInt();
            if(i<s){
            System.out.println("输入过小");
            continue;
                        }
            if (i==s){ 
                System.out.println("猜了"+k+"次");
                break;
            }
            if(i>s){
                System.out.println("输入过大");
                continue;
                }
        }
        }
  }

发表于 2019-10-03 23:58:46 回复(0)
def(a):
    n = 1
    while n<=10:
        b = input('input:')
        if b == a:
            return b
        elif a>b:
            print('输入值小于目标值')
        elif a<b:
            print('输入值大于目标值')
        n += 1

发表于 2020-07-17 09:44:59 回复(0)
n=10 i=0 while(i<10):
    a = int(input('input:'))
    i+=1  if a > n: print('输入过大') elif a < n: print('输入过小') elif a==n: print('猜了')

编辑于 2019-10-19 16:06:38 回复(0)
public class guessFigure { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int answer = (int) (Math.random() * 100) + 1; for(int i=1;i<11;i++) { System.out.println("please input an figure between 0~100 you guess:"); double figure =input.nextDouble(); if (figure<answer>answer){ System.out.println("your answer is a littlle high "); }else { System.out.println("you are so smart "); } if(i==10) { System.out.println("your aready input 10 times"); } } } }</answer>
发表于 2019-08-30 11:07:15 回复(0)
n =10
 num1=int(input("请输入一个整数 num1=  "))
 i=0
whilei <=10:
    ifn > num1 :
         print("小了")
    elifn < num1 :
         print("大了")
    else:
         print("恭喜您,猜对了")
 i+=1
发表于 2018-08-15 22:17:52 回复(0)
package test;
import java.util.Scanner;
public class Guessnum {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Guessnum h=new Guessnum();
        h.guess();
    }


public void guess() {
    
    int m=(int)(Math.random()*100);
    for(int i=0;i<=10;i++) {
        Scanner sc = new Scanner(System.in); 
        System.out.println("请输入您猜的数:"); 
        int num = sc.nextInt(); 
    if (num>m) {
        System.out.println("您的输入过大");
        continue;
    }
    if (num<m) {
        System.out.println("您的输入过小");
        continue;
    }
    if (num==m) {
        System.out.println("恭喜您猜了"+i+"次猜中了");
    }
    
}
}
}


发表于 2018-08-07 12:05:54 回复(0)
#coding = utf-8 
answer = 33;
count = 0;
当count <10:
    cin = int(raw_input('>'))
    count = count + 1
    如果cin <answer:
        print“太小了!”
    elif cin>回答:
        打印“太老了!”
    else:
        打印“这是计数%d”%计数
        中断
发表于 2018-08-05 11:48:21 回复(0)
import java.util.*;
public class test{
    public static void main(String[] args){
        Random random = new Random();
        int s = random.nextInt(100);
        Scanner sc = new Scanner(System.in);
        int count = 0;
        System.out.println("please guess number between 0-100");
        while(sc.hasNext()&&count<10){    
            int c = sc.nextInt();
            if(c>s) System.out.println("the number is more than the correct number,please write down a  smaller number");
            else if(c<s)System.out.println("the number is less than the correct number,please write down a larger number");
            else{ 
                    System.out.println("you are correct,the number is "+ s  );
                    System.out.println("you have guessed "+ count + " times");
                break; } 
            count++;
        }
        if(count==10) System.out.println("game over,no times left");
    }
    
}


发表于 2018-08-03 16:28:28 回复(0)
#include<stdio.h>
main()
{
    int a=4,i=1,b;%可以随机输入
    while(i<=10)
    {
    scanf("%d",&b);
    if(a>b)
        printf("ab");
    else if(a<b)
            printf("ba");
    else break;
    i++;
    }
    printf("%d",i);
      system("pause");


}
 
发表于 2018-08-01 10:42:05 回复(0)
#include <iostream>
#include <time.h>
#include<stdlib.h>
usingnamespacestd;
int main() {
    int a  = 0;
    srand(time(NULL));//设置随机数种子,使每次运行获取到的随机数值不同。
    a =rand()%100+1;//获取1-100的随机数。
    int n = 0;
    int count = 0;
    cout<<"生成的是1-100的数,请猜>>";
    while(cin>>n && count++ <10) {
        if(n < a)
            cout<<"输入过小\n";
        elseif(n > a) {
            cout <<"输入过大\n";
        } else
            cout << count << endl;
    }
}

发表于 2018-07-26 15:12:54 回复(0)
#include<iostream>
usingnamespacestd;
 
intmain(){
intx;
 
while(cin>>x){
inty;
intn=0;
boolflag=false;
        do{
            cin>>y;
               n++;
           if(y>x)
                cout<<"过大"<<endl;
            elseif(y<x)
                cout<<"过小"<<endl;
            else
                   { cout<<n<<endl;   
                    flag=true;   
                    }
        }while(n<=10&&flag==false)
 
}
return0;
}
不知道怎么判定的,反正我对了

发表于 2018-07-23 11:28:59 回复(0)
#include <iostream>
#include <time.h>
#include<stdlib.h>
using namespace std;
int main()
{
    int a = 0;
    srand(time(NULL));//设置随机数种子,使每次运行获取到的随机数值不同。
    a = rand()%100+1;//获取1-100的随机数。
    int n = 0;
    int count = 0;
    cout<<"生成的是1-100的数,请猜>>";
    while(cin>>n && count++ <10)
    {
        if(n < a)
            cout<<"输入过小\n";
        else if(n > a)
        {
            cout << "输入过大\n";
        }
        else
            cout << count << endl;

    }
}


发表于 2018-07-22 10:15:35 回复(0)
#include<iostream>


using namespace std;


int main()
{
        int target;
        cin >> target;
        cout << "请输入你猜测的数据" << endl;
        int count = 0;
        int temp;
        while (cin>>temp)
        {
            count++;
            if (temp == target)
            {
                cout << "您用了" << count << "次猜中了" << endl;
                break;
            }
            else if (temp < target)
            {
                if (count >=10)
                {
                    cout << "您达到了10次没有猜中" << endl;
                    break;
                }
                else
                    cout << "您猜的数据小了请重新猜" << endl;
            }
            else
            {
                if (count >= 10)
                {
                    cout << "您达到了10次没有猜中" << endl;
                    break;
                }
                else
                    cout << "您猜的数据大了请重新猜" << endl;
            }

        }
        system("pause");
        return 0;
    
}

发表于 2018-07-20 16:49:52 回复(0)