Various input scenarios for the Java Language (ACM Common)

Source: Internet
Author: User

1. Enter only one set of data:
Scanner s=new Scanner (system.in);
int A=s.nextint ();
int B=s.nextint ();

2. Enter more than one set of data, no description to enter a few sets of data, each group of data for a row:
Scanner s=new Scanner (system.in);
while (S.hasnext ()) {//To determine if data end
int A=s.nextint ();
int B=s.nextint ();
}

3. Enter multiple sets of data, the first behavior of an integer n, indicating that there are n sets of test data, followed by N-group data, each group of data in one row;
int A;
int b;
int n;
System.out.println ("Please enter an integer n:");
Scanner in=new Scanner (system.in);
N=in.nextint ();
for (int i=0;i<n;i++) {
System.out.println ("Please enter an integer A and b:");
Scanner s=new Scanner (system.in);
A=s.nextint ();
B=s.nextint ();
System.out.println (A+B);
}
3. Read in string
The input data has more than one line, the first line is an integer n, which represents the number of test numbers, followed by n rows;
System.out.println ("Please enter an integer n:");
Scanner s=new Scanner (system.in);
int N=s.nextint ();
for (int i=0;i<n;i++) {
String Str=s.next ();
System.out.println (str);
}
Or you can write it like this:
System.out.println ("Please enter an integer n");
Scanner s=new Scanner (system.in);
int N=integer.parseint (S.nextline ());
for (int i=0;i<n;i++) {
String Str=s.nextline ();
System.out.println (str);
}
3. Input string
If given a date, the date of the output is the day ordinal of the year.
System.out.println ("Please enter a year");
Scanner s=new Scanner (system.in);
int dd[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
while (S.hasnext ()) {
int days=0;
String Str=s.nextline ();
String[] Date=str.split ("/");
int Y=integer.parseint (date[0]);
int M=integer.parseint (date[1]);
int D=integer.parseint (date[2]);
if ((y%400 = = 0 | | (y%4 = = 0 && y%100!=0)) && m>2) days + +;
Days + = D;
for (int i=0;i<m;i++) {
Days + = Dd[i];
}
System.out.println (days);
}

Various input scenarios for the Java Language (ACM Common)

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.