// StringBuffer的概述
**// StringBuffer可以看作一个容器,创建之后里面的内容是可以变的 // 作用:提高字符串操作效率 **
import java.util.Scanner;
public class StringBufferDome { public static void main(String[] args) { // StringBuffer中的常用方法 // 创建对象 StringBuffer sb = new StringBuffer();
// 因为StringBuffer是java已经写好的类 // java在底层对他做了一些特殊处理 // 打印对象不是它的地址值而是它的属性值 System.out.println(sb);
StringBuffer sb1 = new StringBuffer("abc");
// 添加元素 sb1.append(1); sb1.append(2.3); sb1.append(true); System.out.println(sb1);
// 反转 sb1.reverse();
// 获取长度 System.out.println(sb1.length());
//toString的作用是把StringBuffer变回String String v = sb1.toString(); System.out.println(v);
// 链式编程 // 当我们在调用一个方法的时候,不需要变量接受它的结果,可以继续调用其他方法 int len = getString().substring(1).replace("A", "b").length(); System.out.println(len); }
public static String getString() { Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串"); String str = sc.next(); return str; } }
**练习:键盘输入一个字符串,程序判断该字符串是不是对称字符串,在控制台打出是或不是 **
import java.util.Scanner;
public class StringBufferDome1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串"); String str = sc.next();
String sb = new StringBuffer(str).reverse().toString(); if (sb.equals(str)) { System.out.println("字符串是对称字符串"); } else { System.out.println("字符串不是对此字符串"); } } }
String的截取
public class identity { public static void main(String[] args) { String id = "321281202001011234";
String year = id.substring(6,10); String month = id.substring(10,12); String day = id.substring(12,14);
char gender = id.charAt(16); //利用ASCII码表进行转换 int num = gender - 48;
System.out.println("你的个人信息为:"); System.out.println(year+"年"+month+"月"+day+"日");
if (num%2==0){ System.out.println("性别为:女"); }else{ System.out.println("性别为:男"); } } }
String金额转换String金额转换
import java.util.Scanner;
public class money { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个金额"); int money = sc.nextInt();
while(true){ if (money>=0 && money<=9999999){ break; }else{ System.out.println("无效金额"); break; } }
String moneyStr = ""; while(true){ int ge = money%10; String v = getmoney(ge); moneyStr = v + moneyStr; money = money/10; if (money == 0){ break; } }
int num = 7 - moneyStr.length(); for (int i = 0; i < num; i++) { moneyStr = "零"+moneyStr; }
String[] arr = {"佰","拾","万","仟","佰","拾","元",};
String result = ""; for (int i = 0; i < moneyStr.length(); i++) { char c = moneyStr.charAt(i); result = result + c + arr[i]; } System.out.println(result);
} public static String getmoney(int rmb){ String[] arr = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; return arr[rmb]; } }
String的比较
public class compare { public static void main(String[] args) { //字符串字节赋值再进行比较 String str1 = "abc"; String str2 = "abc"; //==比较的是两个字符串的地址值,这里的字符串会在串池里会产生复用,所以地址值相同 System.out.println(str1 == str2); //true
//用new创建字符串再进行比较 String str3 = new String("abc"); String str4 = "abc"; //用new创建的字符串会进入堆,不会产生复用 System.out.println(str3 == str4); //false //equals的作用的直接比较字符串中的值 System.out.println(str3.equals(str4)); //true
String str5 = new String("ABC"); String str6 = "abc"; //equalsIgnoreCase的作用是在比较时不区分大小写 System.out.println(str5.equalsIgnoreCase(str6)); //true } }
练习:用户登陆界面
import java.util.Scanner;
public class Dome1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String user = "zhangsan"; String password = "12345"; boolean v = false; for (int i = 0; i < 3; i++) { String user1 = sc.next(); String password1 = sc.next(); if (user.equalsIgnoreCase(user1) && password.equalsIgnoreCase(password1)){ System.out.println("登陆成功"); break; }else{ System.out.println("登陆失败,您还有"+(3-i)+"次机会"); } } } }
输入一个字符串,计算字符串中大写字母,小写字母,数组各有多少个
import java.util.Scanner;
public class Dome2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); int a = 0,b = 0,c = 0;
for (int i = 0; i < str.length(); i++) { char v = str.charAt(i); if (v >= 'a' && v<= 'z'){ a++; }else if (v >= 'A' && v <= 'Z'){ b++; }else if (v >= '0' && v <= '9'){ c++; } } System.out.println("小写字母有"+a+"个,大写字母有"+b+"个,数字有"+c+"个"); } }
String字符串的常见操作
String,StringBuilder,StringBuffer,pattern,Matcher
**String概述 java.lang.String类代表字符串,java程序中的所有字符串文字都以此类为对象 **String的注意点 字符串的内容是不好发生改变的,它的对象在创建后不能被更改
String是java定义好的一个类,定义在java.lang包中,所以使用的时候不需要导包
String的构造方法
public class gzFf { public static void main(String[] args) { //1.使用直接赋值的方式获取一个字符串对象 String s1 = "abc"; System.out.println(s1);
//2.使用new的方式来获取一个字符串对象 //空参构造,可以获得一个空白的字符串对象 String s2 = new String(); System.out.println("@"+s2+"!");//s2是空的
//传递一个字符串,再根据传递的字符串内容创建一个新的字符串对象 String s3 = new String("abc"); System.out.println(s3);
//传递一个字符数组,根据字符数组的内容再创建一个新的字符串对象 char[] arr = {'a','b','c','d'}; String s4 = new String(arr); System.out.println(s4); //可以把字符串录入字符数组,再进行改动 //abc --> {'a'b'c'} --> {'q'b'c'} --> qbc
//传递一个字节数组,根据字节数组的内容再创建一个新的字符串对象 byte[] arr1 = {97,98,99,100}; String s5 = new String(arr1); System.out.println(s5); } }
对象数组
public class phone { private String name;//品牌 private String color;//颜色 private double price;//价格 // public phone(){
}
public phone(String name,String color,double price){ this.name = name; this.color = color; this.price = price; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public static void main(String[] args) { //创建一个数组 phone[] arr = new phone[3]; //创建手机的对象 phone s1 = new phone("小米","白色",1999); phone s2 = new phone("华为","白色",4999); phone s3 = new phone("iQOO","黄色",3999);
//把手机存进数组里面 arr[0] = s1; arr[1] = s2; arr[2] = s3; //获取平均值 int sum = 0; for (int i = 0; i < arr.length; i++) { // i 索引 arr[i] 元素 (手机对象) phone phones = arr[i]; sum += arr[i].price; } System.out.println(sum/arr.length); } }
三元运算符
public class Dome {
public static void main(String[] args) {
int a = 10;
int b = 10;
//求两个数的最大值可以用三元运算符
int num = a>b?a:b;
System.out.println(num);
int h1 = 10;
int h2 = 20;
int h3 = 30;
int max = h1>h2?h1:h2;
int resut = h3>max?h3:max;
System.out.println(resut);
}
}
构造方法
特点: 1.方法名与类名相同,大小写也要一致 2.没有返回值类型,连void也没有 3.没有具体的返回值(不能由return带回结果数据)
执行时机: 1.创建对象的时候由虚拟机调用,不能手动调用构造方法 2.每创建一次对象,就会调用一次构造方法
构造方法定义: 1.如果没有定义构造方法,系统将会给出一个默认的无参数构造方法 2.如果定义了构造方法,系统将不再提供默认的构造方法
构造方法的重载: 1.带参构造方法和无参构造方法,两者方法名相同,但是参数不同,这叫构造方法的重载
public class gzFf { String name; int age; //系统默认会给出一个无参构造方法,无论用不用,建议写出来 public gzFf(){
}
//有参构造方法,用于给成员变量初始化的 public gzFf(String name,int age){ this.name = name; this.age = age; }
public String getName(){ return name; }
public int getAge(){ return age; } }
public class TestgzFf {
public static void main(String[] args) {
//要使用有惨构造方法,创建对象后面不能为空
gzFf sc = new gzFf("张三",18);
System.out.println(sc.name);
System.out.println(sc.age);
}
}
就近原则和this关键字
public class Sj4 {
//成员变量
private int age;
public void nl(int age){
//局部变量
int age = 10;
//谁离我近,我就用谁
System.out.println(age);
}
}
public class Sj4 {
private int age;
public void nl(int age){
//局部变量表示测试类中调用方法传递过来的数据
//等号的左边,就表示成员位置的age
this.age = age;
}
}
private的用法
1.private关键字是一个权限修饰符 2.可以修饰成员(成员变量和成员方法) 3.被private修饰的成员只能在本类中才能访问 4.针对private修饰的成员变量,如果需要被其他类使用,提供相应的操作 5.提高"setXxx(参数)"方法,用于给成员变量赋值,方法用public修饰 6.提高"getXxx(参数)"方法,用于获取成员变量的值,,方法有public修饰
public class npy { private String name; private String xb; private int nl;
public void setNl(int a){ if (a >= 18 && a <= 25){ nl = a; }else { System.out.println("非法参数"); } }
public int getNl(){ return nl; }
public void setName(String a){ name = a; } public String getName(){ return name; }
public void setXb(String a){ xb = a; } public String getXb(){ return xb; } }
测试类
public class TestNpy { public static void main(String[] args) { npy v = new npy();
v.setName("雅姿"); v.setNl(19); v.setXb("女");
System.out.println(v.getName()); System.out.println(v.getNl()); System.out.println(v.getXb()); } }
对象对象
public class rl1 {
//建一个新的类
String 姓名;
String 性别;
int 年龄;
double 身高;
double 体重;
String 出生年月;
//自动输出
public void 自我介绍(){
System.out.println(姓名+" "+性别+" "+年龄+" "+身高+" "+体重+" "+出生年月);
}
}
检测rl1类
//测试rl1类
public class TestPerson1 {
public static void main(String[] args) {
//创建对象
rl1 sc = new rl1();
//赋值
sc.姓名 = "张三";
sc.性别 = "男";
sc.体重 = 55.5;
sc.身高 = 1.8;
sc.年龄 = 18;
sc.出生年月 = "9月1号";
sc.自我介绍();
}
}
方法的使用
用方法遍历数组
public class A1 {
public static void main(String[] args) {
int arr[] = {11,22,33,44,55,66};
sum(arr);
}
//形参里放数组类型和数组名
public static void sum(int []arr){
for (int i = 0;i < arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
用方法求数组最大值
public class A1 {
public static void main(String[] args) {
int arr[] = {11,22,33,44,55,66};
int max = sum(arr);
System.out.println(max);
}
public static int sum(int[]arr){
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max){
max = arr[i];
}
}
System.out.println(max);
return max;
}
}
定义一个方法判断数组中的某一个数是否存在,将结果返回给调用处
public class A1 { public static void main(String[] args) { int arr[] = {11,22,33,44,55,66}; boolean p = sum(arr,12); System.out.println(p); }
public static boolean sum(int[] arr,int num){ for (int i = 0; i < arr.length; i++) { if (num == arr[i]){ return true; } } return false; }
}
拷贝数组
import java.util.Random;
public class Sj3 { public static void main(String[] args) { int[] arr = {1,2,3,4,5,6,7,8,9}; int[] arr1 = zeroTwo(arr,4,8); for (int i = 0; i < arr1.length; i++) { System.out.print(arr1[i]+" "); } } public static int[] zeroTwo(int[] arr,int a,int b){ int[] arr1 = new int[b - a]; for (int i = a,v = 0; i < b; i++,v++) { arr1[v] = arr[i]; } return arr1; } }
二维数组的定义及使用二维数组的定义及使用
public class kh3 {
public static void main(String[] args) {
//二维数组的定义方式
int[][] arr = new int[7][10];
for (int i = 0; i < arr.length; i++) {
//内层一维数组的长度
for (int j = 0; j < arr[i].length; j++) {
//循环赋值
arr[i][j] = 1;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
二维数组
定义一个二维数组,循环输入3个学生的java,c#,sql,html四门功课的成绩,二维数组的一行记录一个人的成绩
public class kh3 {
public static void main(String[] args) {
//二维数组的定义方式
int[][] arr = { {89,79,98,90},{99,80,100,98},{79,99,87,87} };
System.out.print("学生"+"\t\t"+"\tjava\tc#\t\tsql\t\thtml\t总分\t\t平均分");
System.out.println();
for (int i = 0; i < arr.length; i++) {
int sum = 0;
System.out.print("第"+(i+1)+"名");
for (int j = 0; j < arr[i].length; j++) {
System.out.print("\t\t"+arr[i][j]);
sum = sum + arr[i][j];
}
System.out.print("\t\t"+sum+"\t\t"+(sum/4));
System.out.println();
}
}
}
方法的重载
1,在同一个类中,定义了多个同名的方法,这些同名的方法具有同种功能 2,每个方法具有不同的参数类型或参数个数,这些同名的方法,就构成了重载关系 注:同一个类中,方法名相同,参数不同的方法,与返回值无关
public class Sj1 { public static void main(String[] args) { //整数默认为int类型,打印int类型方法 sum(10,20); System.out.println(""); //需要强制转换 sum((byte)10,(byte)20); System.out.println(""); sum((short)10,(short)20); System.out.println(""); sum((long)10,(long)20); } public static void sum(byte b1,byte b2){ System.out.println("byte类型"); System.out.println(b1 == b2); }
public static void sum(short s1,short s2){ System.out.println("short类型"); System.out.println(s1 == s2); }
public static void sum(int t1,int t2){ System.out.println("int类型"); System.out.println(t1 == t2); }
public static void sum(long g1,long g2){ System.out.println("long类型"); System.out.println(g1 == g2); } }
数组的排序
public class Sj3 {
public static void main(String[] args) {
int arr [] = {5,9,8,3,7,4};
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j] > arr[j+1]){
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
带返回值的方法的定义和调用带返回值的方法的定义和调用
public static 返回值类型 方法名 (){ 方法体; return 返回值; } 示例1:求一个商店一年四个季度的营业额
public class Sj3 {
public static void main(String[] args) {
//第一个季度的营业额
int q = zeroTwo(10,10,10);
//第二个季度的营业额
int w = zeroTwo(20,20,20);
//第三个季度的营业额
int e = zeroTwo(30,30,30);
//第四个季度的营业额
int r = zeroTwo(30,30,30);
int num = q + e + e + r;
System.out.println("全年的营业额为"+num);
}
public static int zeroTwo(int sum1,int sum2,int sum3){
int sum = sum1 + sum2 +sum3;
//返回给方法的调用处
return sum;
}
}
示例2:比较两个长方形的面积
public class Sj3 {
public static void main(String[] args) {
double num1 = zeroTwo(5.2,7.3);
double num2 = zeroTwo(4.6,8.9);
if (num1 > num2){
System.out.println("第一个长方形大");
}else{
System.out.println("第二个长方形大");
}
}
public static double zeroTwo(double height,double width){
double sum = height * width;
return sum;
}
}
带参数的方法
形参:全称形式参数,是指方法定义中的参数 实参:全称实际参数,方法调用中的参数 注意:方法调用时,形参和实参必须一一对应,否则程序将报错
public class Sj2 {
public static void main(String[] args) {
//实参
zeroTwo(5,4);
int u = 2;
int i = 2;
//实参
zeroTwo(u,i);
}
//形参
public static void zeroTwo(int a,int b){
int sum = a + b;
System.out.println(sum);
}
}
定义一个方法,求长方形的周长
public class Sj2 {
public static void main(String[] args) {
zeroTwo(5.2,1.3);
}
public static void zeroTwo(double a,double b){
double sum = (a + b ) * 2;
System.out.println(sum);
}
}
方法的定义及使用
1.什么是方法? 方法是程序中最小的执行单元 2.实际开发中,什么时候用到方法? 重复的代码,具有独立功能的代码可以抽取到方法中 3.实际开发中,方法有什么好处? 可以提高代码的复用性 可以提高代码的可维护性
public class Sj2 {
public static void main(String[] args) {
//方法的调用
playGam();
playGam();
}
//定义一个方法,需要在定义在main外。
public static void playGam(){
//输出
System.out.println("开局");
System.out.println("对线");
System.out.println("崩盘");
System.out.println("骂队友");
System.out.println("送人头");
System.out.println("GG");
}
}
数组值的交换
public class zy3 {
public static void main(String[] args) {
int arr[] = {1,3,5,7,9};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
for (int i = 0; i < arr.length/2; i++) {
int t = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = t;
}
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
}
数组值的打乱与交换
import java.util.Random;
public class A1 { public static void main(String[] args) { int arr[] = {1,2,3,4,5}; //循环遍历数组,从0索引开始打乱数组的数据 Random sc = new Random(); for (int i = 0; i < arr.length; i++) { //生成一个随机索引 int v = sc.nextInt(arr.length); //拿着随机索引指向的元素,跟i指向的元素进行交换 int p = arr[i]; arr[i] = arr[v]; arr[v] = p; } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } }
**java内存分配 1.栈 方法运行时使用的内存,比如main方法进行,进入方法栈中执行 2.堆 存储对象或者数组,new来创建的,都储存在堆内存 3.方法区 存储可以运行的class文件 4.本地方法栈 JVM在使用操作系统功能的时候用,和我们开发无关 5.寄存器 给cpu使用,和我们开发无关 **
阶乘的计算,动态数组
/*编写一个 Java 程序,用于计算并输出 1~10 的阶乘。*/
public class A1 {
public static void main(String[] args) {
int v = 1;
for (int a = 1;a <= 10;a++){
v *= a;
System.out.print("1");
for (int o = 2;o <= a;o++){
System.out.print("*"+o);
}
System.out.println("="+v);
}
}
}
动态数组 动态初始化:初始化时只指定数组长度,由系统为数组分配初始值 格式:数据类型 [] 数组名 = new 数据类型 [数组长度]; 范例:int arr = new int[3]; 使用动态数组录入五个同学的名字
public class Sj2 {
public static void main(String[] args) {
String arr [] = new String[5];
arr[0] = "张三";
arr[1] = "李四";
arr[2] = "王五";
arr[3] = "钱六";
arr[4] = "王八";
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
**数组默认初始值的规律 整数类型:默认初始值为0 小数类型:默认初始值为0.0 字符类型:默认初始值为'/u0000'空格 布尔类型:默认初始值false 引用数据类型:默认初始值null **
数组的使用
//利用索引对数组中的元素进行访问 //1.获取数组中的元素 //格式:数组名[索引] //索引:也叫做下标,角标 //索引的特点:从0开始,逐个+1增长,连续不间断
public class Sj2 {
public static void main(String[] args) {
int [] arr = {1,2,3,4};
//数组中的元素可以赋值给其他变量
int a = arr[0];
System.out.println(a);
//可以直接打印数组中的元素
System.out.println(arr[0]);
//也可以直接赋值给数组中的元素
arr[0] = 100;
System.out.println(arr[0]);
}
}
数组的遍历
public class Sj2 {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
练习:定义一个数组,遍历这个数组求到所有的元素,并求这个数组中能被3整除的的个数
public class Sj2 {
public static void main(String[] args) {
int sum = 0;
int arr [] = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < arr.length; i++) {
if (arr[i]%3 == 0){
System.out.println(arr[i]);
sum++;
}
}
System.out.println("能被三整除的个数有"+sum);
}
}
练习:定义一个数组 遍历这个数组得到每一个元素 如果是奇数,则将当前数组扩大两倍 如果是偶数,则将当前数子变成二分之一
public class A1 {
public static void main(String[] args) {
int arr [] = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println();
for (int i = 0; i < arr.length; i++) {
if (arr[i]%2 == 0){
arr[i]=arr[i]/2;
}else{
arr[i]=arr[i]*2;
}
System.out.println(arr[i]);
}
}
}
静态数组
public class Sj1 {
public static void main(String[] args) {
//格式:
//静态初始化
//完整格式:
//数据类型 [] 数组名 = new 数据类型 [] {元素1,元素2,元素3};
//简化格式:
//数据类型 [] 数组名 = {元素1,元素2,元素3};
//需求1:定义数组存储5个学生的年龄
int [] nl = new int[]{12,13,14,15,16};
int [] nl = {12,13,14,15,16};
//需求2:定义数组存储5个学生的姓名
String [] xm = new String[]{"张三","李四","王五","刘六","老八","三玖"};
String [] xm = {"张三","李四","王五","刘六","老八","三玖"};
//需求3:定义数组存储5个学生的身高
double [] sg = new double[]{1.85,1.95,1.75,1.74,1.68};
double [] sg = {1.85,1.95,1.75,1.74,1.68};
}
}
跳转控制语句
public class Sj1 {
public static void main(String[] args) {
for (int a = 1;a <= 100;a++){
if (a % 10 == 7 || a/10%10 == 7 || a%7 == 0){
System.out.println("过");
continue;
}
System.out.println(a);
}
}
}
用循环求平方根
import java.util.Scanner;
public class Sj2 { public static void main(String[] args) { Scanner zero = new Scanner(System.in); System.out.println("请输入一个数"); int a = zero.nextInt(); for (int v = 1;v < a;v++){ //用vv再来跟a比较 if (vv == a){ System.out.println(v+"是"+a+"的平方根"); break; }else if (v*v > a){ System.out.println(v+"是"+a+"的平方根的整数"); break; } } } }
用true和flase判断一个数是否为质数
import java.util.Scanner;
public class zy41 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个数"); int a = sc.nextInt(); boolean f = true; for (int b = 2;b < a;b++){ //如果a能够被b整除,则不为质数,输出false if (a%b == 0){ f = false; } } System.out.println(f); } }
判断一串数字是不是回文数
import java.util.Scanner;
public class A1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //输入一个数字 int a = sc.nextInt(); //用于记录a原来的值,,用于最后进行比较 int v = a; //记录倒过来之后的结果 int sum = 0; while(a != 0){ //从右到左获取每一位数字 int ge = a%10; //修改一下a的值 a = a / 10; //把当前获取的数字从左到右排序 sum = sum*10+ge; } System.out.println(sum); //进行比较 System.out.println(sum == v); } }
*在不使用/,%,的情况下,输出两个数相除的商和余数
import java.util.Scanner;
public class czxdcz { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入两个数"); //输入被除数 int a = sc.nextInt(); //输入除数 int b = sc.nextInt(); //用于表示商 int v = 0; //只要被除数大于或等于除数,就一直循环下去 while(a >= b){ a = a - b; v++; } System.out.println("商为"+v); System.out.println("余数为"+a); } }
while循环
循环三要素: 循环变量初始化 循环条件 循环体内变量的改变 循环代码
while循环用法 语法: while(布尔表达式){ //逻辑代码(循环操作) }
执行流程: 1.先对布尔表达式进行判断,结果为true,则执行逻辑代码。 2.本次执行完毕后,再进行判断,结果仍旧为true,则再次执行逻辑代码。 3.直至布尔表达式的结果为false时,才会退出循环结构,执行后续代码。
使用案例:
public class Sj1{
public static void main(String[] args){
//输出一百次你好,到一百次时结束
//循环变量
int a = 1;
while(a<=100){
System.out.println("第"+a+"遍你好");
//用于迭代
a++;
}
System.out.println("结束输出");
}
}
在while中套if
public class Sj1{
public static void main(String[] args){
//1到100的总和,奇数和,偶数和
int zs = 0,qs = 0,os = 0;
//循环变量
int a = 1;
while(a<=100){
zs += a; //总和
if (a%2==0){
os += a; //偶数和
}else{
qs += a; //奇数和
}
a++; //迭代
}
System.out.println("一到一百的总和是"+zs);
System.out.println("一到一百中偶数的和是"+os);
System.out.println("一到一百中奇数的和是"+qs);
}
}
循环嵌套:
//输出三行五列的*号
public class Sj1 {
public static void main(String[] args) {
//外层循环控制行数,内层循环控制列数
//外层循环循环一次,内存循环循环一轮
for (int a = 1;a <= 3;a++){
for (int v = 1;v <=5;v++){
System.out.print("*");
}
System.out.println();
}
}
}
循环嵌套的进阶用法
import java.util.Scanner;
public class Sj1 { public static void main(String[] args) { //计算三个班,每个班五名同学的平均成绩 Scanner sc = new Scanner(System.in); //外层循环控制班级数量 for (int a = 1;a <= 3;a++){ //这个变量用来包含班级总分数 int l = 0; //内层循环控制每个班学生的个数 for (int v = 1;v <=5;v++){ System.out.println("请输入第"+a+"个班级第"+v+"个同学的成绩"); l += sc.nextInt(); } System.out.println("第"+a+"个班级的平均分是"+l/5); } } }
用嵌套循环做直角三角形
public class Sj1 {
public static void main(String[] args) {
for (int a = 1;a <= 6;a++){
for (int v = 1;v <= a;v++){
System.out.print("*");
}
System.out.println("");
}
}
}
public class Sj1 {
public static void main(String[] args) {
for (int a = 1;a <= 6;a++){
for (int v = 1;v <= 6-a;v++){
System.out.print(" ");
}
for (int v = 1;v <= a;v++){
System.out.print("*");
}
System.out.println("");
}
}
}
模拟银行菜单
import java.util.Scanner;
public class Sj2 { public static void main(String[] args) { Scanner zero = new Scanner(System.in); System.out.println("欢迎使用Atm自助银行服务"); int a; do{ System.out.println("1.开户2.存款3.取款4.转账5.查询余额6.修改密码7.退出"); a = zero.nextInt(); switch(a){ case 0: System.out.println("谢谢使用,再见"); break; 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; default: System.out.println("输入选项有误,请重新输入"); } }while(a!=0); } }
break在循环中的用法:
import java.util.Scanner;
public class Sj1 { public static void main(String[] args) { Scanner zero = new Scanner(System.in); int zfs = 0; //这个变量记录目前正常输入分数的个数 int rs = 0; for (int a = 1; a < 5; a++) { System.out.println("请输入第"+a+"位同学的分数"); int fs = zero.nextInt(); //不合理的分数,小于0的分数 if (fs<0){ System.out.println("分数录入异常,停止录入"); //退出循环 break; } zfs += fs; rs++; } if (zfs==0){ System.out.println("本次分数无效录入"); }else{ System.out.println(rs+"位同学的平均分位"+(zfs/rs)); }
} }
continue在循环中的用法:
import java.util.Scanner;
public class Sj2 { public static void main(String[] args) { Scanner zero = new Scanner(System.in); int a = 1; for (;a <= 10;a++) { if (a == 10) { //跳出本次循环,跳到第三个表达式 continue; } System.out.println(a);//a = 10; } System.out.println(a);//a = 11; } }
continue的进阶用法
import java.util.Scanner;
public class Sj1 { public static void main(String[] args) { Scanner zero = new Scanner(System.in); //如果输入异常的分数,给它机会重新输入 int zfs = 0; int a = 1; for (;a < 5;a++) { System.out.println("请输入第"+a+"位同学的分数"); int fs = zero.nextInt(); if (fs<0){ System.out.println("分数录入异常,请重新输入"); //防止本次循环丢失 a--; continue; } zfs += fs; } System.out.println("平均分位"+zfs/a); } }
do while循环
语法 do{ 逻辑代码(循环操作) }while(布尔表达书)
**执行流程: 1.先执行一次循环操作之后,再进行布尔表达式的判断。 2.如果结果为true,则再次执行循环操作。 **
import java.util.Scanner;
public class Sj1{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a; do{ System.out.println("张三参加考试,考试成绩为:"); a = sc.nextInt(); }while(a<60); System.out.println("恭喜通过考试"); } }
import java.util.Scanner;
public class Sj1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = 1;
do{
System.out.println("第"+a+"遍Helloworld");
a++;
}while(a<=100);
System.out.println("结束输出");
}
}
for循环
特点:首次即有入口条件,先判断,再执行,适用于循环此数明确的情况。 import java.util.Scanner;
public class Sj1{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); //for循环和while循环相同,首次判断不满足,则一次都不会执行,(执行此数0~n次) //初始部分,只执行一次 for(int a = 1;a <= 100;a++){ System.out.println("第"+a+"遍Helloworld"); } System.out.println("结束输出"); } }
switch语句
import java.util.Scanner;、
public class A1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); //转为全小写 switch(a.toLowerCase()){ case 1: System.out.println("星期一"); //跳出switch break; case 2: System.out.println("星期二"); break; case 3: System.out.println("星期三"); break; //默认值 default: System.out.println("输入错误"); break; } } }
if条件语句
import java.util.Scanner;
public class A1
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt;
if(a<=100){ //条件语句,"如果"
System.out.println("这个数小于或等于100");
}else{ //"否则"
System.out.println("这个数大于100");
}
}
}
public class A1
public static void main(String[] args){
int a = 10;
char = a%3;
}
}