基础 Day 4

Scanner对象

  • 我们可以通过scanner类来获取用户的输出

  • 通过scanner类的next()和nextLine()方法获取输入的字符串,在读取前需要使用hasNext()与hasNextLine()判断是否还有输入的数据

    • next()

      • 一定要读取到有效字符后才可结束输入

      • 对输入有效字符之前遇到的空白next()方法会自动去掉

      • 输入有效字符之后的空白作为分隔符或结束符

      • next()不能得到带有空格的字符串

    • nextLine()

      • 以enter为结束符,即返回的是enter之前的所有数据

      • 可以获得空白

  • package com.pan.scanner;
    ​
    import java.util.Scanner;
    ​
    public class Demo01 {
        public static void main(String[] args) {
            //常见一个扫描器对象,用于接收键盘数据
            Scanner scanner = new Scanner(System.in);
    ​
            System.out.println("使用next方法接收:");
    ​
            //判断用户有没有输入字符串
            if (scanner.hasNext()){
                String str = scanner.next();//程序会等待用户输入完毕
                System.out.println("输入的内容为:"+str);
            }
            System.out.println("111");
            //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯,用完就关掉!!!
            scanner.close();
        }
    }
  • package com.pan.scanner;
    ​
    import java.util.Scanner;
    ​
    public class Demo02 {
        public static void main(String[] args) {
            //使用scanner接收数据
            Scanner scanner = new Scanner(System.in);
            System.out.println("使用nextLine方法:");
            if (scanner.hasNextLine()){
                //判断是否还有输入
                String str = scanner.nextLine();
                System.out.println("输入的内容为:"+str);
            }
            scanner.close();
        }
    }
  • package com.pan.scanner;
    ​
    import java.util.Scanner;
    ​
    public class Demo03 {
        public static void main(String[] args) {
            //使用scanner接收数据
            Scanner scanner = new Scanner(System.in);
    ​
            System.out.println("请输入数据:");
    ​
            String str = scanner.nextLine();
    ​
            System.out.println("输出的内容为"+str);
    ​
            scanner.close();
        }
    }
    ​
  • package com.pan.scanner;
    ​
    import java.util.Scanner;
    ​
    public class Demo04 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    ​
    ​
            //从键盘接收数据
            int i = 0;
            float f = 0.0f;
    ​
            System.out.println("请输入整数:");
    ​
            if (scanner.hasNextInt()){
                i = scanner.nextInt();
                System.out.println("整数数据:"+i);
            }else{
                System.out.println("输入的不是整数数据!");
            }
    ​
            System.out.println("请输入小数:");
    ​
            if (scanner.hasNextFloat()){
                f = scanner.nextFloat();
                System.out.println("浮点数数据:"+f);
            }else{
                System.out.println("输入的不是浮点数数据!");
            }
            scanner.close();
        }
    }
  • package com.pan.scanner;
    ​
    import java.util.Scanner;
    ​
    //练习:输入多个数字,求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并执行输出结果
    public class Demo05 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);//用scanner接收键盘输入的数据
            double sum = 0;//sum为输入数字的和
            int m = 0;//m为输入个数
            System.out.println("请输入数字:");
            while(scanner.hasNextDouble()){
                double x = scanner.nextDouble();
                m++;
                sum = sum + x;
                System.out.println("这是你输入的第"+m+"个数据,目前你输入数据的总和为"+sum);
            }
            System.out.println("你总共输入了"+m+"个数据");
            System.out.println("这"+m+"个数据的总和为:"+sum);
            System.out.println("这"+m+"个数据的平均数为:"+(sum/m));
            scanner.close();
        }
    ​
    }

注意:scanner读取单个字符使用:char 变量名 = scanner.next().tocharArray()[0]

顺序结构

  • 从上到下依次执行

  • 是任何一个算法都离不开的一种基本算法结构

  • package com.pan.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");
        }
    }
    ​

