/编写以下两种方法:print(int) 和 print(String)。 class // // 编写以下两种方法:print(int) 和 print(String)。 // 要求: // Solution 类必须包含三个方法。 // Solution 类必须包含 static main() 方法。 // Solution 类必须包含带 int 参数的 static print() 方法。 // Solution 类必须包含带 String 参数的 static print() 方法。
时间: 2024-03-06 10:51:10 浏览: 21
以下是 Java 代码实现:
```
public class Solution {
public static void main(String[] args) {
int num = 123;
String str = "Hello, world!";
print(num);
print(str);
}
public static void print(int num) {
System.out.println(num);
}
public static void print(String str) {
System.out.println(str);
}
}
```
以上代码实现了一个包含两个静态方法 `print(int)` 和 `print(String)` 的 `Solution` 类,并在 `main` 方法中进行了调用。 `print(int)` 方法接收一个整数参数并将其打印输出, `print(String)` 方法接收一个字符串参数并将其打印输出。注意,这两个方法都必须是静态方法,以便在没有实例化对象的情况下进行调用。
相关问题
//任务1:我们超负荷了! //编写以下两种方法:print(int) 和 print(String)。 class // // 编写以下两种方法:print(int) 和 print(String)。 // 要求: // Solution 类必须包含三个方法。 // Solution 类必须包含 static main() 方法。 // Solution 类必须包含带 int 参数的 static print() 方法。 //
以下是代码实现:
```
public class Solution {
public static void main(String[] args) {
int i = 10;
String s = "Hello World!";
print(i);
print(s);
}
public static void print(int i) {
System.out.println("int: " + i);
}
public static void print(String s) {
System.out.println("String: " + s);
}
}
```
在上述代码中,我们定义了两个方法 `print(int i)` 和 `print(String s)` 分别用于输出 `int` 类型和 `String` 类型的值。在 `main` 方法中,我们定义了一个 `int` 类型的变量 `i` 和一个 `String` 类型的变量 `s`,并分别对它们进行赋值。然后我们分别调用 `print(int i)` 和 `print(String s)` 方法,并将 `i` 和 `s` 作为参数传入。最终输出结果为:
```
int: 10
String: Hello World!
```
//任务2:int 和 Integer //编写以下两种方法:print(int) 和 print(Integer)。 在 main 方法中,编写调用两种方法的代码: // // 编写以下两种方法:print(int) 和 print(Integer)。 // 在 main 方法中,编写调用两种方法的代码: // 要求: // Solution 类必须包含 static main() 方法。 // Solution 类必须包含带
下是代码实现:
```
public class Solution {
public static void main(String[] args) {
int i = 10;
Integer integer = 20;
print(i);
print(integer);
}
public static void print(int i) {
System.out.println("int: " + i);
}
public static void print(Integer integer) {
System.out.println("Integer: " + integer);
}
}
```
在上述代码中,我们定义了两个方法 `print(int i)` 和 `print(Integer integer)` 分别用于输出 `int` 和 `Integer` 类型的值。在 `main` 方法中,我们定义了一个 `int` 类型的变量 `i` 和一个 `Integer` 类型的变量 `integer`,并分别对它们进行赋值。然后我们分别调用 `print(int i)` 和 `print(Integer integer)` 方法,并将 `i` 和 `integer` 作为参数传入。最终输出结果为:
```
int: 10
Integer: 20
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)