public class StrDemo{ public static void main(String[] args){ String str1=”We are Students”; int n1=str1.length; int n2=str1.indexof(“students”) boolean b=str1.contains(“good”) System.out.println(n1); System.out.println(n2); System.out.println(b); }
时间: 2024-03-14 15:43:15 浏览: 24
这段代码有一些语法错误,应该修改为:
```
public class StrDemo {
public static void main(String[] args) {
String str1 = "We are Students";
int n1 = str1.length();
int n2 = str1.indexOf("students");
boolean b = str1.contains("good");
System.out.println(n1);
System.out.println(n2);
System.out.println(b);
}
}
```
这段代码的作用是定义一个 `StrDemo` 类,其中包含一个 `main` 方法。在 `main` 方法中,定义了一个字符串变量 `str1`,并获取了它的长度和包含子串 `"students"` 的位置。同时,使用 `contains` 方法判断字符串中是否包含子串 `"good"`,并将结果输出到控制台。
相关问题
1、(Java常见的异常类)编写一个Java程序,要求在 main() 分别用try-catch语句对以下三种异常进行捕获并显示输出:数组空指针异常,字符串空指针异常,数组下标越界异常; 2、(由方法抛出异常)编写一个Java程序,要求在程序中定义一个能抛出异常的方法,该方法接收三个参数,若第2个参数为0,则抛出算术异常,若第3个参数小于或等于0,则抛出空指针异常。在main()方法中捕获并处理该异常; 3、(主动抛出异常)编写一个Java程序,在程序中定义一个异常类,在main()方法中使用throw语句抛出异常; 4、(泛型类定义与方法的调用)编写一个Java程序,定义泛型类和泛型类的方法,然后创建一个泛型类对象,调用泛型类对象输出相应的结果; 5、(内部类)编写一个Java程序,要求在程序中定义一个School类,在School类中定义一个内部类Student,分别创建这两个类的对象,访问各个对象中的方法,输出各种结果; 6、(Lambda表达式)编写一个Java程序,用Lambda表达式实现接口中的方法;
1、Java程序如下:
```java
public class ExceptionDemo1 {
public static void main(String[] args) {
int[] arr = null;
String str = null;
try {
// 数组空指针异常
System.out.println(arr.length);
} catch (NullPointerException e) {
System.out.println("数组空指针异常:" + e.getMessage());
}
try {
// 字符串空指针异常
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("字符串空指针异常:" + e.getMessage());
}
try {
int[] array = {1, 2, 3};
// 数组下标越界异常
System.out.println(array[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界异常:" + e.getMessage());
}
}
}
```
2、Java程序如下:
```java
public class ExceptionDemo2 {
public static void main(String[] args) {
try {
int result = div(10, 0, -1);
System.out.println("result = " + result);
} catch (ArithmeticException e) {
System.out.println("算术异常:" + e.getMessage());
} catch (NullPointerException e) {
System.out.println("空指针异常:" + e.getMessage());
}
}
public static int div(int a, int b, int c) throws ArithmeticException, NullPointerException {
if (b == 0) {
throw new ArithmeticException("除数不能为0");
}
if (c <= 0) {
throw new NullPointerException("除数小于等于0");
}
return a / b * c;
}
}
```
3、Java程序如下:
```java
public class ExceptionDemo3 {
public static void main(String[] args) {
try {
throw new MyException("自定义异常");
} catch (MyException e) {
System.out.println(e.getMessage());
}
}
}
class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
```
4、Java程序如下:
```java
public class GenericDemo<T> {
private T data;
public GenericDemo(T data) {
this.data = data;
}
public T getData() {
return data;
}
public static void main(String[] args) {
GenericDemo<Integer> intDemo = new GenericDemo<>(123);
System.out.println(intDemo.getData());
GenericDemo<String> strDemo = new GenericDemo<>("Hello");
System.out.println(strDemo.getData());
}
}
```
5、Java程序如下:
```java
public class School {
private String schoolName = "Java School";
class Student {
private String name;
public Student(String name) {
this.name = name;
}
public void show() {
System.out.println("学校名称:" + schoolName + ",学生姓名:" + name);
}
}
public void addStudent(String name) {
Student student = new Student(name);
student.show();
}
public static void main(String[] args) {
School school = new School();
school.addStudent("Tom");
}
}
```
6、Java程序如下:
```java
public class LambdaDemo {
public static void main(String[] args) {
MyInterface myInterface = () -> System.out.println("使用Lambda实现接口方法");
myInterface.show();
}
}
interface MyInterface {
void show();
}
```
python 字符串逆序
在Python中,有多种方法可以实现字符串逆序。其中两种常见的方法是使用切片和使用循环。
第一种方法是使用切片操作,可以通过将字符串作为一个整体进行切片,使用步长为-1来实现逆序。例如,可以使用以下代码来逆序输出字符串:
```python
strDemo = 'pythontab.com'
reversed_str = strDemo[::-1]
print(reversed_str)
```
输出结果为:moc.batahnotyp
第二种方法是使用循环遍历字符串并构建一个新的逆序字符串。可以使用一个空列表来存储逆序后的字符,然后使用循环从字符串最后一个字符开始向前遍历,将每个字符添加到列表中,最后使用join()方法将列表中的字符连接起来。例如,可以使用以下代码来逆序输出字符串:
```python
strDemo = 'pythontab.com'
strList = []
for i in range(len(strDemo)-1, -1, -1):
strList.append(strDemo[i])
reversed_str = ''.join(strList)
print(reversed_str)
```
输出结果为:moc.batahnotyp
这两种方法都能够实现字符串逆序,选择哪种方法取决于个人的编程习惯和需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [python对指定字符串逆序的6种方法](https://blog.csdn.net/xiaoyaozi2020/article/details/117660803)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]