当前位置:网站首页>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
边栏推荐
- 2022制冷与空调设备运行操作国家题库模拟考试平台操作
- PWN attack and defense world int_ overflow
- [programming training] delete public characters (hash mapping) + team competition (greedy)
- Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
- Office365 - how to use stream app to watch offline files at any time
- How relational databases work
- The computer has a network, but all browser pages can't be opened. What's the matter?
- MySQL and redis consistency solution
- Todolist classic case ①
- She is the "HR of others" | ones character
猜你喜欢
源代码加密的意义和措施
Cyclic neural network
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
038 network security JS
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
2022电工(中级)复训题库及答案
【mysql学习笔记25】sql语句优化
Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years
组件的自定义事件②
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
随机推荐
How to check ad user information?
力扣每日一题-第31天-1502.判断能否形成等差数列
Cyclic neural network
go通用动态重试机制解决方案的实现与封装
【mysql学习笔记26】视图
Eigen matrix operation Library
浏览器本地存储
Redisson uses the full solution - redisson official documents + comments (Part 2)
十大劵商如何开户?另外,手机开户安全么?
redisson使用全解——redisson官方文檔+注釋(上篇)
STM32 uses esp01s to go to the cloud, mqtt FX debugging
Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
2022 test question bank and simulation test of tea master (primary) operation certificate
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
Kickback -- find the first palindrome character in a group of characters
sqlalchemy创建MySQL_Table
QT -- 1. QT connection database
【R语言】年龄性别频数匹配 挑选样本 病例对照研究,对年龄性别进行频数匹配
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
2022 tea master (intermediate) recurrent training question bank and answers