随机函数 文本输入响应 的综合运用比如1000以内猜数:初始化计算图标:rannum:=Random(0,1000,1)交互,双击打开,输入相关提示文字交互--文本输入,下挂计算图标,内写代码:if rannumif rannum>NumEntry then ans:="偏小" if rannum=NumEntry then ans:="恭喜您,猜对了" 返回同问。。。
#include#include main() { int a[10];int i=0,j,k,m=1,N; printf("Would you like to guess a number ?\nYES=1 NO=0\n"); scanf("%d",&N); while(N) { m=random(0,100); i=0; while(i<=10) { printf("Please guess a number\n"); scanf("%d",&k); if(k>m) { printf("TOO HIGH !\n");i++;continue; } else if (k { printf("TOO LOW!\n");i++;continue; } if(k==m) { printf("RIGHT!\n"); printf("guess %d times\n",i+1); break; } } if(i==11)printf("NO CHANCE~!\n"); printf("Would you like to guess again ?\nYES=1 NO=0\n"); scanf("%d",&N); } return 0; } 以前的作业题。希望对楼主有帮助!
6,作一个猜数字的游戏随机生成110之间的数字然后有5次机会
public class GuessNum public static void main(String[] args) Random random = new Random(); int num = random.nextInt(10); System.out.println("请输入一个整数:"); Scanner scanner = new Scanner(System.in); int i = 5; while(i > 0) int sysin = scanner.nextInt(); if (sysin == num) System.out.println("恭喜猜对!"); break; } else if(i != 1) i--; System.out.println("请再输一次!还剩"+i+"次机会"); }else System.out.println("机会用完,下次再来"); } } }}#include #include main() { int num, enter; srand(time(null)); //使用当前时间重置随机种子发生器,不然每次产生的随机数就是一样的序列 num = rand() * 1001; //产生 [0,100] 区间的随机数,不知道你要不要生成0和100,我输的是包含0和100的。 printf(“请输入一个数:"); scanf("%d", &enter); if (num == enter) printf("恭喜你,你猜中了!"); else printf("请再努力!"); }
7,1怎样用java做一个猜数字游戏包括文本框一个内容产生一个
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class R extends JFrame{
int flag;
Random rand = new Random();
JTextField t1;
JTextField t;
JLabel l;
public R(){
setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = getContentPane();
JPanel p = new JPanel();
t1 = new JTextField(6);
JButton b1 = new JButton("产生随机数");
l = new JLabel();
t = new JTextField(6);
JButton b2 = new JButton("猜数");
p.add(t1);
p.add(b1);
p.add(t);
p.add(b2);
p.add(l);
container.add(p);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
flag = rand.nextInt(1000);
t1.setText("***");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
int temp = Integer.parseInt(t.getText());
if(temp>flag){
l.setText("大了");
}else if(temp