首页 > 其他分享 >每天都进步的课堂随笔Day03

每天都进步的课堂随笔Day03

时间:2022-09-02 18:23:16浏览次数:55  
标签:scanner Day03 System String println 课堂 随笔 out Scanner

Java Processing Control(流程控制)

  1. user interaction Scanner (用户交互scanner)

  2. sequential structrue (顺序结构)

  3. case structrue(选择结构)

  4. loop structrue(循环结构)

  5. break&continue

  6. practice

    Scanner

    java.util.Scanner

    通过Scanner类来获取用户的输入

    • 基本语法:
    Scanner s = new Scanner(System.in)
    
    • 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用 hasNext()与hasNextLine()判断是否还有输入的数据
package com.example.demo08;
import java.util.Scanner;
public class demo08 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收: ");
        //判断用户有没有输入字符串
        if(scanner.hasNext()){
            //use next method to receive
            String str = scanner.nextLine();
            System.out.println("输出人内容为:" + str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
        scanner.close();
    }
}
package com.example.demo08;

import java.util.Scanner;

public class demo08 {

    public static void main(String[] args) {
        //we can input so many words,then get sum&avg, using enter key to confirm each num u input
        //to end input by inputting non-numeric num then execute output result
        Scanner scanner = new Scanner(System.in);
        //sum
        double sum = 0;
        //calculate how much num has inputted
        int m = 0;
        //verify if there is any input by loop then count for each sum;
        while(scanner.hasNextDouble()){
         double x = scanner.nextDouble();
         m = m+1;//m++
            sum = sum + x;
        }
        System.out.println(m+"个数的和为" + sum);
        System.out.println(m+"个数的平均值是" + (sum/m));
        scanner.close();

    }

}

顺序结构

  • JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行

  • 顺序结构是最简单的算法结构

  • 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构

选择结构

  • if单选择结构
  • if双选择结构
  • if多选择结构
  • 嵌套的if结构
  • switch多选择结构

if单选择结构

public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);

      System.out.println("please input some content: ");
      String s = scanner.nextLine();
      //equals to confirm if there is any equal String
      if(s.equals("Hello")){
          System.out.println(s);
      }
      System.out.println("End");

      scanner.close();
  }

if双选择结构

public static void main(String[] args) {
    //a score greater than 60 marks a passing grade,if less than 60 marks a failed grade
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:  ");
        int score = scanner.nextInt();
        if(score > 60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }
        scanner.close();
    }

if多选择结构


Switch 多选择结构

  • switch case here to verify a variable which equals to a series of values, and called each value branch

    switch (expression){
        case value :
            //sentence
            break;//alternative
        case value :
            //sentence
            break;//alternative
        default : //alternative
            //sentence
    }
    
    • Switch,the variable in switch could be:
      • byte/short/int/char
      • begin with SE 7
      • String was supported in Switch
      • meanwhile case table must be String constants & literals
public class demo08 {
//case 穿透 //switch 匹配一个具体的值
    public static void main(String[] args) {
      char grade = 'c';
      switch(grade){
          case 'A':
              System.out.println("perfect");
              break;
          case 'B':
              System.out.println("excellent");
          case 'c':
              System.out.println("good");
              break;
          default:
              System.out.println("unknown rank");
      }
    }

标签:scanner,Day03,System,String,println,课堂,随笔,out,Scanner
From: https://www.cnblogs.com/chenwenge/p/16650882.html

相关文章

  • 随笔0902补
     ......
  • 2022.09.02 【Fy5】蒟蒻的联赛备战随笔
    \(2022_{年}09_{月}02_{日}\)距离联赛还有多少时间?我不记得了我只知道,今年是第一次参加CSP-S和NOIP,也将马上迎来人生第一次CCF的大型线下比赛NOIP?省选?NOI?我......
  • 每天都进步的课堂随便Day02
    类型转换#强制转换(类型)变量名高----->低#自动转换低----->高#操作比较大的数的时候,注意溢出问题#JDK7特性数字可以用下划线分割intmoney=10_0000_0000;#int......
  • C++学习笔记-day03
    1、嵌套循环2、跳转语句continue不会使整个循环终止,break会跳出循环//输出2,跳过了1gotoA;cout<<1<<endl;A:cout<<2<<endl;3、数组......
  • 课堂笔记 8.31 软件开发课
    1.软件和程序的区别:软件可以满足用户的固定需求2.软件类别:按照功能:系统软件应用软件支撑软件(开发使用)按照服务对象:项目软件,产品划分软件规模:微型  小型中型......
  • [随笔] 屠龙之技
      “朱泙漫学屠龙于支离益,弹千金之家,三年技成,而无所用其巧。”这是《庄子》里关于“屠龙之技”的故事。故事里的朱泙漫,像是如堂吉诃德一般的愚者,而支离益则像是揣着......
  • 软件工程 统一建模语言(Unified Modeling Language UML) 第4篇随笔
    4.1、统一建模语言(UnifiedModelingLanguageUML)是一种可视化的语言规约系统的制品构造系统的制品建立系统制品的文档UML应用范围可用于对象方法和构件方法可......
  • 学习随笔——洛谷题目P1636解答
    摘要:欧拉图的应用。题目原地址如下:https://www.luogu.com.cn/problem/P1636题目截图如下:  一笔画问题,考察欧拉回路的定义,即所有节点的入度出度的和都为偶数即可满足......
  • 学习随笔——codeforces题目Plus and Multiply解答
    摘要:构造算法与数论的结合,巧妙之处在于我们要自己模拟一遍计算过程然后从中找出特殊点。题目原地址如下:https://codeforces.com/problemset/problem/1542/B题目截图如下:......
  • 学习随笔——codeforces题目Color the Picture解答
    摘要:构造类题目题目原地址如下:https://codeforces.com/problemset/problem/1710/A题目截图如下:  关键词:构造算法,递归,*1500简要翻译:给予k种颜料,第i种颜料可以涂满a......