Java基础结构
Scanner
基础语法:Scanner s = new Scanner(System.in);
;
- 通过Scanner类的next()与nextLine()获取字符串
- 用hasNext()与hasNextLine()判断是否还有输入数据
- next()以空格作为结束符;nextLine()以换行符作为结束符
- 对输入有效字符之前遇到的空白,next()会将其自动去掉,nextLine()可以获得空白;
import java.util.Scanner;
public class ScanDemo01{
public static void main(String[] args) {
//创建一个扫描器,用于接收键盘数据;
Scanner s = new Scanner(System.in);
System.out.print("请输入:");
//判断是否有字符串输入;
if(s.hasNext()){
//使用next方式接受;
String str = s.next();
System.out.print("输入字符是:"+ str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要用完就关;
s.close();
}
}
import java.util.Scanner;
public class ScanDemo02 {
public static void main(String[] args) {
//从键盘接收数据;
Scanner scanner= new Scanner(System.in);
System.out.println("请输入:");
//判断是否还有输入;
if (scanner.hasNextLine()){
//使用nextLine方法;
String str= scanner.nextLine();
System.out.println("输入的字符是:"+str);
}
scanner.close();
}
}
- 整数与小数的输入
import java.util.Scanner;
public class ScanDemo03 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
//从键盘输入数据
int i = 0;
float f = 0.0f;
System.out.println("请输入整数数据:");
if(scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("输入的整数数据是: "+ i);
}else {
System.out.println("输入的不是整数数据。");
}
System.out.println("请输入小数数据:");
if(scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("输入的小数数据是: "+ f);
}else {
System.out.println("输入的不是小数数据。");
}
scanner.close();
}
}
- 求和计算
import java.util.Scanner;
public class SumDemo {
//输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输入结果;
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
Double sum= 0.0;
int count = 0;
System.out.println("请输入数据:");
while (scanner.hasNextDouble()){
Double d = scanner.nextDouble();
sum = sum + d;
count++;//count = count + 1;
System.out.println("输入的第 "+count+" 个数据是:" + d);
}
System.out.println("输入数据的总和是:" + sum);
System.out.println("输入数据的平均数是"+ sum/count);
}
}
顺序结构
JAVA的基本结构就是顺序结构,除非特别指明,否则就按顺序一句一句执行。
顺序结构是最简单的算法结构。
它是任何一种算法都离不开的一种基本算法结构。
选择结构
if结构
- if单选泽结构
语法:if(布尔表达式){ //如果布尔表达式为true将执行的语句 }
import java.util.Scanner;
public class IfDemo01 {
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();
}
}
- if双选择结构
语法:
if(布尔表达式){
//如果布尔表达式的值为true
}else {
//如果布尔表达式的值为false
}
import java.util.Scanner;
public class IfDemo02 {
//考试成绩大于60分就是及格,否则就是不及格;
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.println("请输入成绩");
Double score = scanner.nextDouble();
if (score<60){
System.out.println("不及格");
}else{
System.out.println("及格");
}
scanner.close();
}
}
- if多选择结构
语法:
if(布尔表达式1){
//如果布尔表达式1的值为true
}else if(布尔表达式2){
//如果布尔表达式2的值为true
}else if(布尔表达式3){
//如果布尔表达式3的值为true
}else{
//如果以上布尔表达式的值都不为true
}
package javaBase.struct.select;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.print("请输入成绩:");
Double score = scanner.nextDouble();
/*
if语句至多有一个else语句,else语句在所有的else if语句之后。
if语句可以有多个else if语句,它们必须在else语句之前。
一旦其中一个else if语句检测为true,其他的else if语句和else语句都将跳过执行
*/
if (score==100){
System.out.println("恭喜满分!");
}else if(score<100 && score>=90){
System.out.println("优秀");
}else if(score<90 && score>=80){
System.out.println("良好");
}else if(score<80 && score>=60){
System.out.println("及格");
}else if(score<60 && score>=0){
System.out.println("不及格");
}else{
System.out.println("输入的成绩不合法");
}
scanner.close();
}
}
- 嵌套的if结构
语法:
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}
}
例如寻找一个数,在1到100之间(二分法)
所有的流程语句都可以相互嵌套,而不互相影响;
Switch多选择结构
- switch case语句判断一个变量与一系列值中某个值是否相等,每个值为一个分支
- switch语句中的变量类型可以是byte、short、int、char、或者字符串String类型
- case标签必须为字符串常量或字面量
语法:
switch(expression){
case value:
//语句;
break; //可选;
case value:
//语句;
break; //可选;
//可以有任意数量的case语句;
default: //可选
//语句
}
public class SwitchDemo01 {
public static void main(String[] args) {
//switch 匹配一个具体的值
char grade = 'B';
switch (grade){
case 'A':
System.out.println("优秀");
break; //如果不写break语句,会发生case穿透;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("不及格");
break;
default:
System.out.println("未知登记");
}
}
}
循环结构
while循环
while是最基本的循环
- 只要布尔表达式为true,循环内容就会一直执行下去;
- 我们大多数情况是会让循环停止下来,需要一个让表达式失效的方式来结束循环;
- 少部分情况需要循环一直执行,比如服务器的请求响应监听,等待客户端连接等;
- 循环条件一直为true就会造成无限循环【死循环】,正常编程中应尽量避免。会影响程序性能或造成程序卡死崩溃;
语法:
while(布尔表达式){
//循环内容
}
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100;
int i = 0;
while (i<100){
i+=1;//i=i+1;| i++;
System.out.println(i);
}
}
}
应用:计算1+2+3+…+100=?
public class WhileDemo02 {
//计算1+2+...+100=?
public static void main(String[] args) {
int sum=0;
int i=0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println("1+2+3+...+100= "+sum);
}
}
do…while循环
- 对于while语句,如果不满足条件,则不能进入循环。但是有时候我们需要的即时不满足条件,也至少执行一次;
- do…while循环和while循环相似,不同的是,do…while循环至少执行一次
- while先判断后执行,dowhile先执行后判断
语法:
do{
//代码语句
}while(布尔表达式);
public class DoWhileDemo01 {
public static void main(String[] args) {
int sum = 0;
int i = 0;
do{
sum = sum + i;
i++;
}while(i<=100);
System.out.println("1加到100的值是:"+sum);
}
}
区别:
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while(a<0){
System.out.println(a);
a++;
}
System.out.println("===================");
do {
System.out.println(a);
a++;
}while (a<0);
}
}
for循环
- for循环
for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构;
for循环执行次数是在执行前就确定的 - 语法:
for(初始化;布尔表达式;更新){
//代码语句
} - IDEA快捷键:100.for
public class ForDemo01 {
public static void main(String[] args) {
int a = 1; //初始化条件
while (a<=100){ //条件判断
System.out.println(a); //循环体
a+=2; //迭代
}
System.out.println("while循环结束!");
//初始化值//条件//迭代
for (int i=1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束!");
/*
快捷键 100.for : for (int i = 0; i < 100; i++) {};
*/
/*
关于for循环有以下几点说明:
最先执行初始化步骤。可声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式,循环执行上面的过程。
*/
// 死循环:for( ; ; ){}
}
}
练习1:计算0到100之间奇数和偶数的和
public class ForDemo02 {
public static void main(String[] args) {
//计算0到100之间奇数和偶数的和
int evenum = 0;
int oddSum = 0;
for (int i=0; i<=100;i++){
if ((i%2)==0){
evenum = evenum+i;
}else {
oddSum = oddSum+i;
}
}
System.out.println("奇数和是:"+oddSum);
System.out.println("偶数和是:"+evenum);
}
}
练习2:用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个
public class ForDemo03 {
public static void main(String[] args) {
//用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个
for (int i = 1; i <= 1000; i++) {
if (i%5 == 0){
System.out.print(i+"\t"); //输出完不会换行
}
if(i%(5*3) == 0){
System.out.println(); //输出完会换行
// OR: System.out.print("\n");
}
}
}
}
练习3:打印九九乘法表
public class ForDemo04 {
public static void main(String[] args) {
//打印九九乘法表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j +"*"+i+"="+(j*i));
System.out.print("\t");
}
System.out.println();
}
}
}
/*
实现:
1. 打印第一列
2.将固定的1用循环包起来
3.去掉重复项,j<=i
4.调整样式
*/
- 增强型for循环
- 语法:
for(声明语句:表达式){
//代码语句
} - 声明语句:声明新的局部变量,该变量的类型必须和数组类型的元素匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50}; //定义了一个数组;
//遍历数组的元素;
for (int x: numbers){
System.out.println(x);
}
}
}
break continue
- break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强制退出循环,不执行循环中剩余的语句。
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i == 30){
break;
}
}
}
}
- continue语句用在循环语句体中,用于终止某次循环过程,及跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;//跳到循环开始的时候;
}
System.out.print(i+" ");
}
}
}