Scanner
package scanner;
import java.util.Scanner;
/**
* @author: 梅子黄时雨
* */
public class ScannerTest {
// Scanner对象用来获取用户的输入
// 通过Scanner类的next()和nextLine()获取输入的字符串
// 读取之前要用scaner.hasNext()或者scanner.nextLine()来判断是否还有输入的数据
// Scanner.next()和Scanner.nextLine()的区别:
// Scanner.next()返回空格之前的字符串,Scanner.nextLine()返回回车换行之前的字符串
//
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if(scanner.hasNext()){
System.out.println(scanner.next()); //hello
System.out.println(scanner.nextLine()); //hello world
}
scanner.close();
}
}
After learning the scanner, you can perform simple input and output, and with flow control statements, you can realize simple functions.
process control
On the basis of javascript, I have already understood the sequence structure, selection structure and loop structure, so I won't go into details here, and do a few exercises to consolidate the grammar of java.
Find the sum of odd numbers and even numbers within 1~100
package scanner;
/**
* @author :梅子黄时雨
* */
public class RangeSum {
public static void main(String[] args) {
// 求1~100以内的数的奇数和和偶数和
int oddSum = 0;
int evenSum = 0;
for(int i = 1;i<=100;i++){
if(i%2 != 0){
oddSum += i;
}else{
evenSum+=i;
}
}
// 2500
System.out.println("奇数和:"+ oddSum);
// 2550
System.out.println("偶数和:"+ evenSum);
}
}
Output a triangle with five elements
package scanner;
/**
* @author :梅子黄时雨*/
public class Triangle {
public static void main(String[] args) {
// 输出一个五行的三角形
for(int i = 0; i<5;i++){
for(int j = 5; j>=i;j--){
System.out.print(" ");
}
for(int j = 0;j<=i;j++){
System.out.print("*");
}
for(int j = 0; j<i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
Output ninety-nine multiplication table
package scanner;
/**
* @author 梅子黄时雨
* */
public class MultiTable {
// 打印九九乘法表
public static void main(String[] args) {
for(int i = 1; i<=9;i++){
for(int j =1;j<=i;j++){
System.out.print(i +"*"+j+"="+i*j);
System.out.print(" ");
}
System.out.println();
}
}
}
Do a few small exercises to take it easy, get used to writing js, and unconsciously write the type as let, const,,, come on!