发布时间:2024-11-13 16:01
变量的练习:
声明一个变量,一次声明多个变量。
声明变量直接初始化,先声明变量,而后再给变量初始化。
声明整型变量g,声明另一个整型变量h并赋值为h+10,输出变量h的值。
声明整型变量i,在i本身基础之上增10,输出变量i的值。
声明一些正确的和错误的变量名。
参考案例:
//变量的演示
public class VarDemo {
public static void main(String[] args) {
//1)变量的声明:----相当于在银行开帐户
int a; //声明一个整型的变量,名为a
int b,c,d; //声明三个整型的变量,名为b,c,d
//int a; //编译错误,变量不能同名
//2)变量的初始化:----相当于给帐户存钱
int e = 250; //声明整型变量e并赋值为250
int f; //声明整型变量f
f = 250; //给f赋值为250
//3)变量的使用:----使用的是帐户里面的钱
int g = 5;
int h = g+10; //取出g的值5,加10后,再赋值给变量h
System.out.println(h); //输出变量h的值15
System.out.println(\"h\"); //输出h,双引号中的原样输出
g = g+10; //在g本身基础之上增10
System.out.println(g); //输出g的值15
//int i = 3.14; //编译错误,数据类型必须匹配
//System.out.println(j); //编译错误,变量j未声明
int j;
//System.out.println(j); //编译错误,变量j未初始化
//4)变量的命名:
int a1,a_5$,_3c,$_b;
//int a*b; //编译错误,不能包含*号等特殊符号
//int 1a; //编译错误,不能以数字开头
int aa = 5;
//System.out.println(aA); //编译错误,严格区分大小写
//int class; //编译错误,不能使用关键字
//int k; //不直观,不建议
//int 年龄; //允许,但不建议
//int nianLing; //既不直观也不专业,必须杜绝
int age; //建议\"英文的见名知意\"
int score,myScore,myJavaScore; //建议\"小驼峰命名法\"
}
}
基本数据类型的练习:int
参考案例:
//1)int:整型,4个字节,-21个多亿到21个多亿
int a = 25; //25为整数直接量,默认为int类型
//int b = 10000000000; //编译错误,100亿默认为int类型,但超出int范围了
System.out.println(5/2); //2
System.out.println(2/5); //0
System.out.println(5/2.0); //2.5
int c = 2147483647; //int的最大值
c = c+1;
System.out.println(c); //-2147483648(int的最小值),发生溢出了,需要避免
基本数据类型的练习:long
参考案例:
//2)long:长整型,8个字节,很大很大很大
long a = 25L; //25L为长整型直接量,默认为long型
//long b = 10000000000; //编译错误,100亿默认为int类型,但超出int范围了
long c = 10000000000L; //100亿L为长整型直接量
long d = 1000000000*2*10L;
System.out.println(d); //200亿
long e = 1000000000*3*10L;
System.out.println(e); //不是300亿
long f = 1000000000L*3*10; //运算若有可能溢出,建议在第1个数字后加L
System.out.println(f); //300亿
基本数据类型的练习:double
参考案例:
//3)double:浮点型,8个字节,很大很大很大
double a = 3.14159; //3.14159为浮点数直接量,默认为double类型
float b = 3.14159F; //3.14159F为float型-----了解即可
double c=6.0,d=1.9;
System.out.println(c-d); //0.10000000000000009,有可能发生舍入误差
基本数据类型的练习:boolean
参考案例:
//4)boolean:布尔型,1个字节
boolean a = true; //true为布尔型直接量
boolean b = false; //false为布尔型直接量
//boolean c = 25; //编译错误,布尔型只能赋值为true或false
基本数据类型的练习:char
参考案例:
//5)char:字符型,2个字节
char c1 = \'女\'; //字符女
char c2 = \'f\'; //字符f
char c3 = \'6\'; //字符6
char c4 = \'*\'; //字符*
//char c5 = 女; //编译错误,字符型直接量必须放在单引号中
//char c6 = \'\'; //编译错误,必须有字符
//char c7 = \'女性\'; //编译错误,只能有一个字符
char c8 = \'\\\\\';
System.out.println(c8);
基本数据类型的转换:两种方式
参考案例:
int a = 5;
long b = a; //自动类型转换
int c = (int)b; //强制类型转换
long d = 5; //自动类型转换
double e = 5; //自动类型转换
long f = 10000000000L;
int g = (int)f;
System.out.println(g); //1410065408,强转有可能发生溢出
double h = 25.987;
int i = (int)h;
System.out.println(i); //25,强转有可能丢失精度
基本数据类型的转换:两点规则
参考案例:
byte b1 = 5;
byte b2 = 6;
byte b3 = (byte)(b1+b2);
System.out.println(2+2); //4
System.out.println(2+\'2\'); //52,2加上\'2\'的码50
System.out.println(\'2\'+\'2\'); //100,\'2\'的码50,加上\'2\'的码50
运算符的练习:算术
参考代码:
System.out.println(5%2); //1,商2余1
System.out.println(8%2); //0,商4余0----整除
System.out.println(2%8); //2,商0余2
//单独用----自增
int a=5,b=5;
a++; //相当于a=a+1
++b; //相当于b=b+1
System.out.println(a); //6
System.out.println(b); //6
//单独用----自减
int a=5,b=5;
a--; //相当于a=a-1
--b; //相当于b=b-1
System.out.println(a); //4
System.out.println(b); //4
//被使用----自增
int a=5,b=5;
int c = a++; //1)将a++的值5赋值给c 2)a自增1变为6
int d = ++b; //1)将++b的值6赋值给d 2)b自增1变为6
System.out.println(a); //6
System.out.println(b); //6
System.out.println(c); //5
System.out.println(d); //6
//被使用----自减
int a=5,b=5;
int c = a--; //1)将a--的值5赋值给c 2)a自减1变为4
int d = --b; //1)将--b的值4赋值为d 2)b自减1变为4
System.out.println(a); //4
System.out.println(b); //4
System.out.println(c); //5
System.out.println(d); //4
运算符的练习:关系
参考代码:
int a=5,b=10,c=5;
boolean b1 = b>a;
System.out.println(b1); //true
System.out.println(c<b); //true
System.out.println(a>=c); //true
System.out.println(b<=a); //false
System.out.println(b==c); //false
System.out.println(a!=c); //false
System.out.println(a+c>10); //false
System.out.println(b%2==0); //true
System.out.println(c++>5); //false------c自增1变为6
System.out.println(c++>5); //true-------c自增1变为7
参考代码:
int a=5,b=10,c=5;
//演示&&
boolean b1 = b>=a && b<c;
System.out.println(b1); //true&&false=false
System.out.println(b<=c && b>a); //false&&true=false
System.out.println(a==b && c>b); //false&&false=false
System.out.println(b!=c && a<b); //true&&true=true
//演示||
System.out.println(b>=a || b<c); //true||false=true
System.out.println(b<=c || b>a); //false||true=true
System.out.println(b!=c || a<b); //true||true=true
System.out.println(a==b || c>b); //false||false=false
//演示!
boolean b2 = !(a<b);
System.out.println(b2); //!true=false
System.out.println(!(a>b)); //!false=true
//演示&&的短路
int a=5,b=10,c=5;
boolean b3 = a>b && c++>2;
System.out.println(b3); //false
System.out.println(c); //5,发生短路了
//演示||的短路
boolean b4 = a<b || c++>2;
System.out.println(b4); //true
System.out.println(c); //5,发生短路了
参考代码:
int a = 5;
a += 10; //相当于a=(int)(a+10)
System.out.println(a); //15
a *= 2; //相当于a=(int)(a*2)
System.out.println(a); //30
a /= 6; //相当于a=(int)(a/6)
System.out.println(a); //5
short s = 5;
//s = s+10; //编译错误,需强转,改为: s=(short)(s+10);
s += 10; //相当于s=(short)(s+10)
参考代码:
//演示字符串连接
int age = 38;
System.out.println(\"age=\"); //age=
System.out.println(age); //38
System.out.println(\"age=\"+age); //age=38
System.out.println(\"我的年龄是\"+age); //我的年龄是38
System.out.println(\"我今年\"+age+\"岁了\"); //我今年38岁了
String name = \"WKJ\";
System.out.println(\"name=\"+name); //name=WKJ
System.out.println(\"大家好,我叫\"+name); //大家好,我叫WKJ
System.out.println(\"大家好,我叫\"+name+\",今年\"+age+\"岁了\"); //大家好,我叫WKJ,今年38岁了
//演示同化作用
System.out.println(10+20+\"\"+30); //3030---------String
System.out.println(\"\"+10+20+30); //102030-------String
System.out.println(10+20+30+\"\"); //60-----------String
参考代码:
int num = 5;
int flag = num>0?1:-1;
System.out.println(flag); //1
int a=8,b=5;
int max = a>b?a:b;
System.out.println(\"max=\"+max);
参考代码:
//1)偶数的判断:
int num = 6; //带数(6,5)
if(num%2==0){
System.out.println(num+\"是偶数\");
}
System.out.println(\"继续执行...\");
//2)满500打8折
//带数(600.0,300.0)
double price = 300.0; //消费金额
if(price>=500){ //满500
price*=0.8; //打8折
}
System.out.println(\"最终结算金额为:\"+price);
参考代码:
//if..else结构的演示
public class IfElseDemo {
public static void main(String[] args) {
//1)偶数、奇数的判断:
int num = 5; //带数(6,5)
if(num%2==0){
System.out.println(num+\"是偶数\");
}else{
System.out.println(num+\"是奇数\");
}
System.out.println(\"继续执行...\");
//2)满500打8折,不满500打9折:
double price = 300.0; //带数(600.0,300.0)
if(price>=500){ //满500,打8折
price*=0.8;
}else{ //不满500,打9折
price*=0.9;
}
System.out.println(\"最终结算金额为:\"+price);
}
}
参考代码:
package test;
public class Day03 {
public static void main(String[] args) {
/*
int a=8,b=5;
int max = a>b?a:b;
System.out.println(\"max=\"+max);
*/
int a=8,b=5;
int max;
if(a>b){
max = a;
}else{
max = b;
}
System.out.println(\"max=\"+max);
}
}
参考代码:
package test;
public class Day03 {
public static void main(String[] args) {
/*
闰年的判断公式:
1)4年一闰,百年不闰,就是:能被4整除,并且,不能被100整除
或者
2)400年再闰,就是:能被400整除
*/
int year = 2000; //年份 //带数(2021,2020,2000)
if((year%4==0 && year%100!=0) || year%400==0){
System.out.println(year+\"是闰年\");
}else{
System.out.println(year+\"是平年\");
}
}
}
参考代码:
package day04;
import java.util.Scanner;
//成绩等级判断
public class ScoreLevel {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"请输入成绩:\");
double score = scan.nextDouble();
//带数(888,-45,95,85,65,45)
if(score<0 || score>100){
System.out.println(\"成绩不合法\");
}else if(score>=90){ //合法
System.out.println(\"A-优秀\");
}else if(score>=80){
System.out.println(\"B-良好\");
}else if(score>=60){
System.out.println(\"C-中等\");
}else{
System.out.println(\"D-不及格\");
}
}
}
参考代码:
package day04;
import java.util.Scanner;
//命令解析程序
public class CommandBySwitch {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"请选择功能: 1.取款 2.存款 3.查询余额 0.退卡\");
int command = scan.nextInt();
switch(command){
case 1:
System.out.println(\"取款操作...\");
break;
case 2:
System.out.println(\"存款操作...\");
break;
case 3:
System.out.println(\"查询余额操作...\");
break;
case 0:
System.out.println(\"退出成功\");
break;
default:
System.out.println(\"输入错误\");
}
}
}
参考代码:
package day04;
import java.util.Scanner;
//猜数字小游戏
public class Guessing {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*1000+1); //1到1000之内
System.out.println(num); //作弊
//300(大),200(小),250(对)
System.out.println(\"猜吧!\");
int guess = scan.nextInt(); //1.
while(guess!=num){ //2.
if(guess>num){
System.out.println(\"太大了\");
}else{
System.out.println(\"太小了\");
}
System.out.println(\"猜吧!\");
guess = scan.nextInt(); //3.
}
System.out.println(\"恭喜你猜对了!\");
}
}
参考代码:
package day04;
import java.util.Scanner;
//猜数字小游戏
public class Guessing {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*1000+1); //1到1000之内
System.out.println(num); //作弊
//假设num=250
//300(大),200(小),250(对)
int guess;
do{
System.out.println(\"猜吧!\");
guess = scan.nextInt(); //1+3
if(guess>num){
System.out.println(\"太大了\");
}else if(guess<num){
System.out.println(\"太小了\");
}else{
System.out.println(\"恭喜你猜对了\");
}
}while(guess!=num); //2
}
}
参考代码:
package test;
import java.util.Scanner;
public class Day04 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"请输入一个整数:\");
int num = scan.nextInt();
if(num>0){
System.out.println(\"+\");
}else if(num<0){
System.out.println(\"-\");
}else{
System.out.println(\"0\");
}
}
}
参考代码:
package test;
import java.util.Scanner;
public class Day04 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"请输入年份:\");
int year = scan.nextInt();
System.out.println(\"请输入月份:\");
int month = scan.nextInt();
int days = 0; //天数
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if((year%4==0 && year%100!=0) || year%400==0){
days = 29;
}else{
days = 28;
}
}
System.out.println(year+\"年的\"+month+\"月共\"+days+\"天\");
}
}
参考代码:
package test;
public class Day04 {
public static void main(String[] args) {
int sum = 0; //和
int i = 1;
while(i<=100){
sum+=i;
i++;
}
System.out.println(\"sum=\"+sum);
}
}
参考代码:
package day05;
import java.util.Scanner;
//随机加法运算器
public class Addition {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score = 0; //得分
for(int i=1;i<=10;i++){ //10次 (1)25+36=?
int a = (int)(Math.random()*100); //加数a(0到99的随机数)
int b = (int)(Math.random()*100); //加数b(0到99的随机数)
int result = a+b; //存正确答案
System.out.println(\"(\"+i+\")\"+a+\"+\"+b+\"=?\"); //1)出题
System.out.println(\"算吧!\");
int answer = scan.nextInt(); //2)答题
if(answer==-1){ //3)判题
break;
}
if(answer==result){
System.out.println(\"答对了\");
score+=10; //答对1题加10分
}else{
System.out.println(\"答错了\");
}
}
System.out.println(\"总分为:\"+score);
}
}
参考代码:
package day05;
//九九乘法表
public class MultiTable {
public static void main(String[] args) {
for(int num=1;num<=9;num++){ //控制行
for(int i=1;i<=num;i++){ //控制列
System.out.print(i+\"*\"+num+\"=\"+i*num+\"\\t\");
}
System.out.println(); //换行
}
}
}
参考代码:
//声明整型数组arr,包含10个元素,每个元素都是int型,默认值为0
int[] arr = new int[10];
参考代码:
//2)数组的初始化:--初始化的是数组中的数据
int[] arr1 = new int[3]; //0,0,0
int[] arr2 = {2,5,8}; //2,5,8
int[] arr3 = new int[]{2,5,8}; //2,5,8
int[] arr4;
//arr4 = {2,5,8}; //编译错误,此方式只能声明同时初始化
arr4 = new int[]{2,5,8}; //正确
参考代码:
//3)数组的访问:--访问的是数组中的数据
int[] arr = new int[3];
System.out.println(arr.length); //3,输出arr的长度
arr[0] = 100; //给arr中第1个元素赋值为100
arr[1] = 200; //给arr中第2个元素赋值为200
arr[2] = 300; //给arr中第3个元素赋值为300
//arr[3] = 400; //运行时发生ArrayIndexOutOfBoundsException数组下标越界异常
System.out.println(arr[arr.length-1]); //输出最后一个元素的值
参考代码:
int[] arr = new int[10];
for(int i=0;i<arr.length;i++){ //遍历arr数组
arr[i] = (int)(Math.random()*100); //给每个元素赋值为0到99的随机数
System.out.println(arr[i]); //输出每个元素的值
}
参考代码:
package test;
public class Day05 {
public static void main(String[] args) {
int sum = 0;
for(int i=2;i<=100;i+=2){
sum+=i;
}
System.out.println(\"sum=\"+sum);
}
}
参考代码:
package test;
public class Day05 {
public static void main(String[] args) {
int num = 1; //阶乘
for(int i=1;i<=8;i++){
num*=i;
}
System.out.println(\"num=\"+num);
}
}
*
***
*****
*******
*********
***********
参考代码:
package test;
public class Day05 {
public static void main(String[] args) {
for(int i=1;i<=6;i++){
for(int j=1;j<=6-i;j++){ //空格
System.out.print(\" \");
}
for(int k=1;k<=2*i-1;k++){
System.out.print(\"*\");
}
System.out.println();
}
}
}
参考代码:
package day06;
import java.util.Arrays;
//求数组元素的最大值,并将其放在最后一个元素的下一个位置
public class MaxOfArray {
public static void main(String[] args) {
int[] arr = new int[10];
for(int i=0;i<arr.length;i++){
arr[i] = (int)(Math.random()*100);
System.out.println(arr[i]);
}
int max = arr[0]; //假设第1个元素为最大值
for(int i=1;i<arr.length;i++){ //遍历剩余元素
if(arr[i]>max){ //若剩余元素大于max
max = arr[i]; //则将max修改为较大的
}
}
System.out.println(\"最大值为:\"+max);
arr = Arrays.copyOf(arr,arr.length+1); //扩容
arr[arr.length-1] = max; //将最大值max放到最后一个元素上
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
参考代码:
package day06;
import java.util.Arrays;
//数组的演示
public class ArrayDemo {
public static void main(String[] args) {
//8)数组的排序:
int[] arr = new int[10];
for(int i=0;i<arr.length;i++){
arr[i] = (int)(Math.random()*100);
System.out.println(arr[i]);
}
Arrays.sort(arr); //对arr进行升序排列
System.out.println(\"数组排序后的数据:\");
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
System.out.println(\"倒序输出:\");
for(int i=arr.length-1;i>=0;i--){
System.out.println(arr[i]);
}
}
}
参考代码:
package day06;
//方法的演示
public class MethodDemo {
public static void main(String[] args) {
say();
sayHi(\"zhangsan\"); //String name=\"zhangsan\"
sayHello(\"zhangsan\",25); //String name=\"zhangsan\",int age=25
}
//无参无返回值
public static void say(){
System.out.println(\"大家好,我叫WKJ,今年38岁了\");
}
//有参无返回值
public static void sayHi(String name){
System.out.println(\"大家好,我叫\"+name+\",今年38岁了\");
}
//有参无返回值
public static void sayHello(String name,int age){
if(age>=35){ //在某种特定条件下,提前结束方法
return; //结束方法的执行
}
System.out.println(\"大家好,我叫\"+name+\",今年\"+age+\"岁了\");
}
}
参考代码:
package day06;
//方法的演示
public class MethodDemo {
public static void main(String[] args) {
double a = getNum(); //getNum()值就是return后那个数
System.out.println(a); //8.88,模拟对返回值的后续操作
int b = plus(5,6);
System.out.println(b); //11,模拟对返回值的后续操作
int m=5,n=6;
int c = plus(m,n); //传递的是m和n里面的数
System.out.println(c); //11,模拟对返回值的后续操作
int[] d = testArray();
System.out.println(\"长度为:\"+d.length); //10,模拟对返回值的后续操作
for(int i=0;i<d.length;i++){ //模拟对返回值的后续操作
System.out.println(d[i]);
}
}
//无参有返回值
public static double getNum(){
//return \"abc\"; //编译错误,返回值类型不匹配
return 8.88; //1)结束方法的执行 2)返回结果给调用方
}
//有参有返回值
public static int plus(int num1,int num2){
int num = num1+num2;
return num; //返回的是num里面的那个数
//return num1+num2; //返回num1与num2的和
}
//无参有返回值
public static int[] testArray(){
int[] arr = new int[10];
for(int i=0;i<arr.length;i++){
arr[i] = (int)(Math.random()*100);
}
return arr;
}
}
参考代码:
package test;
public class Day06 {
public static void main(String[] args) {
int[] arr = generate(5,20);
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
//生成随机数组方法
public static int[] generate(int len,int max){
int[] arr = new int[len];
for(int i=0;i<arr.length;i++){
arr[i] = (int)(Math.random()*(max+1));
}
return arr;
}
}
docker出现GPG error: At least one invalid signature was encountered 相关问题及解决方法
重读经典:《End-to-End Object Detection with Transformers》
Linux常用命令及使用,超级详细(记不住的记得收藏哦避免以后找不到)
路透社:谷歌已停止与华为部分合作;联想否认断供华为PC;微软计划直供Linux内核;谷歌无人机快递Wing进军芬兰……...
常见企业拓扑Cisco配置:三层交换机互联、DHCP配置、路由器交换机配置、NAT静态地址转换、DMZ区域的ACL配置
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace