• Java笔记三


    包机制:

    为了更好地组织类,Java提供了包机制,用于区别类名的命名空间。

    包语句的语法格式为:pack pkg1[. pkg2[. pkg3...]];

     般利用公司域名倒置作为包名;如com.baidu.com,如图

     

     导包:

     为了能够使用某一个包的成员,需要在Java程序中明确导入该包。使用“import”语句可完成此功能

     import package[.package2...].(classname|*);

     JavaDoc

     生成自己的API文档

     

     ◆@author作者名

    ◆@version版本号

    ◆@since指明需要最早使用的jdk版本

    ◆@param参数名

    ◆@return返回值情况x BI UA.图三当您楼

    ◆@throws异常抛出情况

     Scanner对象

     我们可以通过Scanner类来获取用户的输入。

     ◆基本语法:`Scanner s = new Scanner(System. in);`

     

     可以看到输入的是Hello world但是输出的是Hello

     ◆通过Scanner类的next()与nextLine方法获取输入的字符串,在读取前我们一-般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

     

    1. import java.util.Scanner;
    2. public class work1 {
    3. public static void main(String[] args) {
    4. Scanner scanner=new Scanner(System.in);
    5. System.out.println("使用nextLine方式接收");
    6. //判断是否还有输入
    7. if (scanner.hasNextLine()){
    8. //使用next方式接收
    9. String str= scanner.nextLine();//程序会等待用户输入完毕
    10. System.out.println("输出的内容为:"+str);
    11. }
    12. scanner.close();
    13. }
    14. }

     

     

    next与nextLine的区别

    ◆next():

    ◆1、一定要读取到有效字符后才可以结束输入。

    ◆2. 对输入有效字符之前遇到的空白, next() 方法会自动将其去掉。

    ◆3.只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。

    ◆4、next()不能得到带有空格的字符串。

     ◆nextLine():

    ◆1、以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。

    ◆2、可以获得空白。

     进阶使用实例:

     

    1. import java.util.Scanner;
    2. public class work2 {
    3. public static void main(String[] args) {
    4. Scanner scanner=new Scanner(System.in);
    5. int i=0;
    6. float f=0.0f;
    7. System.out.println("请输入整数:");
    8. //如果...那么
    9. if (scanner.hasNextFloat()){
    10. i = scanner.nextInt();
    11. System.out.println("整数数据:"+i);
    12. }else {
    13. System.out.println("输入的不是整数数据!");
    14. }
    15. System.out.println("请输入小数:");
    16. //如果...那么
    17. if (scanner.hasNextFloat()){
    18. f = scanner.nextFloat();
    19. System.out.println("小数数据:"+f);}else {
    20. System.out.println("输入的不是小数数据!");
    21. }
    22. scanner.close();
    23. }
    24. }

     求和与平均值示例代码:

     

    1. public class day4 {
    2. public static void main(String[] args) {
    3. //输入多个数字,求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来借宿输入并输出执行结果
    4. Scanner scanner=new Scanner(System.in);
    5. //和
    6. double sum=0;
    7. //计算输入了多少个数字
    8. int m=0;
    9. //通过循环判断是否还有输入,并在里面对每一次进行求和和统计
    10. while (scanner.hasNextDouble()){
    11. double x=scanner.nextDouble();
    12. m=m+1;
    13. sum=sum+x;
    14. System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum);
    15. }
    16. System.out.println(m+"个数的和为"+sum);
    17. System.out.println(m+"个数的平均值是"+(sum/m));
    18. }
    19. }

     单选择结构if...else...实例

     

    1. public class work3 {
    2. public static void main(String[] args) {
    3. Scanner scanner=new Scanner(System.in);
    4. System.out.println("请输入内容:");
    5. String s=scanner.nextLine();
    6. //equals:判断字符串是否相等
    7. if (s.equals("Hello")){
    8. System.out.println(s);
    9. }else System.out.println("End");
    10. scanner.close();
    11. }
    12. }

     选择语句switch case实例

    1. public class work4 {
    2. public static void main(String[] args) {
    3. //case穿透 //switch匹配一个具体的值
    4. char grade='C';
    5. switch (grade){
    6. case 'A':
    7. System.out.println("优秀");
    8. break;//可选
    9. case 'B':
    10. System.out.println("良好");
    11. case 'C':
    12. System.out.println("及格");
    13. case 'D':
    14. System.out.println("再接再厉");
    15. case 'E':
    16. System.out.println("挂科");
    17. default://可选
    18. System.out.println("未知等级");
    19. }
    20. }
    21. }

     

     

     

     1+2+3+...+100=?

    while案例

     

    1. public class work5 {
    2. public static void main(String[] args) {
    3. //计算1+2+3+...+100=?
    4. int i=0;
    5. int sum=0;
    6. while (i<=100){
    7. sum=sum+i;
    8. i++;
    9. }
    10. System.out.println(sum);
    11. }
    12. }

     do...while案例

     

    1. public class work6 {
    2. public static void main(String[] args) {
    3. int i=0;
    4. int sum=0;
    5. do {
    6. sum=sum+i;
    7. i++;
    8. }while (i<=100);
    9. System.out.println(sum);
    10. }
    11. }

     while与do while的区别

     ◆while先判断后执行。dowhile是先执行后判断!

    ◆Do...while总是保证循环体会被至少执行一次!这是他们的主要差别。

     代码解释如下:

    1. public class work7 {
    2. public static void main(String[] args) {
    3. int a=0;
    4. while (a<0){
    5. System.out.println(a);
    6. a++;
    7. }
    8. System.out.println("=============");
    9. do {
    10. System.out.println(a);
    11. a++;
    12. }while (a<0);
    13. }
    14. }

     

    for实例

    实例一:100以内奇数与偶数的和

     

    1. public class work8 {
    2. public static void main(String[] args) {
    3. int oddSum=0;//奇数的和
    4. int evenSum=0;//偶数的和
    5. for (int i = 0; i <=100; i++) {
    6. if (i%2!=0){
    7. oddSum+=i;
    8. }else {
    9. evenSum+=i;
    10. }
    11. }
    12. System.out.println("技术的和:"+oddSum);
    13. System.out.println("偶数的和:"+evenSum);
    14. }
    15. }

     实例二:输出1-1000之间能被5整除的数,并且每行输出3个

     

    1. public class work9 {
    2. public static void main(String[] args) {
    3. for (int i = 0; i < 1000; i++) {
    4. if (i%5==0){
    5. System.out.print(i+"\t");
    6. }
    7. if (i%(5*3)==0){
    8. System.out.println();//每三个换一行
    9. //System.out.println("\n");
    10. }
    11. }
    12. }
    13. }

    println 输出完会换行

    print 输出完不会换行

    九九乘法表

     

    1. public class work11 {
    2. public static void main(String[] args) {
    3. for (int j = 1; j <= 9; j++) {
    4. for (int i = 1; i <= j; i++) {
    5. System.out.print(i + "*" + j + "=" + (j * i)+"\t");
    6. }
    7. System.out.println();
    8. }
    9. }
    10. }

     

    用两个循环使用两个乘数都增大,使用i

    增强for循环

     增强for循环语法格式如下:

     for(声明语句:表达式)
    {
        //代码句子
    }

    声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

    表达式:表达式是要访问的数组名,或者是返回值为数组的方法

     break与continue

     break:跳出这个循环但代码继续往下运行如:

    1. public class work13 {
    2. public static void main(String[] args) {
    3. int i=0;
    4. while (i<100){
    5. i++;
    6. System.out.println(i);
    7. if(i==30){
    8. break;
    9. }
    10. }
    11. System.out.println("123");
    12. }
    13. }

     

     continue:在循环语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定

     

    1. public class continuework {
    2. public static void main(String[] args) {
    3. int i=0;
    4. while(i<100){
    5. i++;
    6. if (i%10==0) {
    7. System.out.println("no");
    8. continue;
    9. }
    10. System.out.println(i);
    11. }
    12. }
    13. }

     

     打印三角形案例

     

    1. public class sanjiao {
    2. public static void main(String[] args) {
    3. for (int i = 1; i <= 5; i++) {//5行的一个三角
    4. for (int j = 5; j >= i; j--) {
    5. System.out.print(" ");
    6. }
    7. for (int j = 1; j <= i; j++) {
    8. System.out.print("*");
    9. }//打印左半个三角
    10. for (int j = 1; j < i; j++) {
    11. System.out.print("*");
    12. }//打印右半个三角
    13. System.out.println();
    14. }
    15. }
    16. }

     学习资源来自哔哩哔哩——遇见狂神说——狂神说Java

     

  • 相关阅读:
    【Linux】部署单体项目以及前后端分离项目(项目部署)
    知识点8--SSM项目整合redis、kafka、es以及整合es高亮
    车规级共模电感厂家教你贴片共模电感如何选型
    深究数据库E-R模型设计
    Mysql树形表的两种查询方案(递归与自连接)
    pymysql调用存储过程
    mysql比较时间
    正则表达式常用示例
    从零开始配置深度学习环境:CUDA+Anaconda+Pytorch+TensorFlow
    软件无线电系列——模拟无线电、数字无线电、软件无线电
  • 原文地址:https://blog.csdn.net/m0_73770225/article/details/133283703