java学习-2

程序交互

scanner对象----java.util.Scanner获取用户输入

基本语法

Scanner s=new Scanner(System.in);

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

使用next进行接受

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //创建scanner对象接受输入
        Scanner sc = new Scanner(System.in);
        System.out.println("使用next方式接受:");

        //判断用户有没有输入字符串
        if(sc.hasNext()){
            //使用next方式接受
            String str=sc.next();//程序会等待用户输入完毕
            System.out.println("输出的内容为:"+str);
        }
        //关闭IO流
        sc.close();
    }
}

使用nextLine进行接受

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //创建scanner对象接受输入
        Scanner sc = new Scanner(System.in);
        System.out.println("使用nextLine方式接受:");

        //判断用户有没有输入字符串
        if(sc.hasNextLine()){
            //使用nextLine方式接受
            String str=sc.nextLine();
            System.out.println("输出的内容为:"+str);
        }
        //关闭IO流
        sc.close();
    }
}

next与nextLine的区别

next():

  1. 程序会一直等待用户输入
  2. 会自动删除输入字符串之前的空格
  3. 当输入有效字符后,才会将空白作为结束符
  4. next不能得到带有空格的字符串

nextLine()

  1. nextLine读取一行字符串,以回车作为结束符
  2. nextLine可以得到带有空格的字符串
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //创建scanner对象接受输入
        Scanner sc = new Scanner(System.in);
        System.out.println("使用nextLine方式接受:");

        //使用nextLine方式接受
        String str=sc.nextLine();
        System.out.println("输出的内容为:"+str);
       
        //关闭IO流
        sc.close();
    }
}

其他的输入格式要求

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int i=0;
        float f=0.0f;
        //创建scanner对象接受输入
        Scanner sc = new Scanner(System.in);
        System.out.println("输入整数:");

        if(sc.hasNextInt()){
            i=sc.nextInt();
            System.out.println("输出的整数为:"+i);
        }

        System.out.println("输入小数:");

        if(sc.hasNextFloat()){
            f=sc.nextFloat();
            System.out.println("输出的小数为:"+f);
        }

        //关闭IO流
        sc.close();
    }
}

输入多个数据

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //创建scanner对象接受输入
        Scanner sc = new Scanner(System.in);
         double sum=0;
         int m=0;
         while(sc.hasNextDouble()){
             double v=sc.nextDouble();
             sum+=v;
             m++;
         }
         System.out.println(sum/m);
        //关闭IO流
        sc.close();
    }
}

程序结构

switch多选择结构

switch语句中的变量类型可以是:

  • btye,short,int或char
  • 从jdk7开始支持String类型

增强for循环

for(声明语句:表达式){
    //代码
}
  • 声明语句:声明新的局部变量,该变量的类型必须和数据元素的类型匹配。其作用域限定在循环语句块,其值与此时数据元素的值相等
  • 表达式:表达式是要访问的数据名或者是返回值为数组的方法
public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        //遍历数组
        for(int i:numbers){
            System.out.println(i);
        }
    }
}
posted @ 2025-02-15 15:51  狐狸胡兔  阅读(19)  评论(0)    收藏  举报