Java Exercise-005

Source: Internet
Author: User

Import Java.util.Scanner;

Import java.io.*;


/**

* @author Yufeifei

* @version November 20, 2017 17:44:16

This practice harvest is as follows:

1. Create an input stream scanner: Scanner scan = new Scanner (sysem.in);

2. Receive user input: String input = Scan.nextline (); int input = Scan.nextint (); String input = Scan.next ();

3. Create file Input stream: printstream PS = new PrintStream ("C:/log.txt")

The input stream input log is rewritten by system.setout (PS);

4. Coercion type conversion Syntax: string tmp2 = (string) tmp1;

5, XOR operation (^) rule: The same is 0, the difference is 1;

6, ternary operator rule: conditional operation? Operation result 1: The result of the operation 2, if the operation condition is true, the result of the operation is returned 1, if the operation condition is false, then the result of the operation 2 is returned;


/** get user input through the scanner class */

public class testdemo12{

public static void Main (String agrs[]) {

Scanner Scanner = new Scanner (system.in);//create an input stream scanner

System.out.println ("Please enter your ID number:");//Prompt user input

String line = Scanner.nextline ();//get user input lines of text

//Print a description of the input text

System.out.println ("Originally your ID number has" + line.length () + "digits ah. ");

//}

//

//}


/** redirect Input stream Implementation program log */

public class testdemo12{

public static void Main (String agrs[]) {

try{

PrintStream out = system.out;//Save the original output stream

PrintStream ps = new PrintStream ("C:/log.txt");//create file input stream

System.setout (PS);//Set up with a new input stream

int age = 18;//defines an integer variable

System.out.println ("Age variable is defined successfully, initial value is 18");

String sex = "female";//define string variable

System.out.println ("The gender variable is defined successfully, the initial value is female");

//integration of 2 variables

String info = "This is a" + Sex + "child and should have" + age + "years old. ";

System.out.println ("Consolidates 2 variables for the info string variable, the result is:" + info);

System.setout (out);//restore the original input stream

System.out.println ("The program has finished running, please review the log file. ");

}catch (FileNotFoundException e) {

e.printstacktrace ();

//}

//}

//}


/** automatic type conversion and forced type conversion

* Syntax: Converted type variable = (converted type) variable to be converted */

public class testdemo12{

public static void Main (String agrs[]) {

byte B = 127;

char c = ' W ';

Short s = 23561;

int i = 3333;

long L = 400000L;

float f = 3.141591F;

Double d = 54.523;

//low type to high type automatic conversion

System.out.println ("Cumulative byte equals:" + B);

System.out.println ("Cumulative char equals:" + (b + c));

System.out.println ("Cumulative short equals:" + (b + C +s));

//high type to low type cast

System.out.println ("Converts a long cast type to int:" + (int) l);

//high type to low type conversion lost data

System.out.println ("Convert long light type to short:" + (short) l);

//}

//}

The/** operator, denoted by a symbol (^), is a bitwise of two operands with the same result of 0 and a different result as 1*/

public class testdemo12{

public static void Main (String agrs[]) {

Scanner scan = new Scanner (system.in);

System.out.println ("Please enter an English string or decrypt the string:");

String password = scan.nextline ();//Get user input

Char[]array = Password.tochararray ();//Get character array

for (int i = 0; i < Array.Length; i++) {;//traversal character array

Array[i] = (char) (Array[i] ^ 20000);//The XOR of each array element

//}

System.out.println ("Encrypted or decrypted results are as follows:");

System.out.println (new String (array));//Output key

//}

//}


/** using ternary operators to determine odd and even numbers

* Syntax: conditional operation? Operation result 1: result of Operation 2

* If the conditional operation result is true, the return value is the result of the operation 1, otherwise the return value is the result of the Operation 2*/

public class testdemo12{

public static void Main (String agrs[]) {

Scanner scan = new Scanner (system.in);//create an input stream scanner

System.out.println ("Please enter an integer:");

Long number = Scan.nextlong ();//Gets the integer entered by the user

String check = (number% 2 = = 0?) "This number is: even!": "This number is: Odd!");

System.out.println (check);

//}

//}


Java Exercise-005

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.