2016-07-24
1,Math类介绍
Math.Random() [0,1)
2,猜数字游戏
课下作业:猜数字小程序优化,增加次数限制的功能。
package com.java1995; public class TestMath { public static void main(String[] args) { double d = Math.ceil(2.98); System.out.println(d); d = Math.floor(2.98); System.out.println(d); System.out.println(Math.max(6.5, 4.6)); for (int i = 0; i < 10; i++) { System.out.println(Math.random()); } } }
package com.java1995; import java.util.Scanner; public class GuessNumber { public static void main(String[] args) { int num = (int) (Math.random() * 100) + 1;// [1,100] System.out.println("请输入1-100之间的整数"); Scanner sc = new Scanner(System.in); while (true) { int input = sc.nextInt(); if (input > num) { System.out.println("太大了"); } else if (input < num) { System.out.println("太小了"); } else { System.out.println("恭喜你,猜对了!"); break; } } // sc.close(); } }
【参考资料】
时间: 2024-10-05 12:39:00