Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of random Class and scanner Class

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Random class and scanner class example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Generate random numbers within a specified range

Math.random () generates a random number between 0 and 1, with a type of double.

Code example:

Public class randCase {public static void main (String [] args) {double rand = 0; for (int I = 0; I < 10; iTunes +) {rand = Math.random (); System.out.println (rand);}

(free video tutorials to share: java video tutorials)

Read strings and integers from standard input (get user input)

Scanner in = new Scanner (System.in) connects to standard input.

Where in represents a variable.

In.nextLine () can read a line of string from the command line.

In.nextInt () can read a positive integer from the command line.

The point operator is the operator in Java, which is the same as the point in System.out.printf () and Math.random (). It is the "operation" after the point on the "variable" before the point. The so-called "operation" refers to the method, which is the method of the main method that we have been writing about. These operations are done in an one-by-one way. Using a method is what we call an invoke a method.

Import java.util.Scanner; tells the program where the type Scanner is.

Create a "variable" of type Scanner, which helps us read data from standard input.

Code example:

Import java.util.Scanner; / / tells the program where the Scanner type is public class scannerCase {public static void main (String [] args) {/ / Scanner.nextLine (); reads a line of string from the command line. Scanner in = new Scanner (System.in); System.out.println ("Please enter a sentence:"); String str = in.nextLine (); System.out.println (str); / / Scanner.nextInt (); read a positive integer line from the command line. System.out.println ("Please enter a number:"); int num = in.nextInt (); System.out.println (num);}} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 251

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report