zoukankan      html  css  js  c++  java
  • Java:基本语法

    Java语言是由类和对象组成的,其对象和类又是由变量和方法组成,而方法,又包含了语句和表达式。

    1. 变量

    Java语言提供了两种变量:成员变量和局部变量

    • 成员变量:是在方法体外的类中声明和定义的,可以被自动初始化
    • 局部变量:是在方法中声明和定义的,不能被自动初始化,方法执行完,局部变量也就不存在了

    在Java中,使用任何变量之前都需要对变量进行创建,创建变量实际上就是对变量的声明过程,需要指明变量类型和变量名。

    1 int a;
    2 boolean b;
    3 char c = 'A';
     1 public class DataDemo {
     2     
     3     int a;
     4     public void test()
     5     {
     6         boolean b=false;
     7         char c='';
     8     }
     9 
    10     public static void main(String[] args) {
    11         float f=0;
    12         String s=null;
    13 
    14     }
    15 
    16 }

    成员变量对应的自动初始化值:

    类型变量 初始值
    字节型 byte 0
    整型 int 0
    单精度型 fload 0.0f
    字符型 char 'u0000'
    字符串型 String null
    短整型 short 0
    长整型 long 0L
    双精度型 double 0.0d
    布尔型 boolean false

    2. 4类基本数据类型

    Java数据类型:

    • 布尔型
    • 整型: 整型,短整型,长整型,字节型
    • 字符型
    • 浮点型:单精度型,双精度型

    String不是基本数据类型。String类所定义的变量是一个对象,而不是简单类型。与简单类型不同,类的对象含有自己的方法,是复杂类型。

    布尔型(boolean),用于逻辑条件判断,只含两个值,真(true)、假(false)。需要注意的是,在C语言中,1和true等价,0和false等价,但在Java中,boolean变量的取值只可能是true或false。

    3. 算数运算符

    取模运算(%)中,若操作数包含正负数,则结果的正负号与左操作数一致。 例如: -8%3=-2, 8%(-3)=2

    4. switch语句

    public class SwitchTest {
    
        public static void main(String[] args) {
            int student[] = {95, 85, 75, 65, 55};
            for(int i=0; i<5; i++)
            {
                switch(student[i]/10)
                {
                    case 9:
                        System.out.println("Student" + i + "'s result is A!");
                        break;
                    case 8:
                        System.out.println("Student" + i + "'s result is B!");
                        break;
                    case 7:
                        System.out.println("Student" + i + "'s result is C!");
                        break;
                    case 6:
                        System.out.println("Student" + i + "'s result is D!");
                        break;
                    default:
                        System.out.println("Student" + i + "'s result is F!");
                        
                }
                    
            }
    
        }
    
    }

    Student0's result is A!
    Student1's result is B!
    Student2's result is C!
    Student3's result is D!
    Student4's result is F!

    public class SwitchTest {
    
        public static void main(String[] args) {
            int student[] = {95, 85, 75, 65, 55};
            for(int i=0; i<5; i++)
            {
                switch(student[i]/10)
                {
                    case 9:
                        System.out.println("Student" + i + "'s result is A!");
            
                    case 8:
                        System.out.println("Student" + i + "'s result is B!");
                        
                    case 7:
                        System.out.println("Student" + i + "'s result is C!");
                        
                    case 6:
                        System.out.println("Student" + i + "'s result is D!");
                        
                    default:
                        System.out.println("Student" + i + "'s result is F!");
                        
                }
                    
            }
    
        }
    
    }

    Student0's result is A!
    Student0's result is B!
    Student0's result is C!
    Student0's result is D!
    Student0's result is F!
    Student1's result is B!
    Student1's result is C!
    Student1's result is D!
    Student1's result is F!
    Student2's result is C!
    Student2's result is D!
    Student2's result is F!
    Student3's result is D!
    Student3's result is F!
    Student4's result is F!

    注意:当已经进入一个case分支,同时这个case语句中并没有使用break,那么以后的每个case都不用匹配就可以直接进入,知道遇到break为止。

    5. 实战练习

    • 使用for循环来实现对1~99之间奇数的求和
      import javax.swing.JOptionPane;
      
      
      public class OddSum {
      
          public static void main(String[] args) {
              int sum=0;
              for(int i=1; i<=99; i++)
              {
                  if(i%2!=0)
                  {
                      sum=sum+i;
                  }
              }
              JOptionPane.showMessageDialog(null, "The sum is "+ sum, "Total Even Integer from 1 to 99", JOptionPane.INFORMATION_MESSAGE);
      
          }
      
      }

    • switch语句与for循环结合
      public class TestSwitch
      {
          int i=0, w=0;
          public TestSwitch()
          {
              for(; i<=5; i++)
              {
                  switch (i)
                  {
                  case 3: w+=1;
                  case 0: w+=1;
                  case 1: w+=1; continue;
                  case 2: w+=1;
                  case 4: w+=1;
                  default:
                      w+=2;
                  }
                  System.out.print(" "+w);
              }
              
          }
      
          public static void main(String[] args) 
          {
              TestSwitch testSwitch=new TestSwitch();
          }
      
      }

       7 13 15

    • 利用多重for循环,用“*”绘制一个直角三角形,并使用消息对话框显示出来
    import javax.swing.JOptionPane;
    
    
    public class MultipleLoop1 {
    
        public static void main(String[] args) {
            String out="";
            loop:
                for(int row=1; row<=5; row++)
                {
                    out+="
    ";
                    for(int column=1; column<+6; column++)
                    {
                        if(column>row) continue loop;
                        out+="*  ";
                    }
                }
            JOptionPane.showMessageDialog(null, out, "Test multiply loop 1", JOptionPane.INFORMATION_MESSAGE);
    
        }
    
    }

    • 用“*”输出一个菱形并显示
    public class MultiplyLoop2 {
    
        public static void main(String[] args) {
            MultiplyLoop2 multiplyLoop2=new MultiplyLoop2();
            multiplyLoop2.print(11);
    
        }
        
        public void print(int n)
        {
            int temp=0;
            for(int i=0; i< n; i++)
            {
                for(int j=0; j<Math.abs(n/2-i);j++)
                {
                    System.out.print(" ");
                }
                if(i<=n/2)
                {
                    temp=i;
                }
                else
                {
                    temp=n-i-1;
                }
                for(int k=0; k<(2*temp +1); k++)
                {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    
    }

  • 相关阅读:
    底部导航栏
    判断手机是否连接网络
    瀑布流(圆角,卡片效果)
    列表卡片效果
    使用Glide改变图片的圆角
    条形码EAN-13码和EAN-8码的原理
    自定义底部弹窗
    【代码笔记】Java常识性基础补充(一)——赋值运算符、逻辑运算符、三元运算符、Scanner类、键盘输入、Random类、随机数
    【Android】9.0活动的生命周期(二)——实际代码演示
    【Android】8.0活动的生命周期(一)——理论知识、活动的启动方式
  • 原文地址:https://www.cnblogs.com/LilianChen/p/4559059.html
Copyright © 2011-2022 走看看