【编程】编写猜数游戏程序,语言不限。给定某个整数,从键盘上反复输入数据进行猜测。如果未猜中,程序提示输入过大或者过小;如果猜中,则输出猜的次数,最多允许猜10次。
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('猜了')
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+"次猜中了");
}
}
}
}
#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;
}
} #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;
}
}
#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;
}