Review University-freshman Java experiment-Experiment 2 Java program Input and Output

Source: Internet
Author: User
Lab Purpose

1. Understand Java input and output programming and operations.

2. Learn about the input and output on the Java application character interface.

Lab content

Write a Java application program.

1) read a three-digit integer from the console
2) output it in reverse order (for example, if 123 is input, the output result is 321)

Description: an int type is read from the console. The entire division and remainder are used to output the data in reverse order.

Note

1. Java is case sensitive and must be noted. For example, in system, in bytes, S, I, and in nextline.

2. The program is composed of a series of statements. ";" must be added at the end of the statement, and the symbols in the Java program must be entered in English; otherwise, a syntax error occurs.

3. in Java, each class is in a separate file, and the class name must be the same as the file name. The class that can be run must contain the public static void main (string [] ARGs) and can only be compiled without the main method.

4. Pay attention to the friendliness of the program interface. For example, you should be prompted before asking the user to input an integer from the interface. Repeated scan = new partition (system. In );
System. Out. println ("Please input an integer :");
Scan. readlineint ();

5. The Division operator "/" is an integer operation that directly discards the part after the decimal point.

-----------------------------------------------------------------------------

Below is my code

-----------------------------------------------------------------------------

import java.util.Scanner;public class shiyan2 {public static void main(String arg[]){int a,s;Scanner scan=new Scanner(System.in);System.out.println("please input an integer(3):");a=scan.nextInt();s=a%10*100+(a%100-a%10)+a/100;System.out.println(s);}}

Now let's think about it. The experiment requires that we use an int integer because we haven't learned the string, so we have limited three digits.

If a string is used, you can output a string in reverse order, not limited to numbers or length. it is also very simple, that is, the output string in reverse order. I will not write it here. When beginners learn the string, they can think about it and practice it.

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.