网站自己怎么制作做网站用asp还是php
news/
2025/10/4 8:52:04/
文章来源:
网站自己怎么制作,做网站用asp还是php,亲子游网站怎么做,网站开发中遇到哪些问题3.1 表达式和语句 表达式一共分为三种#xff1a; #xff08;1#xff09;变量或常量 运算符构成的计算表达式 #xff08;2#xff09;new 表达式#xff0c;结果是一个数组或类的对象。#xff08;后面讲#xff09; #xff08;3#xff09;方法调用表达式… 3.1 表达式和语句 表达式一共分为三种 1变量或常量 运算符构成的计算表达式 2new 表达式结果是一个数组或类的对象。后面讲 3方法调用表达式结果是方法返回值或void无返回值。后面讲 表达式变量或常量 运算符构成的计算表达式 3.2 顺序结构 顺序结构就是程序从上到下行地执行。表达式语句都是顺序执行的。并且上一行对某个变量的修改对下一行会产生影响。 public class TestStatement{public static void main(String[] args){int x 1;int y 2;System.out.println(x x); System.out.println(y y); //对x、y的值进行修改x;y 2 * x y;x x * 10; System.out.println(x x);System.out.println(y y);}
} 3.3 输入输出语句 3.3.1 输出语句 1、两种常见的输出语句 换行输出语句 System.out.println(输出内容); 不换行输出语句 System.out.print(输出内容); 拼接输出语句 System.out.print(姓名 name ,age: age); 2、格式化输出 %d十进制整数 %f浮点数 %c单个字符 %bboolean值 %s字符串 3.3.2 输入语句 输入各种类型的数据 int num input.nextInt(); boolean b input.nextBoolean(); long big input.nextLong(); String str input.next(); char c input.next().charAt(0); input.close();//代码没有错误但是会造成JVM以外的操作系统相关内存没有得到释放。 键盘输入代码的四个步骤 1、申请资源创建Scanner类型的对象 2、提示输入xx 3、接收输入内容 4、全部输入完成之后释放资源归还资源 示例代码public class TestPrintlnAndPrint {public static void main(String[] args) {String name 小好;int age 18;//对比如下两组代码System.out.println(name);System.out.println(age);System.out.print(name);System.out.print(age);System.out.println(); //()里面为空效果等同于换行输出一个换行符//等价于 System.out.print(\n); 或 System.out.print(\n);//System.out.print();//错误()里面不能为空 核心类库PrintStream类中没有提供print()这样的方法//对比如下两组代码System.out.print(姓名 name ,);//中的内容会原样显示System.out.println(年龄 age);//中的内容会原样显示System.out.print(name name ,);System.out.println(age age);}
} 注意事项 换行输出语句括号内可以什么都不写只做换行处理不换行输出语句括号内什么都不写的话编译报错如果()中有多项内容那么必须使用 连接起来如果某些内容想要原样输出就用引起来而要输出变量中的内容则不要把变量名用引起 3.4 分支语句 3.4.1 单分支条件判断if if语句第一种格式 if if(条件表达式) 语句体; 执行流程 - 首先判断条件表达式看其结果是true还是false - 如果是true就执行语句体 - 如果是false就不执行语句体 3.4.2 双分支条件判断if...else if语句第二种格式 if...else if(关系表达式) { 语句体1; }else { 语句体2; } 执行流程 首先判断关系表达式看其结果是true还是false 如果是true就执行语句体1 如果是false就执行语句体2 3.4.3 多分支条件判断if...else if if语句第三种格式 if...else if ...else if (判断条件1) { 执行语句1; } else if (判断条件2) { 执行语句2; } 执行流程 首先判断关系表达式1看其结果是true还是false 如果是true就执行语句体1然后结束当前多分支 如果是false就继续判断关系表达式2看其结果是true还是false 如果是true就执行语句体2然后结束当前多分支 如果是false就继续判断关系表达式…看其结果是true还是false … 如果没有任何关系表达式为true就执行语句体n1然后结束当前多分支。 案例通过指定考试成绩判断学生等级成绩范围[0,100]- 90-100 优秀
- 80-89 好
- 70-79 良
- 60-69 及格
- 60以下 不及格import java.util.Scanner;public class Test11IfElseIf {public static void main(String[] args) {Scanner input new Scanner(System.in);System.out.print(请输入成绩[0,100]);int score input.nextInt();if(score0 || score100){System.out.println(你的成绩是错误的);}else if(score90 score100){System.out.println(你的成绩属于优秀);}else if(score80 score90){System.out.println(你的成绩属于好);}else if(score70 score80){System.out.println(你的成绩属于良);}else if(score60 score70){System.out.println(你的成绩属于及格);}else {System.out.println(你的成绩属于不及格);}input.close();}
}import java.util.Scanner;public class Test11IfElseIf {public static void main(String[] args) {Scanner input new Scanner(System.in);System.out.print(请输入成绩[0,100]);int score input.nextInt();if(score0 || score100){System.out.println(你的成绩是错误的);}else if(score90){System.out.println(你的成绩属于优秀);}else if(score80){System.out.println(你的成绩属于好);}else if(score70){System.out.println(你的成绩属于良);}else if(score60){System.out.println(你的成绩属于及格);}else {System.out.println(你的成绩属于不及格);}input.close();}
}3.4.4 if..else嵌套 在if的语句块中或者是在else语句块中 又包含了另外一个条件判断可以是单分支、双分支、多分支 案例从键盘输入一个年份值和月份值输出该月的总天数要求年份为正数月份1-12。例如输入2022年5月总天数是31天。import java.util.Scanner;public class Test12NestIfElse {public static void main(String[] args){//从键盘输入一个年份和月份Scanner input new Scanner(System.in);System.out.print(年份);int year input.nextInt();System.out.print(月份);int month input.nextInt();if(year0){if(month1 month12){//合法的情况int days;if(month2){if(year%40 year%100!0 || year%4000){days 29;}else{days 28;}}else if(month4 || month6 || month9 || month11){days 30;}else{days 31;}System.out.println(year年 month 月有 days 天);}else{System.out.println(月份输入不合法);}}else{System.out.println(年份输入不合法);}input.close();}
} 3.4.5 switch...case多分支选择结构 语法格式 switch(表达式){ case 常量值1: 语句块1; 【break;】 case 常量值2: 语句块2; 【break;】 。。。 【default: 语句块n1; 【break;】 】 } 避免case穿透 从键盘输入星期的整数值输出星期的英文单 import java.util.Scanner;public class Test13SwitchDemo1 {public static void main(String[] args) {//定义指定的星期Scanner input new Scanner(System.in);System.out.print(请输入星期值);int weekday input.nextInt();//switch语句实现选择switch(weekday) {case 1:System.out.println(Monday);break;case 2:System.out.println(Tuesday);break;case 3:System.out.println(Wednesday);break;case 4:System.out.println(Thursday);break;case 5:System.out.println(Friday);break;case 6:System.out.println(Saturday);break;case 7:System.out.println(Sunday);break;default:System.out.println(你输入的星期值有误);break;}input.close();}
} 利用case的穿透性 在switch语句中如果case的后面不写break将出现穿透现象也就是一旦匹配成功不会在判断下一个case的值直接向后运行直到遇到break或者整个switch语句结束switch语句执行终止。 练习根据指定的月份输出对应季 import java.util.Scanner;/** 需求指定一个月份输出该月份对应的季节。* 一年有四季* 3,4,5 春季* 6,7,8 夏季* 9,10,11 秋季* 12,1,2 冬季*/
public class Test14SwitchDemo2 {public static void main(String[] args) {Scanner input new Scanner(System.in);System.out.print(请输入月份);int month input.nextInt();/*switch(month) {case 1:System.out.println(冬季);break;case 2:System.out.println(冬季);break;case 3:System.out.println(春季);break;case 4:System.out.println(春季);break;case 5:System.out.println(春季);break;case 6:System.out.println(夏季);break;case 7:System.out.println(夏季);break;case 8:System.out.println(夏季);break;case 9:System.out.println(秋季);break;case 10:System.out.println(秋季);break;case 11:System.out.println(秋季);break;case 12:System.out.println(冬季);break;default:System.out.println(你输入的月份有误);break;}*/// 改进版switch(month) {case 1:case 2:case 12:System.out.println(冬季);break;case 3:case 4:case 5:System.out.println(春季);break;case 6:case 7:case 8:System.out.println(夏季);break;case 9:case 10:case 11:System.out.println(秋季);break;default:System.out.println(你输入的月份有误);break;}input.close();}
}if语句与switch语句比较 if语句的条件是一个布尔类型值if条件表达式为true则进入分支可以用于范围的判断也可以用于等值的判断使用范围更广。 switch语句的条件是一个常量值byte,short,int,char,枚举,String只能判断某个变量或表达式的结果是否等于某个常量值使用场景较狭窄。 当条件是判断某个变量或表达式是否等于某个固定的常量值时使用if和switch都可以习惯上使用switch更多。当条件是区间范围的判断时只能使用if语句。 另外使用switch可以利用穿透性同时执行多个分支而if...else没有穿透性。 3.5 循环语句 3.5.1 for循环 语句格式 for(初始化语句①; 循环条件语句②; 迭代语句④){ 循环体语句③ } 执行流程 第一步执行初始化语句①完成循环变量的初始化 第二步执行循环条件语句②看循环条件语句的值是true还是false - 如果是true执行第三步 - 如果是false循环语句中止循环不再执行。 第三步执行循环体语句③ 第四步执行迭代语句④针对循环变量重新赋值 第五步根据循环变量的新值重新从第二步开始再执行一遍 死循环 for(;;){ 循环体语句块//如果循环体中没有跳出循环体的语句那么就是死循环 } 3.5.2 关键字break 使用场景终止switch或者当前循环 3.5.3 while循环 while循环语句基本格式 while (循环条件语句①) { 循环体语句② } 死循环 while(true){ 循环体语句;//如果此时循环体中没有跳出循环的语句就是死循环 } 3.5.4 do...while循环 语句标准格式 do { 循环体语句① } while (循环条件语句②) 死循环 do{ 循环体语句;//如果此时循环体中没有跳出循环的语句就是死循环 }while(true); 3.5.5 循环语句的区别 从循环次数角度分析 do...while循环至少执行一次循环体语句 for和while循环先循环条件语句是否成立然后决定是否执行循环体至少执行零次循环体语句 如何选择 遍历有明显的循环次数范围的需求选择for循环 遍历没有明显的循环次数范围的需求循环while循环 如果循环体语句块至少执行一次可以考虑使用do...while循环 本质上三种循环之间完全可以互相转换都能实现循环的功能 三种循环结构都具有四要素 1循环变量的初始化表达式 2循环条件 3循环变量的修改的迭代表达式 4循环体语句块 3.5.6 循环嵌套 嵌套循环是指一个循环的循环体是另一个循环。比如for循环里面还有一个for循环就是嵌套循环。当然可以是三种循环任意互相嵌套。 3.5.7 关键字continue 使用场景提前结束本次循环继续下一次的循环 3.6 扩展 3.6.1 验证码 随机数用String拼接 String randomCode ;for (int i 0; i 6; i) {int randomNum (int) (Math.random() * 10);randomCode randomNum;}System.out.println(randomCode); 3.6.2 equal方法 代码 while(true){System.out.print(请输入您的性别);String str input.next();if(str.equals(男)||str.equals(女)||str.equals(保密)){System.out.println(您的性别str);break;}else{System.out.println(输入无效请重新输入);}} 注意 equals方法用于比较两个对象的内容是否相等。它是在Object类中定义的但通常需要被子类重写以提供具体的比较逻辑。 例对于两个字符串对象s1和s2如果它们包含相同的内容则s1.equals(s2)返回true即使这两个对象可能位于内存中的不同位置。 equals方法在Java中用于比较两个对象的内容是否相等与运算符在引用比较上的不同行为相区分。当需要比较对象内容是否相等时应使用equals方法并确保在自定义类中适当地重写该方法以提供正确的比较逻辑。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/926948.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!