请使用8个基本类型和String类型分别声明成员变量和局部变量,在main方法中,分别打印输出 这些变量 的值。 打印的时候请使用字符串拼接,格式如下:变量名 = 值;
时间: 2024-09-06 18:04:29 浏览: 29
好的,以下是使用Java语言按照你的要求完成的例子:
```java
public class Main {
// 声明成员变量(实例变量)
int primitiveInt = 10;
double primitiveDouble = 3.14;
boolean primitiveBoolean = true;
char primitiveChar = 'A';
byte primitiveByte = (byte) 5;
short primitiveShort = (short) 100;
long primitiveLong = 9223372036854775807L;
String stringVar = "Hello, World!";
// 声明局部变量
int localInt;
double localDouble = 3.14159;
boolean localBoolean = false;
char localChar;
byte localByte = (byte) 0;
short localShort = (short) 255;
long localLong;
String localVar;
public static void main(String[] args) {
// 初始化并打印成员变量的值
System.out.println("primitiveInt = " + primitiveInt);
System.out.println("primitiveDouble = " + primitiveDouble);
System.out.println("primitiveBoolean = " + primitiveBoolean);
System.out.println("primitiveChar = " + primitiveChar + "(char code: " + (int) primitiveChar + ")");
System.out.println("primitiveByte = " + primitiveByte);
System.out.println("primitiveShort = " + primitiveShort);
System.out.println("primitiveLong = " + primitiveLong);
System.out.println("stringVar = " + stringVar);
// 初始化并打印局部变量的值
localInt = 50;
localDouble = Math.PI;
localBoolean = !localBoolean;
localChar = 'Z';
localByte = (byte) 127;
localShort = (short) -1;
localLong = 1234567890L;
localVar = "Local String";
// 打印局部变量的值
System.out.println("localInt = " + localInt);
System.out.println("localDouble = " + localDouble);
System.out.println("localBoolean = " + localBoolean);
System.out.println("localChar = " + localChar + "(char code: " + (int) localChar + ")");
System.out.println("localByte = " + localByte);
System.out.println("localShort = " + localShort);
System.out.println("localLong = " + localLong);
System.out.println("localVar = " + localVar);
}
}
```
当你运行这个`main`方法时,它会依次打印出所有指定类型的变量及其对应的值。
阅读全文