当前位置:网站首页>Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
2022-07-01 07:54:00 【Onycho】
Scanner object

next() and nextLine()
next()

package com.jiao.scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
// Create a scan object , For receiving keyboard data
Scanner scanner = new Scanner(System.in);
System.out.println(" Use next Send and receive :");
// Determine whether the user has input string
if (scanner.hasNext()){
// Use next Send and receive
String str = scanner.next(); // The program will wait for the user to input
System.out.println(" The input content is :"+str);
}
// All belong to IO( Input 、 Output ) If the class of stream is not closed, it will occupy resources all the time
scanner.close();
}
}
Output
Use next Send and receive :
hello world
The input content is :hello
nextLine()

package com.jiao.scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
// Receive data from keyboard
Scanner scanner = new Scanner(System.in);
System.out.println(" Use next Send and receive :");
// Judge whether there is any input
if(scanner.hasNext()){
String str = scanner.nextLine();
System.out.println(" The input content is :"+str);
}
scanner.close();
}
}
Output
Use next Send and receive :
hello world
The input content is :hello world
package com.jiao.scanner;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" Please input data :");
String s = scanner.nextLine();
System.out.println(" The output content is :"+s);
scanner.close();
}
}
Output
Please input data :
Welcome to Study java
The output content is : Welcome to Study java
practice : We can enter multiple numbers , And find the sum and average , Press enter to confirm each number entered , End the input and output the execution result by inputting non number
package com.jiao.scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
// We can enter multiple numbers , And find the sum and average , Press enter to confirm each number entered , End the input and output the execution result by inputting non number
Scanner scanner = new Scanner(System.in);
// and
double sum = 0;
// Calculate how many numbers are entered
int m = 0;
System.out.println(" Please start typing :");
// Loop to see if there is any input , And sum up every time in it
while (scanner.hasNextDouble()){
double v = scanner.nextDouble();
m = m + 1; //m++
sum = sum + v;
System.out.println(" You entered the number one "+m+" Data , Then the current result is sum="+sum);
}
System.out.println(m + " The sum of the numbers is " + sum);
System.out.println(m + " The average number is " + (sum / m));
scanner.close();
}
}
Output
Please start typing :
2
You entered the number one 1 Data , Then the current result is sum=2.0
88
You entered the number one 2 Data , Then the current result is sum=90.0
911
You entered the number one 3 Data , Then the current result is sum=1001.0
l
3 The sum of the numbers is 1001.0
3 The average number is 333.6666666666667
Sequential structure

package com.jiao.struct;
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
}
}
Output
hello1
hello2
hello3
hello4
Selection structure


package com.jiao.struct;
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter a content :");
String s = scanner.nextLine();
//equals: Determines whether the strings are equal
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
Output
Please enter a content :
Hello
Hello
End

package com.jiao.struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter the grade :");
int score = scanner.nextInt();
if (score > 60){
System.out.println(" pass ");
}else{
System.out.println(" fail, ");
}
scanner.close();
}
}
Output
Please enter the grade :
50
fail,

package com.jiao.struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter the grade :");
int score = scanner.nextInt();
if (score == 100){
System.out.println(" Full marks ");
}else if(score < 100 && score >= 90){
System.out.println("A");
}else if(score < 90 && score >= 80){
System.out.println("B");
}else if(score < 80 && score >= 60){
System.out.println("C");
}else if(score < 60 && score >= 0){
System.out.println(" fail, ");
}else{
System.out.println(" The score entered is not legal ");
}
scanner.close();
}
}
Output
Please enter the grade :
80
B


package com.jiao.struct;
//case through
//switch Match a specific value
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = 'B';
switch (grade){
case'A':
System.out.println(" good ");
break; // Optional , If not, it will happen case Penetration phenomenon , The following statement results will be output
case'B':
System.out.println(" good ");
break; // Optional , If not, it will happen case Penetration phenomenon , The following statement results will be output
case'C':
System.out.println(" pass ");
break; // Optional , If not, it will happen case Penetration phenomenon , The following statement results will be output
case'D':
System.out.println(" Make persistent efforts ");
break; // Optional , If not, it will happen case Penetration phenomenon , The following statement results will be output
default:
System.out.println(" Unknown level ");
}
}
}
Output
good
jdk7 New characteristics
package com.jiao.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = " Li Ming ";
switch (name){
case " Zhang San ":
System.out.println(" Zhang San ");
break;
case " Li Ming ":
System.out.println(" Li Ming ");
break;
default:
System.out.println(" Illegal input ");
}
}
}
Output
Li Ming
Loop structure


