Print Hello, World. on the first line. Print Hello, Java. on the second line.
Sample Output
Hello, World. Hello, Java.
Just simple Ctrl + C from the question part & Ctrl + V to the code block. Done.
1 2 3 4 5 6 7 8 9 10
publicclassSolution {
publicstaticvoidmain(String[] args) { /* Enter your code here. Print output to STDOUT. Your class should be named Solution. */ System.out.println("Hello, World."); System.out.println("Hello, Java."); } }
3 types of print statements
System.out.print(argument) –> just print out the argument.
System.out.println(argument) –> print out the argument and ends the line.
System.out.printf(format, argument) –> print out the argument by given format.
A print statement is a call to the print / println method of the System.out object.
The print method takes exactly one argument;
The println method takes one or no arguments.
The arguments maybe a String which you create using the string concatenation “+” or an object, the print and println methods call that object’s toString() method to get a printable string.
Stdin and Stdout I
Two ways –> Scanner class OR BufferedReader class
Scanner: specifying Input Stream as System.in.
1 2 3 4 5 6 7 8 9 10 11 12
// create a Scanner Object named scan Scannerscan=newScanner(System.in);
// use Scanner scan to read a String and an int StringmyString= scan.next(); intmyInt= scan.nextInt();
// close Scanner Object because no more input to read scan.close();
System.out.println("myString is: " + myString); System.out.println("myInt is: " + myInt);
BufferedReader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
FileReaderfr=newFileReader("File.txt"); // or use InputStreamReader // InputStreamReader insreader =new InputStreamReader(System.in);
// create a BufferedReader Object BufferedReaderbfreader=newBufferedReader(fr);
String name=""; while(!name.equals("stop")) { System.out.println("Enter data: "); name = bfreader.readLine(); System.out.println("data is: "+ name); }
bfreader.close(); fr.close();
bfreader.close();
Read integers from stdin and then print them to stdout. Each integer must be printed on a new line.
publicstaticvoidmain(String[] args) { Scannerscan=newScanner(System.in); inta= scan.nextInt(); intb= scan.nextInt(); intc= scan.nextInt(); // Complete this line // Complete this line
System.out.println(a); System.out.println(b); System.out.println(c); // Complete this line // Complete this line // BETTER IDEA Scannersc=newScanner(System.in); while(sc.hasNextInt()) { inta= sc.nextInt(); System.out.println(a); } // IDEA 2 /* a Try with Resource to automatically close any classes that implements AutoClosable interface (Scanner class in this case). That way you don't have to do scanner.close() */ try(Scannerscan=newScanner(System.in)) { while (scan.hasNext()) { System.out.println(scan.nextInt()); } } } }
获取数据类型的取值范围
1 2 3 4
if(a >= Short.MIN_VALUE && a <= Short.MAX_VALUE) { System.out.println(“It’s a short type”); }
publicstaticvoidmain(String[] args) { Scanner sc=newScanner(System.in); String A=sc.next(); String B=sc.next(); /* Enter your code here. Print output to STDOUT. */ // Task 1 System.out.println(A.length()+B.length()); // Task 2 // Compare first letter which is in front if(A.charAt(0) > B.charAt(0)) { System.out.println("Yes"); } else { System.out.println("No"); } // Task 3 // Replace first character with upper case System.out.println( A.replace(A.charAt(0), Character.toUpperCase(A.charAt(0))) + " " + B.replace(B.charAt(0), Character.toUpperCase(B.charAt(0)))); } }