学习javaDay5
9.9包机制
- 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间。
- 包语言的语法格式为:
package pkg1[,pkg2[,pkg3...]];
- 一般利用公司域名倒置作为包名;com.kuangstudy.blog
- 为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包。使用"import"语句可完成此功能。
import package1[.package2....].(classname|*);
9.10JavaDoc
javadoc命令是用来生成自己API文档的
- 参数信息
10.流程控制
10.1用户交互Scanner
java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。
- 基本语法:
Scanner s = new Scanner(system.in);
-
通过Scanner类的nect()与nextLine()方法获取输入的字符串,在读取前我们一般需要 使用hasNext()与hasNextLine()判断是否还有输入的数据。
-
next():
? 1.一定要读取到有效字符后才可以结束输入。
? 2.对于输入有效字符之前遇见的空白,next()方法会自动将其去掉。
? 3.只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
? 4.next() 不能得到带有空格的字符串。
- nextLine():
- 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
- 可以获得空白。
import java.util.Scanner;
public class scanner {
public static void main(String[] args) {
//创建一个扫描器对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收: ");
//判断用户有没有输入字符串
/*
if (scanner.hasNext()){
//使用next方式接1收
String str = scanner.next();
System.out.println("输出的内容为: "+str);
}
*/
//判断是否还有输入
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输出的内容为: "+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
}
}
import java.util.Scanner;
public class scanner {
public static void main(String[] args) {
Scanner S = new Scanner (System.in);
System.out.println("请输入数据: ");
String str = S.nextLine();
System.out.println("输出的内容是: "+str);
}
}
Scanner用法例子:
import java.util.Scanner;
public class scanner3 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计算输入了多少个数字
int m = 0;
//通过循环判断是否还有输入,并在里面对每一次进行求和统计
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m+1;
sum = sum + x;
}
System.out.println(m + "个数的和为: " + sum);
System.out.println(m + "个数的平均值是" + (sum/m));
scanner.close();
}
}
10.2顺序结构
- JAVA 的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
- 顺序结构是最简单的算法结构。
10.3选择结构
10.3.1 If单选择结构
- 我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示。
-
语法:
If(布尔表达式){
//如果布尔表达式为true将执行的语句
//例子
import java.util.Scanner;
public class selectionStructure {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据: ");
String s = scanner.nextLine();
//equals:判断字符串是否相等
if (s.equals("hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
10.3.2If双选择结构
- 那现在有个需求,公司要收购一个软件,成功了,给人支付100万元,失败了,自己找人开发。这样的需求用一个if就搞不定了,我们需要有两个判断,需要一个双选择结构,所以就有了if - else结构。
- 语法:
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
10.3.3 If多选择结构
- 我们发现刚才的代码不符合实际情况,真实的情况还可能存在ABCD,存在区间多级判断。比如90100就是A,8090就是B...等等,在生活中我们很多时候的选择也不仅仅只有两个,所以我们需要一个多选择结构来处理这类问题。
- 语法:
if (布尔表达式1){
//如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}else if(布尔表达式3){
//如果布尔表达式3的值为true执行代码
}else{
//如果以上布尔表达式都不为true执行代码
}
//例子
import java.util.Scanner;
public class selectionStructure2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩: ");
int score = scanner.nextInt();
if (score==100){
System.out.println("恭喜满分");
}else if (score<100 && score>=90){
System.out.println("A级");
}else if (score<90 && score>=80){
System.out.println("B级");
}else if (score<80 && score>=70){
System.out.println("C级");
}else if (score<70 && score>=60){
System.out.println("D级");
}else if (score<60 && score<=0){
System.out.println("不及格");
}else{
System.out.println("成绩不合法");
}
}
}
10.3.4 嵌套的If结构
- 使用嵌套的if....else语句是合法的。也就是说你可以在另一个if或者else if语句中使用if或者else if 语句。你可以像if语句一样嵌套else if....else。
- 语法:
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}
}
10.3.5 switch多选择结构
- 多选择结构还有一个实现方式就是switch case语句。
- switch case 语句判断一个变量与一系列值中某个值是否相等。每个值称为一个分支。
switch 语句中的变量类型可以是:
- byte、short、int或者char。
- 从JavaSE7开始
- switch 支持字符串String类型了
- 同时case标签必须为字符串常量或字面量。
import java.util.Scanner;
public class switchStructure {
public static void main(String[] args) {
//case 穿透 //switch 匹配一个具体的值
Scanner scanner = new Scanner(System.in);
char grade = scanner.next().charAt(0);
//char grade = 'C';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("再接再厉");
break;
case 'F':
System.out.println("挂科");
break;
default:
System.out.println("未知等级");
}
scanner.close();
}
}
public class switchStructure2 {
public static void main(String[] args) {
String name = "狂神";
//JDK7的新特性 ,表达式结果可以是字符串!!!!
//字符的本质还是数字
//反编译 Java-----class(字节码文件)----反编译(IDEA)
switch (name){
case "秦江":
System.out.println("秦江");
break;
case "狂神":
System.out.println("狂神");
break;
default :
System.out.println("未知身份");
}
}
}
10.4循环结构
10.4.1 while循环
while是最基本的循环结构,它的结构为:
while(布尔表达式){
//循环内容
}
public class whileDemo01 {
public static void main(String[] args) {
//输出1~100
int i = 0;
int sum = 0;
/* while (i<100){
i++;
System.out.println(i);
*/
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
//(1)只要布尔表达式为true,循环就会一直执行下去。
//(2)我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
//(3)少部分情况需要循环一直执行,比如服务器的请求响应监听等。
//(4)循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。
10.4.2 do...while
do {
//代码语句
}while(布尔表达式)
/*
while和do-while的区别:
(1)While先判断后执行。dowhile是先执行后判断
(2)do...while总是保证循环体会被至少一次!这是他们的主要差别。
*/
public class whileDemo02 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
10.5 For循环结构
for(初始化;布尔表达式;更新){
//代码语句
}
public class forDemo02 {
public static void main(String[] args) {
//练习1:计算0到100之间的奇数和偶数的和
int oddSum = 0;//奇数和
int evenSum = 0;//偶数和
for (int i = 0; i < 100; i++) {
if (i%2!=0){
oddSum+=i;
}
else {
evenSum+=i;
}
}
System.out.println("奇数和为: "+oddSum);
System.out.println("偶数和为: "+evenSum);
}
}
public class forDemo03 {
public static void main(String[] args) {
//练习2: 用while 或for 循环输出1~1000之间能被5整除的数并且每行输出3个
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.printf(i+"\t");
}
if (i%(5*3)==0){
System.out.println();
//System.out.print("\n");
}
}
}
}
public class forDemo04 {
public static void main(String[] args) {
for (int j = 1; j <=9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
增强for循环:
for(声明语句 : 表代式)
{
//代码句子
}
/*
(1)声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
(2)表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
*/
10.6 break continue
(1)break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
(2)continue 语句用在循环体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
public class breakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==20){
break;
}
}
System.out.println(123);
}
}
public class continueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
continue;
}
System.out.println(i);
}
}
}
public class LabelDemo {
public static void main(String[] args) {
//打印101~150之间所有质数
//质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数
int count = 0;
outer:for (int i = 101;i<150;i++){
for (int j = 2;j= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}