System.out.println(a.length);解释
时间: 2024-05-18 13:11:35 浏览: 8
这段代码是用来输出数组a的长度的。在Java中,数组是一种有序的数据结构,其长度是不可变的,即一旦数组被创建,其长度就不能再改变。因此,通过a.length可以获取数组a的长度,即数组中元素的个数。注意,这里的length是一个属性而不是一个方法,因此不需要在其后面加上括号。输出数组的长度可以帮助我们在代码中确定数组的大小,以便正确地操作数组。
相关问题
2.理解代码,写出执行结果。 System. out. println("b". matches("[abc]")); System. out. println("b". matches("[^abc]")); System. out. println("A". matches("[a-zA-Z]")); System. out. println("A". matches("[a-z[A-Z]]")); System. out. println("R". matches("[A-Z&&[RFG]]")); System. out. println("\n\t". matches("\\s{2}")); System. out. println("". matches("\\S")); System. out. println("3". matches("\\d")); System. out. println("&". matches("\\D")); System. out. println("a_8". matches("\\w{3}")); System. out. println("\n". matches(".")); System. out. println("\\u0041\\\\". matches("A\")); System. out. println("aaaa". matches("a*")); System. out. println("aaaa". matches("a+")); System. out. println("aaaa". matches("a?")); System. out. println("". matches("a?")); System. out. println("aaaa". matches("a{4}")); System. out. println("abcabcabc". matches("(abc){2,}")); System. out. println("4563456257". matches("\\d{3,10}")); 3.理解代码,写出程序功能。 String s="abc 123 abc1234abcabc"; String s1="abc"; int count=0; int index=0; while((index=s. indexOf(s1, index))!=-1){ index+=s1. length; count++; } System. out. println(count); 4、写出使用StringBuffer判断是否为回文串的代码? 5、利用Pattern和Matcher,查找字符串s (“123abcsfs123a1213c34sf32324f243aa45c c99”)中所有有连续数字(出现2次及以上,例如:123)的起始位置和对应的数字字符串。 思考(选做)找到字符串中出现的两位数。 6、购物小票内容如下: 牛奶:89.9元香肠:12.9元啤酒:69元巧克力:132元 要求使用StringTokenizer类,输出购物小票中的价格数据,并计算出菜单的总价格。
2. 执行结果:
true
false
true
false
false
true
false
true
false
true
true
false
true
true
true
false
true
true
3. 程序功能:统计字符串s中子串s1出现的次数。
4. 使用StringBuffer判断是否为回文串的代码:
```
public static boolean isPalindrome(String s) {
StringBuffer sb = new StringBuffer(s);
sb.reverse();
String reversed = sb.toString();
return s.equals(reversed);
}
```
5. 利用Pattern和Matcher,查找字符串s中所有有连续数字(出现2次及以上,例如:123)的起始位置和对应的数字字符串:
```
String s = "123abcsfs123a1213c34sf32324f243aa45cc99";
Pattern pattern = Pattern.compile("\\d{2,}");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println("起始位置:" + matcher.start() + ",数字字符串:" + matcher.group());
}
```
6. 输出购物小票中的价格数据,并计算出菜单的总价格:
```
StringTokenizer st = new StringTokenizer("牛奶:89.9元 香肠:12.9元 啤酒:69元 巧克力:132元", " ");
double totalPrice = 0.0;
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.contains(":")) {
String[] arr = token.split(":");
double price = Double.parseDouble(arr[1].replace("元", ""));
System.out.println(arr[0] + "的价格是:" + price);
totalPrice += price;
}
}
System.out.println("总价格:" + totalPrice);
```
package com.company; import java.util.Scanner; public class Demo001 { public static void main(String[] args) { Scanner mima=new Scanner(System.in); System.out.println("欢迎进入注册系统\n"); switch (true){ System.out.println("请输入用户名:"); String name=mima.next(); System.out.println("请输入密码:"); String password =mima.next(); System.out.println("请再次输入密码:"); String password1 =mima.next(); if (name.length()<3&&password.length()<6){ System.out.println("用户名长度不能小于3,密码长度不能小于6"); }else if (password.equalsIgnoreCase(password1)){ System.out.println("注册成功请牢记密码"); break; }else { System.out.println("两次密码需相同"); } } } }
这段代码中存在一些问题,以下是修改后的代码:
```java
package com.company;
import java.util.Scanner;
public class Demo001 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("欢迎进入注册系统\n");
while (true) {
System.out.println("请输入用户名:");
String name = input.next();
System.out.println("请输入密码:");
String password = input.next();
System.out.println("请再次输入密码:");
String password1 = input.next();
if (name.length() < 3 || password.length() < 6) {
System.out.println("用户名长度不能小于3,密码长度不能小于6");
} else if (!password.equals(password1)) {
System.out.println("两次密码需相同");
} else {
System.out.println("注册成功请牢记密码");
break;
}
}
}
}
```
以下是修改后的说明:
1. `Scanner` 对象的名称最好取得有意义一些,例如 `input`。
2. `switch` 语句的使用不符合语法规则,应该改为 `while (true)` 循环。
3. 检查用户名和密码长度的 `if` 语句中,应该使用逻辑运算符 `||`,因为当用户名或密码长度小于规定的长度时,都需要输出提示信息。
4. 在比较两次密码是否相同时,应该使用 `!password.equals(password1)`,因为 `equalsIgnoreCase()` 方法在比较字符串时不考虑大小写,而在此处需要考虑大小写。
5. 在输入密码时,最好使用 `input.nextLine()`,以避免输入的字符串含有空格而只取到空格前的部分。
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)