选择结构

  • if单选择结构:if () {}

  • if双选择结构:if () {}else {}

  • if多选择结构:if () {}else if () {}else {}\

  • if嵌套结构: if () { if () {}}

  • switch选择结构:

    • switch case语句判断一个变量与一系列值中某个值是否相等

    • switch中的变量类型可以是:

      • byte、short、char

      • Java SE 7 开始支持String

      • case标签必须为字符串常量或空白量

  • package com.pan.struct;
    ​
    import java.sql.SQLOutput;
    import java.util.Scanner;
    ​
    public class IfDemo01 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入内容:");
            String s = scanner.nextLine();
    //      equals判断字符串是否相等
            if (s.equals("Hello")){
                System.out.println(s);
            }
            System.out.println("END");
        }
    }
    ​
  • package com.pan.struct;
    ​
    import java.util.Scanner;
    ​
    public class IfDemo02 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            //考试分数大于60为及格,小于60 为不及格
            System.out.println("请输入你的考试成绩:");
            double score = scanner.nextDouble();
            if (score < 60){
                System.out.println("你的考试成绩不及格!");
            }else{
                System.out.println("你的考试成绩及格了!");
            }
    ​
            scanner.close();
        }
    }
    ​
  • package com.pan.struct;
    ​
    import java.util.Scanner;
    ​
    public class IfDemo03 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            //考试分数大于60为及格,小于60 为不及格
            System.out.println("请输入你的考试成绩:");
            double score = scanner.nextDouble();
            if (score < 60 && score>=0){
                System.out.println("你的考试成绩不及格,且等级评定为F!");
            }else   if (score >= 60&&score < 70){
                System.out.println("你的考试成绩评定等级为D!");
            }
            else   if (score >= 70&&score < 80){
                System.out.println("你的考试成绩评定等级为C!");
            }else   if (score >= 80&&score < 90){
                System.out.println("你的考试成绩评定等级为B!");
            }else   if (score >= 90&&score <= 100){
                System.out.println("你的考试成绩评定等级为A!");
            }else{
                System.out.println("成绩不合法!");
            }
    ​
            scanner.close();
        }
    }
    ​
  • package com.pan.struct;
    ​
    import java.util.Scanner;
    ​
    public class SwitchDemo01 {
        public static void main(String[] args) {
            //
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入:");
    ​
    //        char grade = 'C';
            char grade = scanner.next().toCharArray()[0];//读取输入的单个字符
    ​
            switch (grade){
                case 'A':
                    System.out.println("优秀");
                    break;
                case 'B':
                    System.out.println("良好");
                    break;
                case 'C':
                    System.out.println("及格");
                    break;
                case 'D':
                    System.out.println("再接再厉");
                    break;
                case 'E':
                    System.out.println("挂科");
                    break;
                default:
                    System.out.println("未知等级");
    ​
            }
            scanner.close();
        }
    }
    ​

