以下name最终打印结果是什么 String name="123"; name += 1; System.out.println( name) ; A 123 B 1231 c 编译错误,String不可变 D 124
时间: 2024-03-31 19:32:18 浏览: 65
The final output will be "1231" (Option B).
In the given code, the variable "name" is initially assigned the value "123". Then, the "+=" operator is used to append the value "1" to the existing value of "name". This results in the string "1231". Finally, the value of "name" is printed using the "System.out.println()" method, which outputs "1231" to the console.
相关问题
public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); String[] name= new String[5]; String[] name1={}; for (int i=0;i<name.length;i++ ){ System.out.println("请输入"); String next = scanner.next(); name1[i]=next; System.out.println(name1); } System.out.println(name1);}那行不对
这段代码存在两个问题:
1. `name1` 的长度为 0,使用 `name1[i]` 会抛出 `ArrayIndexOutOfBoundsException` 异常。可以使用 `new String[5]` 来初始化 `name1` 的长度。
2. `System.out.println(name1)` 会输出 `name1` 数组的地址,而不是数组中的元素。可以使用 `Arrays.toString()` 方法将数组转换为字符串输出。
修改后的代码如下:
```java
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String[] name = new String[5];
String[] name1 = new String[5];
for (int i = 0; i < name.length; i++) {
System.out.println("请输入");
String next = scanner.next();
name1[i] = next;
System.out.println(Arrays.toString(name1));
}
System.out.println(Arrays.toString(name1));
}
}
```
public class Student { private String number;//学号 private String name;//姓名 private String sex;//性别 private String class1;//班级 public Student(String number, String name, String sex, String class1) {//有参构造方法 this.number = number; this.name = name; this.sex = sex; this.class1 = class1; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getClass1() { return class1; } public void setClass1(String class1) { this.class1 = class1; } public class void main add (String[] args) { Scanner in = new Scanner(System.in); Student[] stu = new Student[100]; int num = 0; while (true) { System.out.println("请输入要执行的操作:"); System.out.println("1.添加学生信息"); System.out.println("2.查看学生信息"); System.out.println("3.退出程序"); int choice = in.nextInt(); if (choice == 1) { if (num > stu.length - 1) { System.out.println("系统成员已满"); continue; } System.out.println("请输入学号"); String number = in.next(); for (int i = 0; i < num; i++) { if (stu[i].getNumber().equals(number)) { System.out.println("学号输入重复,请重新输入"); number = in.next(); i = -1; } } System.out.println("请输入姓名"); String name = in.next(); System.out.println("请输入性别"); String sex = in.next(); System.out.println("请输入班级"); String class1 = in.next(); Student student = new Student(number, name, sex, class1); stu[num] = student; num++; System.out.println("添加成功"); } else if (choice == 2) { if (num == 0) { System.out.println("没有学生信息"); continue; } System.out.println("学号\t姓名\t性别\t班级"); for (int i = 0; i < num; i++) { Student student = stu[i]; System.out.println(student.getNumber() + "\t" + student.getName() + "\t" + student.getSex() + "\t" + student.getClass1()); } } else if (choice == 3) { System.out.println("程序已退出"); break; } else { System.out.println("请输入正确的操作编号"); } } } } }中public class void main add (String[] args) {在jdk进行编译时标识符错误
在Java中,一个类只能有一个main方法,而且其签名必须是:
```
public static void main(String[] args)
```
您的代码中,出现了一个名为add的非法方法签名:
```
public class void main add (String[] args)
```
因此,在编译时出现了标识符错误。正确的写法应该是:
```
public static void main(String[] args)
```
请将您的代码改为以下形式:
```
public class Student {
// 省略其他代码
public static void main(String[] args) {
// 程序逻辑
}
}
```
阅读全文