package com.jiao.struct;
public class WhileDemo01 {
public static void main(String[] args) {
// Output 1~100
int i = 0;
while (i < 100){
i++;
System.out.println(i);
}
}
}
Output
1
2
、、、
100
package com.jiao.struct;
public class WhileDemo02 {
public static void main(String[] args) {
// Dead cycle
while (true){
// Waiting for the customer to connect
// Timing check
}
}
}
Calculation 1+2+3+…+100 = ?
package com.jiao.struct;
public class WhileDemo03 {
public static void main(String[] args) {
// Calculation 1+2+3+...+100 = ?
int i = 0;
int sum = 0;
while (i <= 100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
Output
5050

package com.jiao.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do{
sum = sum + i;
i++;
}while (i <= 100);
System.out.println(sum);
}
}
Output
5050
package com.jiao.struct;
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a < 0){
System.out.println(a);
a++;
}
System.out.println("================");
do {
System.out.println(a);
a++;
}while (a < 0);
}
}
Output
0

package com.jiao.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1; // Initialization conditions
while (a <= 100) {
// conditional
System.out.println(a); // The loop body
a += 2; // iteration
}
System.out.println("while The loop ends ");
// initialize value 、 conditional 、 iteration
for (int i = 1;i <= 100;i++){
System.out.println(i);
}
//100.for Fast generation for (int i = 0; i < 100; i++)
System.out.println("for The loop ends ");
}
}
practice
practice 1: Calculation 0 To 100 The sum of odd and even numbers between
package com.jiao.struct;
public class ForDemo02 {
public static void main(String[] args) {
// practice 1: Calculation 0 To 100 The sum of odd and even numbers between
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if (i % 2 != 0){
oddSum += i;
}else {
evenSum += i;
}
}
System.out.println(" Odd sum "+oddSum);
System.out.println(" An even sum "+evenSum);
}
}
Output
Odd sum 2500
An even sum 2550
practice 2: use while or for Cyclic output 1~1000 Inter energy 5 Divisible number , And output each line 3 individual
package com.jiao.struct;
public class ForDemo03 {
public static void main(String[] args) {
// practice 2: use while or for Cyclic output 1~1000 Inter energy 5 Divisible number , And output each line 3 individual
for (int i = 0; i <= 1000; i++) {
if(i % 5 == 0){
System.out.print(i+"\t");
}
if(i % (5*3) == 0){
//System.out.println(); It can also be realized
System.out.println("\n");
}
}
//println Line wrap after output
//print The output will not wrap
}
}
Output
0
5 10 15
20 25 30
35 40 45
50 55 60
、、、
practice 3: Output 9X9 Multiplication table
package com.jiao.struct;
public class ForDemo04 {
// practice 3: Output 9X9 Multiplication table
public static void main(String[] args) {
//1. Print the first column first
//2. Fix it 1 Wrap it up again by one person
//3. Remove duplicates i <= j
//4. Adjust the style
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j + "*" + i + "=" + (j * i) + "\t");
}
System.out.println();
}
}
}

package com.jiao.struct;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {
10,20,30,40,50}; // An array is defined
for (int i = 0;i < 5;i++) {
System.out.println(numbers[i]);
}
System.out.println("=============");
// Traversing array elements
for (int x:numbers){
// hold number Assign a value to int
System.out.println(x);
}
}
}
Output
10
20
30
40
50
=============
10
20
30
40
50
边栏推荐
- STM32 uses esp01s to go to the cloud, mqtt FX debugging
- Microsoft stream - how to modify video subtitles
- 奥迪AUDI EDI 项目中供应商需要了解哪些信息?
- [chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
- 038 network security JS
- [untitled]
- Apple account password auto fill
- Custom events of components ①
- 【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
- How relational databases work
猜你喜欢

2022年流动式起重机司机考试练习题及在线模拟考试
![[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched](/img/8d/cf259b9bb8574aa1842280c9661d1e.jpg)
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
![[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario](/img/10/ed857892d2e0ea72e100a4008e6d69.png)
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario

MATLAB之基础知识

浅谈CVPR2022的几个研究热点

【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)

下载Xshell和Xftp

Thesis learning -- Analysis and Research on similarity query of hydrological time series
![[untitled]](/img/c2/63286ba00321c9cdef43ff40635a67.png)
[untitled]

【无标题】
随机推荐
2022危险化学品经营单位主要负责人试题及模拟考试
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
【微服务|openfeign】Feign的日志记录
Basic knowledge of MATLAB
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
凸印的印刷原理及工艺介绍
Warm congratulations on the successful listing of five elements hehe liquor
Redisson uses the full solution - redisson official document + comments (Part 2)
038 network security JS
[MySQL learning notes27] stored procedure
[软件] phantomjs屏幕截图
Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?
Minecraft 1.16.5 module development (51) tile entity
Eigen矩阵运算库快速上手
How to make the two financial transactions faster
How relational databases work
2022电工(中级)复训题库及答案
【R语言】两个/N个数据合并merge函数
PWN attack and defense world int_ overflow