In Java, enter multiple integers from the keyboard __java

Source: Internet
Author: User

Example: Finding the number of sequences and

Input two integer n,m, the middle is separated by a space, N is the first item, the following items are the previous open root, and the first M.

The first way to enter and read from the keyboard: the Sc.hasnextint () function and the Sc.nextint () function

Hasnextint () determines whether the current input is an integer

Import Java.util.Scanner;
Import java.lang.math.*;

Class test1{public
	static void Main (String [] args) {
          Scanner sc=new Scanner (system.in);
          int m;
          Double N,result;

          while (Sc.hasnextint ()) {
        	n=sc.nextint ();
        	M=sc.nextint ();
        	result=0;

        	for (int i=0; i<m; i++) {result
		    	= N;
		    	n = math.sqrt (n);
	           }
            System.out.printf ("%.2f", result);

          }
       }


The second way: the Sc.trim () function and the Sc.split () function

Sc.trim () Remove string and tail space

Sc.split () splits a string according to the specified character (string) or regular, and the result is returned as an array of strings


Import Java.util.Scanner;
Import java.lang.math.*;

Class test{public
	static void main () {
		Scanner sc=new Scanner (system.in);
		String input=sc.nextline ();
		Input=input.trim ()//Remove string
		string[] Temp=input.spilt (""); Splits a string according to the specified string and returns a
                double n=integer.parsedouble (temp[0] as an array of strings);  
                int M=integer.parseint (temp[1]); 
                Double result=0;
  
                for (int i=0; i<m; i++) {result
	        	= N;
	        	n = math.sqrt (n);
                }
                SYSTEM.OUT.PRINTLN (result);
	}








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.