当前位置: 代码迷 >> J2SE >> 简单程序功能
  详细解决方案

简单程序功能

热度:81   发布时间:2016-04-24 02:17:01.0
求一个简单程序功能
功能要求 程序随机给出两个数字 第一次猜对 加五分 第二次猜对加三分 第三次加两分 当输入999时程序退出 大概是这个意思 能写出大概的代码就好

------解决方案--------------------
很简单,随机产生两个100以内的数 :
int r1 = (int) (Math.random() * 100);
int r2 = (int) (Math.random() * 100);
然后用switch 方法写出五个状态来 case 1: System.out.println( "输入第一次要猜的数字 ");

if (var == r1 || var == r2) {
point += 5 ;}
else {
point = point; }

System.out.println( "输出分数: " + point);
同理 case 2 和 3;
case 4 :System.out.println( "查看两个随机数的值 ");
System.out.println(r1);
System.out.println(r2);
case 999 :System.out.println( "退出 ");
System.exit(0);
break;
------解决方案--------------------
Java code
import java.util.Random;public class GuessAction {    private int[] nums = new int[2];// 声明一个数组用来存放两个随机数    private int count = 1;// 声明一个计数器记录已猜的次数    public int[] getNums() {        return nums;    }    public GuessAction() {        super();        new GuessAction(10);// 默认为随机数[0~10)    }    public GuessAction(int n) {// 取随机产生范围        Random random = new Random();// 实例化随机数类        int randomNum1 = random.nextInt(n);// 将随机产生 和{0~n)的数赋给randomNum1        int randomNum2 = random.nextInt(n);// 将随机产生 和{0~n)的数赋给randomNum2        this.nums[0] = randomNum1;// 给数组赋值        this.nums[1] = randomNum2;    }    /**     * 娄判断结果     *      * @param num1     *            ,num2 要判断的数1和数2     * @return 表示得分和次数,-11表示猜的第一个数和第二个数太小,-12表示猜的第一个数太小但第二个数太大,     *         -21表示猜的第一个数太大但第二个数太小 ,-22 表示猜的第一个数太大且第二个数太大 ,-01     *         表示第一个数正确但第二个输入的数太小 ,-02 表示第一个数正确第二个数太大 ,-10 表示第一个数太小第二个数太小 ,-20     *         表示第一个数太大但第二个数正确 ,输入 是999返回0,猜对返回分数     */    public int[] judge(int num1, int num2) {        int[] result = new int[2];// 声明一个返回数组,第一个存得分,第二个存已猜次数        if (999 != num1 && 999 != num2) {            if (num1 < this.nums[0]) {                if (num2 < this.nums[1]) {                    result[0] = -11;                    this.count++;                } else if (num2 > this.nums[1]) {                    result[0] = -12;                    this.count++;                } else {                    result[0] = -10;                    this.count++;                }            } else if (num1 > this.nums[0]) {                if (num2 < this.nums[1]) {                    result[0] = -21;                    this.count++;                } else if (num2 > this.nums[1]) {                    result[0] = -22;                    this.count++;                } else {                    result[0] = -20;                    this.count++;                }            } else if (num1 == this.nums[0]) {                if (num2 < this.nums[1]) {                    result[0] = -01;                    this.count++;                } else if (num2 > this.nums[1]) {                    result[0] = -02;                    this.count++;                } else {                    if (this.count == 1) {                        result[0] = 5;                    } else if (this.count == 2) {                        result[0] = 3;                    } else {                        result[0] = 2;                    }                }            }            result[1] = this.count;        }        return result;    }}
  相关解决方案