循环结构

  • while循环:while(布尔表达式){ } //先判断再执行(日常编程中避免死循环)

  • package com.pan.struct;
    ​
    public class WhileDemo01 {
        public static void main(String[] args) {
            //输出1-100
            int i = 0;
            while (i<100){
                i++;
                System.out.println(i);
            }
        }
    }
  • package com.pan.struct;
    ​
    public class WhileDemo02 {
        public static void main(String[] args) {
            //死循环
            while (true){
                //等待客户端链接
                //定时检查
            }
        }
    }
    ​
  • package com.pan.struct;
    ​
    public class WhileDemo03 {
        public static void main(String[] args) {
            //计算1+2+……+100
            int i = 0;
            int sum = 0;
            while (i<=100){
                sum+=i;
                i++;
            }
            System.out.println(sum);
        }
    }
  • do……while循环:即使不满足条件至少会执行一次//先执行再判断

  • package com.pan.struct;
    ​
    public class DoWhileDemo01 {
        public static void main(String[] args) {
            int i = 0;
            int sum = 0;
            do {
                sum+=i;
                i++;
            }while (i<=100);
            System.out.println(sum);
        }
    }
  • package com.pan.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);
        }
    ​
    }
  • For循环

    • 是最有效、最灵活的循环结构

    • for( ; ;){}是死循环

    • 增强for循环:for(声明语句:表达式){}//主要用于数组(是一种简化for循环的写法)

    • 附:println:输出换行; print:输出不换行

  • package com.pan.struct;
    ​
    public class ForDemo01 {
        public static void main(String[] args) {
            int a = 1;//初始化条件
            while (a<=100){//条件判断
                System.out.println(a);//循环体
                a+=2;//迭代
            }
            System.out.println("while循环结束");
    ​
            for (int i=1;i<=100;i++){
                System.out.println(i);
            }
    ​
            System.out.println("for循环结束");
        }
    }
    ​
  • package com.pan.struct;
    ​
    public class ForDemo02 {
        public static void main(String[] args) {
            //练习1:计算0-100之间的奇数和偶数的和
            int oddsum = 0;
            int evensum = 0;
            for (int i = 0; i <= 100; i++) {
                if (i%2==0){
                    evensum+=i;
                }else {
                    oddsum+=i;
                }
    ​
            }
            System.out.println(oddsum);
            System.out.println(evensum);
    ​
        }
    }
    ​
  • package com.pan.struct;
    ​
    public class ForDemo03 {
        public static void main(String[] args) {
            //练习:用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个
            //1.for循环
            for (int i = 0; i <= 1000; i++) {
                if (i%5==0){
                    System.out.print(i+"\t");
                    if (i%(5*3)==0){
                        System.out.print("\n");
                    }
                }
    ​
            }
            System.out.println("===============这是一条分界线================");
            //while循环
            int a = 0;
            while (a<=1000){
                if (a%5==0){
                    System.out.print(a+"\t");
                    if (a%(5*3)==0){
                        System.out.print("\n");
                    }
                }
                a++;
            }
        }
    }
    ​
  • package com.pan.struct;
    ​
    public class ForDemo04 {
        public static void main(String[] args) {
            //练习:打印九九乘法表
            for (int i = 1; i <=9; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(j+"*"+i+"="+(i*j)+"\t");
                }
                System.out.println();
            }
        }
    }
    ​
  • package com.pan.struct;
    ​
    public class ForDemo05 {
        public static void main(String[] args) {
            int[] numbers = {10,20,30,40,50};//定义数组
    ​
    ​
            for (int i = 0;i<5;i++){
                System.out.println(numbers[i]);
            }
            System.out.println("===================");
    ​
    ​
            //遍历数组中的元素
            for (int x:numbers){
                System.out.println(x);
            }
        }
    }
    ​

break && continue

  • break用于强制退出循环,不中止程序(通常使用于switch case语句)

  • package com.pan.struct;
    ​
    public class BreakDemo {
        public static void main(String[] args) {
            int i = 0;
            while (i<100){
                i++;
                System.out.println(i);
                if (i==30){
                    break;//跳出了while循环
                }
            }
            System.out.println("123");//并未中止程序
        }
    }
    ​
  • continue用于中止某次循环

  • package com.pan.struct;
    ​
    public class ContinueDemo {
        public static void main(String[] args) {
            int i = 0;
            while (i<100){
                i++;
                if (i%10==0){
                    System.out.println();
                    continue;
                }
                System.out.print(i);
            }
        }
    }
    ​

练习: 打印三角形

package com.pan.struct;
​
import java.util.Scanner;
​
public class TestDemo01 {
    //打印三角形
    public static void main(String[] args) {
        System.out.println("你想要打印几行的三角形:");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        for (int i = 1; i <= a; i++) {
            for (int j= a; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j= 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j= 1; j < i; j++) {
                System.out.print("*");
            }
​
            System.out.println();
        }
    }
}
​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值