Java是面向对象的语言,Java把一切内容(字符串,数字,布尔,日期,时间)都用对象表示
在Java世界中 变量的内容就是对象
String say = "Hello SuperXCR";重复赋值
String name;
name = "小明";
System.out.println(name);
name = "小红";
System.out.println(name);Java类型中 String 的值需要 “ “包围。
int 与long 的区别
int
最小值 : Integer.MIN_VALUE=-2147483648 (-2的31次方)
最大值 : Integer.MIN_VALUE=2147483647 (2的31次方-1)
long
最小值 : Long.MIN_VALUE=-2147483648 (-2的63次方)
最大值 : Long.MIN_VALUE=2147483647 (2的63次方-1)
注意long类型赋值的时候要在数字最后跟上一个小写的 l (是字母L)
long num = 78900000123l;比如说
// 是否允许
boolean isAllow = ture;可以通过 变量+数字 来完成计算
int second = 1*60*60;
System.out.println("second");
//也可以
int unit = 60;
int hour = 1;
int second = hour * unit * unit;
System.out.println(second);
//计算平均分
int total = 0;
int u1 = 80;
total=total+u1;
int u2 = 90;
total=total+u2;
int u3 = 50;
total=total+u3;
int u4 = 65;
total=total+u4;
int average = total/4;
System.out.println(average);//获取一个随机数
double value = Math.random();
//Math.random()方法返回一个随机值 随即值的范围是0.0 =<Math.random<1.0
System.out.println(value);double value = Math.random();
int nValue = (int) (value*10);
System.out.println(nValue);// public(公共的) static(静态) void(空类型)
public static void 方法名称(方法参数){
代码块
}
小驼峰
方法名遵守的是小驼峰
// 密码
public static void password(){
//代码块
}
// 我的文档
public static void myDocument(){
//代码块
}
方法调用
public static void main(String[] args) {
System.out.println("测试");
newLine();
newLine();
System.out.println("结束");
}
public static void newLine (){
newWord();
System.out.println("Hello");
}
public static void newWord (){
System.out.println("-----------");
}
//在main函数下;
{
code( "Apple");
}
public static void code(String food){//就是指定变量类型;
System.out.println(code);
}
图片示例
声明变量
传递变量
//直接传值
random(100000);
//传递变量
int len = 100000;
random(len);
代码示例
public class MessageCode {
public static void main(String[] args) {
String text = "亲爱的SuperXCR用户,你本次的验证码是";
code(text,1000);//形参之间用 ,隔开;
}
public static void code(String text,int len){
int code = (int)((Math.random()+1)*len);
System.out.println(text+"【"+code+"】");
}
}
关键字 if 后括号中的代码叫做:条件表达式 如果为真,大括号中的语句就会执行;反之,则什么都不会发生;
if(表达式){
代码块
}
//条件表达式可以包含任意的比较比较运算符
x == y // x 等于 y
x != y // x 不等于 y
x > y // x 大于 y
x < y // x 小于 y
x >= y // x 大于等于 y
x <= y // x 小于等于 y
//注意
Java中 = 符号是赋值运算符,这个我们在变量那个章节里学习到过。== 是判断是否相同的比较运算符
Java中没有 =< 和 => 这两种写法,这是错误的
此外, Java中比较运算符的左边和右边的对象类型必须一样的,int整数只能和int整数比较,双精度double只能和double比较
也就是if else语句
if(条件表达式){
代码语句
}else{
代码语句
}
//奇数偶数判断
public class Condition{
public static void main(String[] args){
number(4);
number(5);
}
public static void number(int x){
if(x % 2 == 0){
System.out.println(x+"是偶数");
}else{
System.out.println(x+"是奇数");
}
}
}
判断链也就是通过一层一层的判断
public static void report(String name,double score){
if(score >= 90){
System.out.println(name+",你本次的成绩为优秀");
} else if(score >= 80){
System.out.println(name+",你本次的成绩为良好");
} else if(score >= 60){
System.out.println(name+",你本次的成绩为及格");
} else {
System.out.println(name+",你本次的成绩为不通过");
}
}
嵌套就是在 if else 中嵌套if else
代码示例如下
public class StudentReport {
public static void main(String[] args){
report("小明",80);
report("小王",90);
report("小四",70);
report("小五",50);
report("小六",40);
}
public static void report(String name,double score){
if(score >= 60){
message(name);
if(score >= 90){
System.out.println(name+",你本次的成绩为优秀");
} else if(score >= 80){
System.out.println(name+",你本次的成绩为良好");
}else{
System.out.println(name+",你本次的成绩为及格");
}
} else {
System.out.println(name+",你本次的成绩为不通过");
}
}
public static void message(String name){
System.out.println(name+"恭喜你成功进阶");
}
}
public static void plan(double temperature){
System.out.println("准备出门去学校");
if(temperature>38){
// 体温超过38度,属于发烧,忌运动
return;
}
System.out.println("去操场踢球");
}
返回语句也就是通过if判断后,如果进入if语句后,在return后就不会在执行下面的语句
一个方法也可以调用它本身,这叫做递归,这种方法称为递归方法;
递归实例
public class CountDown {
public static void main(String[] args) {
count(3);
}
public static void count(int number) {
System.out.println(number);
number = number - 1;
if (number == 0) {
System.out.println("程序执行完毕");
return;
}
count(number);//递归用法;
}
}
random这个方法Java源代码
public static double random() {
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
关于return的知识点
//return对应的类型相同
public static int result(){//此处为整型,还可以为String,double,boolean
return "整型";
}
public class Captcha{
public static void main(String[] args){
// 得到验证码
int codeValue = code(1000);
// 如果 codeValue 在1000和9999之间,那么就是正确的4位随机数
if(codeValue >= 1000 ){
if(codeValue <= 9999){
System.out.println("本次的验证码是:" + codeValue);
}
}
}
/**
* 得到验证码
*/
public static int code(int len){
// 得到随机数结果
double val = Math.random();
// 随机数结果*9+1 然后再乘以位数得到验证码
int result = (int)((val * 9 + 1) * len);
return result;
}
}
public class BonusDemo{
public static void main(String[] args){
int amount = bonus(0);
System.out.println(amount);
amount = bonus(80000);
System.out.println(amount);
amount = bonus(280000);
System.out.println(amount);
amount = bonus(500000);
System.out.println(amount);
}
/**
* 计算奖金
*/
public static int bonus(int amount){
if (amount<100000){
return amount*10/100;
} else if(amount>300000){
return amount*7/100;
} else if(amount>=100000){
return amount*9/100;
} else {
return 0;
}
}
}
if(x>0 && x <10){
// 表示 x 大于0 并且 x小于10时才为真
}
if(x == 60 || x == 80){
// 表示 x 等于60 或者 x 等于80 都为真
}
if(x!=0){
// 表示x不等于0的时候
}
//可以用 !对boolean进行否定;
boolean isPass = false;
if(!isPass){
System.out.println("通过考试");
}
if(x == 60 || x == 80 || x == 90){
// 表示 x等于60 或者x等于80 或者x等于90为真
}
//自减,就是
number = number - 1;
number --;
//自增
number = number + 1;
number ++;
int[ ] 表示”整型数组”类,String[ ] 表示”字符串”
// 声明一个 int 数组的变量,数组大小为6
int[] numbers = new int[6];
创建好数组变量后,可以借助[下标]来完成操作
// 声明一个 int 数组的变量,数组大小为6
int[] numbers = new int[6];
numbers[0] = 1;
numbers[1] = 3;
numbers[2] = 5;
numbers[3] = 7;
numbers[4] = 9;
numbers[5] = 11;
获取某一数组可以用:
// 得到数组中第三个数字
int num = numbers[2];
System.out.println(numbers);
长度
public static void main(String[] args) {
int[] numbers = new int[8];//数组定义方式;
int size = numbers.length;
System.out.println(size);
}
//size可以计算数组长度
用代码解释
String[] tables = new String[3];
tables[0] = "张三";
tables[1] = "李四";
tables[2] = "王五";
for(int i;i<names.length;i++){//利用for循环输出
String name =tables[i];
System.out.println(name);
}
//也可以通过如下来赋值数组;
String[] tanles = new String[]{"张三","李四","王五"};
for(int i = 0; i < tables.length; i++){
String name = tables[i];
}
//也可以这样输出
for(String tanle : tanles) {
System.out.println(tanle);
}
//缺点,无法输出位置;
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
String line = "";//从0开始存储数据;
for (int j = 1; j <= i; j++) {
// 拼接line字符串,打印出 类似 1*2=2 2*2=4 这样的效果
line = line + j + "*" + i + "=" + j * i + " ";
}
System.out.println(line);
}
}