system.out.println("aa"+"bb"+"cc"="aabbcc")对吗
时间: 2024-01-29 07:10:15 浏览: 26
对于这个问题,"system.out.println("aa"+"bb"+"cc"="aabbcc")"是错误的语法。在Java中,"+"用于连接字符串,而不是用于赋值。正确的语法应该是:
```java
System.out.println("aa" + "bb" + "cc" + "=" + "aabbcc");
```
这样会将字符串"aa"、"bb"、"cc"和"="连接起来,并输出结果"aabbcc"。
相关问题
class Father { int a = 100; int b = 200; public void print() { System.out.println(a + " " + b); } } class Child extends Father { int b = 300; int c = 400; public void print() { System.out.println(a + " " + b + " " + c); } public void printExtend() { System.out.println(c); } } public class Main { public static void main(String[] a) { Father obj = new Child(); System.out.println(obj.a); / AA语句 System.out.println(obj.b); // BB语句 obj.print(); // CC语句 obj.printExtend(); // DD语句 } }
这段代码会输出什么?
答案:
AA语句会输出 100,因为 Father 类的 a 变量被继承并赋值为 100,而子类 Child 中没有对 a 进行重写或者隐藏。
BB语句会输出 200,因为在 Child 类中定义了一个与父类同名的 b 变量,这个变量会隐藏父类的 b 变量,所以 obj.b 会访问到父类的 b 变量。
CC语句会输出 100 300 400,因为 obj 是一个 Father 类型的引用,但是实际引用的是 Child 类的一个对象,所以调用 print 方法时会调用 Child 类中的 print 方法,从而输出 Child 类中的 a、b、c 变量。
DD语句会编译错误,因为 Father 类中没有 printExtend 方法,而 obj 引用的类型是 Father 类型,无法调用子类中的方法。
学习完异常处理后,我们可以将数据校验放在一起做。从此不必输入一个,校验一个。 这样的编程风格不好,容易打断业务流程的描述,特别是数据输入较多的时候。 请按照Main类及输出案例 1. 编写抽象类Pet,包含构造方法, 抽象方法eat和setNameAge及成员变量:name, age; 2. 编写Dog类(Pet类的子类)。 3. 编写AgeException异常类,当年龄不大于零,抛出该类异常(Dog类中的setNameAge方法中抛异常, 输出:AgeException: age cannot be this.age)。 import java.util.*; public class Main { public static void main(String[] args) { Scanner r = new Scanner(System.in); int count; try { count = r.nextInt(); Pet petList[] = new Pet[count]; for(int i=0;i<count;i++) { String name; int age; petList[i] = new Dog(); name = r.next(); age = r.nextInt(); petList[i].setNameAge(name, age); petList[i].eat(); } } catch(AgeException e) { System.out.println(e); } catch(InputMismatchException e) { System.out.println(e+ ": count or age must be an integer"); } catch(Exception e) { System.out.println(e+": other exception"); } } } 样例输入 4a aa 11 bb 22 cc 33 dd 44 样例输出 java.util.InputMismatchException: count or age must be an integer
以下是代码实现:
```
import java.util.*;
abstract class Pet {
protected String name;
protected int age;
public Pet(String name, int age) {
this.name = name;
this.age = age;
}
public abstract void eat();
public abstract void setNameAge(String name, int age);
}
class Dog extends Pet {
public Dog() {
super("", 0);
}
public void eat() {
System.out.println(name + " is eating.");
}
public void setNameAge(String name, int age) {
if (age <= 0) {
throw new AgeException("AgeException: age cannot be " + age);
}
this.name = name;
this.age = age;
}
}
class AgeException extends RuntimeException {
public AgeException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
int count;
try {
count = r.nextInt();
Pet petList[] = new Pet[count];
for (int i = 0; i < count; i++) {
String name;
int age;
petList[i] = new Dog();
name = r.next();
age = r.nextInt();
petList[i].setNameAge(name, age);
petList[i].eat();
}
} catch (AgeException e) {
System.out.println(e);
} catch (InputMismatchException e) {
System.out.println(e + ": count or age must be an integer");
} catch (Exception e) {
System.out.println(e + ": other exception");
}
}
}
```
输入样例:
```
4a aa 11 bb 22 cc 33 dd 44
```
输出样例:
```
java.util.InputMismatchException: count or age must be an integer
```