怎么使用java编程: 1、 随机产生一个1-100(小于100)内的整数,由用户通过键盘输入来猜,当从

怎么使用java编程:
1、 随机产生一个1-100(小于100)内的整数,由用户通过键盘输入来猜,当从键盘接收到游戏者输入的数据后,如果猜对,请输出”你真棒!猜对了!”,程序结束;否则,请输出“你猜大了”或“你猜小了”,然后提示用户继续猜,直到猜对为止;若用户猜的次数超过3次,仍未猜中则强行结束游戏。
升级猜数字游戏:
功能增加:程序还提供了“重新开始”和“退出”的功能,可供游戏者重复进行游戏。

编程思想:使用OO思想。
这个题能给我写一下详细代码吗??谢谢了。。

public class Hello{
public static int max = 0;
static{
System.out.println("*********猜数游戏***********");

}
public static void main(String[] args){
try{
new Hello(Hello.syso());
}catch(Exception e){
System.out.println("输入错误,请输入整数.");
new Hello(Hello.syso());
}
}
public static int syso(){
java.util.Scanner sc = new java.util.Scanner(System.in);
return sc.nextInt();
}
public Hello(int a){
//产生随机数
int suiJi = new java.util.Random().nextInt(101)+1;
switch(max){
case 0:
case 1:
case 2:
if(suiJi == a){
System.out.println("猜中了...");
}else{
System.out.println("请再猜.");
Hello.max = Hello.max + 1;
if(max == 3){
System.out.println("只有三次机会.");
}else{
new Hello(Hello.syso());
}
}
break;

}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-13
package com.isoftstone.interview.traffic;
import java.util.Random;
import java.util.Scanner;
public class BullsandCows {
private static int lucknum;
/**
* 随机产生1-99的一个数字
* @return
*/
public static int makeNum(){
lucknum = new Random().nextInt(99) + 1;
return lucknum;
}
/**
* @param args
*/
public static void main(String[] args) {
BullsandCows.makeNum();
System.out.println("幸运数字是:" + lucknum);
int count = 1;
while(count < 4){
System.out.print("请输入:");
Scanner objScanner = new Scanner(System.in);
int putnum = objScanner.nextInt();
if(putnum == lucknum){
System.out.println("你真棒!猜对了!");
break;
}else if(putnum > lucknum){
System.out.println("你猜大了");
}else{
System.out.println("你猜小了");
}
if(count == 3){
System.out.println("游戏结束!");
}
count++;
}
}
}本回答被网友采纳
第2个回答  2013-04-13
//编写一个字符界面的Java Application 程序,接受用户从键盘输入的一个正整数,然后统计并输出从1到这个正整数的累加和。
import java.awt.*;
import java.awt.event.*;
public class DS implements ActionListener
{
Frame f=new Frame("输入正整数");
Label l=new Label("请输入整数");
Button b=new Button("确定");
TextField t=new TextField(20);
TextField t2=new TextField(20);
Label l1=new Label("结果为");
DS(){
f.setLayout(new GridLayout(2,3));
f.add(l);
f.add(t);
f.add(b);
f.add(l1);
f.add(t2);
f.setSize(200,300);
f.setVisible(true);
t2.setEditable(false);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
int k = Integer.valueOf(t.getText()).intValue();
int sum=0;
String s="";
for (int i=1;i<=k;i++)
{
sum=sum+i;
}
s=s+sum;
t2.setText(s);
}
public static void main(String args[]){
new DS();
}
}
希望采纳。
相似回答