Java学习5--基础知识

news/2024/7/8 4:40:06
  • JAVA 流程控制
    • 用户交互scanner
    • 顺序结构
    • 选择结构
    • 循环结构
    • break & continue
    • 练习

用户交互scanner/Scanner 对象
java提供的工具,用来获取用户输入。
java.util.Scanner是Java5引入的特征,用的时候程序最顶上加引用源
import java.util.Scanner
具体用法是
Scanner x = new Scanner(System.in);
配合使用
String str=x.nextline();

解释说明:通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前一般需要使用hasNext() 与 hasNextLine()判断是否还有输入的证据。

next()和nextline()

  • next()

    • 一定要读取到有效字符后才可以结束输入。
    • 只要遇见空格,就停止读取,认为结束了。
  • nextline()

    • Enter为结束符,如果输入了空格也没有关系。
      记得用完要关闭,类似这样 xxx.close();
      最最简单的一段代码举例,
public static void main(String[] args) {
    //新定义了一个Scanner类型的x
        Scanner x=new Scanner(System.in);
    //将x新输入的内容给str
        String str= x.nextLine();
        System.out.println(str);
    //用完关闭,养成良好习惯
          x.close();
    }


(x.close();用完就关闭,是良好的使用习惯)

举例:
要求创建一个扫描器对象,用于接收键盘输入的一句话(两个单词以上),并将这句话输出。
分别使用next()和nexline()输出结果,观察有什么区别。

package 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()){
            //使用Next方式接收
            String str = scanner.next();
            System.out.println("输出的内容为:"+str);
        }
        //凡是属于IO流-即就是输入输出流的类,如果不关闭会一直占用资源,要养成良好的习惯,用完就关闭它。
        scanner.close();
    }
}


因为这句 String str = scanner.next();
所以如果输入了hello world 只是返回hello

使用nextLine() 这个用的多些

package scanner;

import java.util.Scanner;

public class Demo02 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextline方式接受数据:");
        if (scanner.hasNextLine()){
            String str =scanner.nextLine();
            System.out.println("输出的内容为:"+str);
        }
    }
}


因为这句String str =scanner.nextLine();
所以如果输入hello world则会返回hello world

类似的方法,也可使用hasNextFloat()或者hasNextInt()等 对输入数据的类型进行判断。

比如:输入两次,系统需要识别用户输入的内容分别是integer还是float。并产生对应的输出反馈。如果用户输入非数字,提示输入错误,如果输入数字,则返回该数字。

package scanner;

import java.util.Scanner;

public class Demo04 {

    public static void main(String[] args) {
        int i=0;
        float f=0.0f;
        Scanner x =new Scanner(System.in);
        System.out.println("please enter the integer: ");
        if(x.hasNextInt()) {
            i=x.nextInt();
            System.out.println("you have entered the integer: "+i);
        }
        else {
            System.out.println("you didn't enter a proper integer");
        }

        System.out.println("please enter a float:");
        if(x.hasNextFloat()){
            f=x.nextFloat();
            System.out.println("you have entered the float, which is: "+f);
        }
        else {
            System.out.println("you didn't enter the float");
        }
        x.close();
    }

}

please enter the integer: 
56
you have entered the integer: 56
please enter a float:
78
you have entered the float, which is: 78.0

Process finished with exit code 0

举例 求一堆数字的平均值与和

//输入多个数字,求和与平均数,每输入一个数字,用回车确认,通过输入非数字来结束输入,并输出执行结果。

注意 这里用到了循环结构while

import java.util.Scanner;
public class testjan05{
    public static void main(String[] args) {
        System.out.println("please enter the numbers, use enter key after each numb and any other key to stop counting:"+"\n");
    Scanner x= new Scanner(System.in);
    int con=0;
    float num =0;
    float sum=0;
    float data =0;
    while (x.hasNextFloat()){
        con=con+1;
        num=x.nextFloat();
        sum=sum+num;
    }

    data=sum/con;
        System.out.println("you have entered "+con+" numbs and their sum is "+sum+" and average is "+data);
    }
}

输出结果:

please enter the numbers, use enter key after each numb and any other key to stop counting:

