No. 04 Day Java Basic syntax

Source: Internet
Author: User

Introduction of today's content
Random
Array
Chapter 1th Random

1.1 Generating an integer random number 1.1.1 Random use steps
What should we do if we want to generate random numbers of 1~100 (including 1 and 100)? We don't need to write the algorithm ourselves, because Java has provided us with classes that produce random numbers---random:
Role:
– Used to generate a random number
? Use steps (similar to scanner)
– Guide Pack
? Import Java.util.Random;
– Create Objects
? Random r = new Random ();
– Get random numbers
? int number = R.nextint (10);
? The resulting data is between 0 and 10, including 0, not including 10.
? The 10 inside the parentheses is changeable, and if it's 100, that's 0-100 data.
1.1.2 Case Code One:

package com.itheima;import java.util.Random;/* * Random:用于产生随机数 * * 使用步骤: * A:导包 * import java.util.Random * B:创建对象 * Random r = new Random(); * C:获取随机数 * int number = r.nextInt(10); * 获取的是0-10之间的随机数,包括0,不包括10 * * 需求:如何获取到一个1-100之间的随机数呢? */public class RandomDemo {public static void main(String[] args) {// 创建对象Random r = new Random();for (int x = 0; x < 10; x++) {// 获取随机数int number = r.nextInt(10);// 输出随机数System.out.println("number:" + number);}System.out.println("--------------------");// 如何获取到一个1-100之间的随机数呢?int i = r.nextInt(100) + 1;System.out.println("i:" + i);}}

1.2 Guess number Games case: The 1.2.1 system produces a random number between 1-100, please guess how much this data is. 1.2.2 Case Code Two:

package com.itheima;import java.util.Random;import java.util.Scanner;/* * 猜数字小游戏案例 *        系统产生一个1-100之间的随机数,请猜出这个数据是多少。 * 分析: * A:系统产生一个随机数1-100之间的。 * int number = r.nextInt(100) + 1; * B:键盘录入我们要猜的数据 * 用Scanner实现 *        C:比较这两个数据(用if语句) *        大了:给出提示大了 *        小了:给出提示小了 *        猜中了:给出提示,恭喜你,猜中了 *        D:多次猜数据,而我们不知道要猜多少次,怎么办呢? *        while(true) {循环的内容} */public class RandomTest {public static void main(String[] args) {// 系统产生一个随机数1-100之间的。Random r = new Random();int number = r.nextInt(100) + 1;while(true){// 键盘录入我们要猜的数据Scanner sc = new Scanner(System.in);System.out.println("请输入你要猜的数字(1-100):");int guessNumber = sc.nextInt();// 比较这两个数据(用if语句)if (guessNumber > number) {System.out.println("你猜的数据" + guessNumber + "大了");} else if (guessNumber < number) {System.out.println("你猜的数据" + guessNumber + "小了");} else {System.out.println("恭喜你,猜中了");break;}}}}

No. 04 Day Java Basic syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.