class StudyDay04

本文深入探讨了Java中的循环结构,包括for循环、while循环、do-while循环以及嵌套循环的使用方法。通过实例展示了如何控制循环次数,如何跳出循环,以及如何利用循环进行随机数生成。此外,还介绍了字符串转义字符的用法。


import java.util.Random;

public class StudyDay04 {
    public static void main(String[] args){
        for(int j=1;j<=100;j++){
            if (j==99) {
                continue;
            }
            System.out.println("Hello World!!"+"第"+j+"次");
        }
    }
    /*
    public static void main(String[] args){
        //写两个for循环,其中一个for循环B在for循环A里面B循环两次后终止A循环
        A:for(int i=0;i<5;i++){
            B:for(int j=0;j<5;j++){
                if (j>=2) {
                    break A;
                }
                System.out.println("Hello World!!");
            }
        }
    }
    */
    /*
    public static void main(String[] args){
        System.out.println("Hello\bWorld!!");//退格键,删除键一次
        System.out.println("Hello\tWorld!!");//制表符,tab一次
        System.out.println("Hello\nWorld!!");//换行,另起一行
        System.out.println("Hello\rWorld!!");//回车,标示回到第一表示
        System.out.println("Hello\\World!!");// 显示\
        System.out.println("Hello\'World!!");// 显示‘
        System.out.println("Hello\''World!!");// 显示‘’
        System.out.println("Hello World!!");
    }
    */
    /*
    public static void main(String[] args){
        //写一个for循环,要求循环6次
        for(int i=0;i<6;i++){
            System.out.println("Hello World!!");
        }
    }
    */
    /*
    public static void main(String[] args){
        //写一个for循环,要求是死循环
        for(int i=0;i<1;){
            System.out.println("Hello World!!");
        }
    }
    */
    /*
    public static void main(String[] args){
        //写一个do , while的循环,要求到底 第10此停止
        int i=1;
        do {
            Random random=new Random();//创建一个随机数
            int num=random.nextInt(21);//参数0-20之间的随机数
            System.out.println("随机数为"+num+";当前第"+i+"次");
            i++;
        } while (i<=10);
    }
    */
    /*
    public static void main(String[] args){
        //2.产生一个随机的数字,在[0-20]以内,包括20
        Random random=new Random();//创建一个随机数
        int num=random.nextInt(21);//参数0-20之间的随机数
        System.out.println("随机数为"+num);
    }
    */
    /*
    public static void main(String[] args){
        //1.写一个while循环,此循环到第九次就停止
        int i=0;
        while(i<9){
            System.out.println("Hello World!!"+i);
            i++;
        }
    }
    */
}

### Python 类的使用教程和基本语法 #### 创建类 在 Python 中,定义一个新的类通过 `class` 关键字来完成。一个简单的例子如下所示: ```python class MyClass: """这是一个简单的类""" pass ``` 这里定义了一个名为 `MyClass` 的空类[^3]。 #### 初始化方法与属性设置 为了给新创建的对象初始化状态,在类中可以定义一个特殊的方法叫做构造器或初始化方法 (`__init__`)。此方法会在每次实例化该类的时候自动调用,并允许传递参数以设定初始值。 ```python class Person: def __init__(self, name, age): self.name = name # 属性赋值 self.age = age ``` 上述代码片段展示了如何利用构造函数为 `Person` 对象分配两个属性:名字(`name`) 和年龄(`age`)[^4]。 #### 方法定义 除了属性外,还可以向类添加行为——即方法。这些是在对象上调用的功能。下面的例子增加了打印个人信息的方法: ```python class Person: def __init__(self, name, age): self.name = name self.age = age def introduce(self): # 定义方法 print(f"My name is {self.name}, I'm {self.age} years old.") ``` 现在可以通过创建 `Person` 实例并调用其 `introduce()` 方法来进行自我介绍: ```python person_example = Person('Alice', 30) person_example.introduce() ``` 这将会输出:“My name is Alice, I'm 30 years old.” #### 继承机制 Python 支持面向对象编程中的继承特性,使得子类能够从父类那里获得所有的公共属性和方法。实现方式很简单,只需要指定基类作为派生类的第一个参数即可。 ```python class Student(Person): # 子类Student继承自父类Person def study(self): print(f"{self.name} studies hard every day.") ``` 在这个例子中,`Student` 是 `Person` 的子类,因此它不仅拥有来自 `Person` 的所有功能,还额外具有自己特有的 `study` 行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值