1
2
3
4
5
6
7
8
9
10
go
you have entered 10 numbs and their sum is 55.0 and average is 5.5

equal语句 判断A和B是否想等
B.equals("A")

import java.util.Scanner;
public class testjan05{
    public static void main(String[] args) {
        Scanner x= new Scanner(System.in);
        String s= x.next();
        if (s.equals("123")){
            System.out.println("hello dear!");}
        else
        { System.out.println("咒语错误,芝麻芝麻不开门");
        }
    }

}

switch语句
输入学生成绩A/B/C/D/或其他任何输入,返回
A-优秀
B-良好
C-合格
D-不及格
其它任意输入-输入不合法



import java.util.Scanner;
public class testjan05{
    public static void main(String[] args) {
        System.out.println("please enter the grade(A-D): "+"\n");
        Scanner x= new Scanner(System.in);
        if (x.hasNextLine()){
        String grade = x.nextLine();
        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;}
            default: {System.out.println("输入不合法");break;}

        }
        System.out.println();
    }
    }
}

输出结果

please enter the grade(A-D): 

niubi
输入不合法


http://lihuaxi.xjx100.cn/news/1985613.html

相关文章

Vue.js动画库

1、vue2-animate https://animate.style/ 地址:https://www.npmjs.com/package/vue2-animate一个可以在你的网站中即用型跨浏览器动画库,非常适合主页、滑块和动画引导提示。这是Animate.css 的一个端口,用于 Vue.js 2.0/3.0 和Alpines.js …

【系统DFX】如何诊断占用过多 CPU、内存、IO 等的神秘进程?

热门面试问题:如何诊断占用过多 CPU、内存、IO 等的神秘进程? 下图展示了 Linux 系统中有用的工具。 🔹’vmstat’ - 报告有关进程、内存、分页、块 IO、陷阱和 CPU 活动的信息。🔹’iostat’ - 报告系统的 CPU 和输入/输出统计信…

Redis应用(1)缓存(1.2)------Redis三种缓存问题

三者出现的根本原因是:Redis缓存命中率下降,请求直接打到DB上了。 一、 缓存穿透: 1、定义: 缓存穿透是指客户端请求的数据在缓存中和数据库中都不存在,这样缓存永远不会生效,这些请求都会打到数据库。…

github无法访问此网站,github.com 的响应时间过长。

问题 点击之前书签页中保存的去github搜集的项目连接,出现github无法访问此网站,github.com 的响应时间过长。 解决办法 1、打开浏览器,点击百度; 2、搜索hub.nuaa.cf; 3、点击第一项,如下所示&#xf…

学会使用ubuntu——ubuntu22.04使用WebCatlog

Ubuntu22.04使用WebCatlog WebCatlog是适用于Gnu / Linux,Windows或Mac OS X系统的桌面程序。 引擎基于铬,它用于在我们的桌面上处理Web服务。简单点就是把网页单独一个窗口出来显示,当一个app用。本文就是利用WebCatlog安装后的notion编写的…

aspose-cells-20.7.jar 去除水印及次数限制

1.使用 jd-gui.exe 反编译查看,直接搜索 License 1.修改 public static boolean isLicenseSet() {return (a ! null);}改成 public static boolean isLicenseSet() {return true;}2.修改 public void setLicense(InputStream stream) {Document document null;if (…

重温《深入理解Java虚拟机:JVM高级特性与最佳实践(第二版)》 –– 学习笔记(一)

第一部分:走近Java 第1章:走近Java 1.1 Java的技术体系 SUN 官方所定义的 Java 技术体系包括:Java程序设计语言、Java虚拟机、Class文件格式、Java API类库、第三方(商业机构和开源社区)Java类库。 其中&#xff0…

【MySQL】Char与VarChar详解

目录 长度申明 存储结构 超长处理 变长字段 排序规则 CHAR和VARCHAR类型相似,但它们在存储和检索方式上有所不同。它们在最大长度和是否保留尾随空格方面也存在差异。 长度申明 CHAR和VARCHAR类型的声明包含一个长度,该长度表示您希望存储的最大字…