解题思路:需要返回两个值,使用数组进行返回
注意事项:返回值类型为数组 int[ ] 放在void位置
参考代码:
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
int a=scanner.nextInt();
int b=scanner.nextInt();
int [] arr=swap(a, b);
for (int i = 0; i < 2; i++) {
System.out.print(arr[i]+" ");
}
}
//自定义方法只能return一次,故用数组返回交换后的值
public static int[] swap(int a,int b) {
int [] arr=new int [2];
arr[0]=b;
arr[1]=a;
return arr;
}
}
0.0分
1 人评分
C语言训练-8除不尽的数 (C语言代码)暴力解法,答案只有一个,直接输出就好了浏览:1015 |
C语言程序设计教程(第三版)课后习题9.2 (C语言代码)浏览:687 |
简单的a+b (C语言代码)浏览:643 |
程序员的表白 (C语言代码)浏览:1319 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:525 |
【绝对值排序】 (C语言代码)浏览:713 |
A+B for Input-Output Practice (VII) (C++代码)浏览:606 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:597 |
妹子杀手的故事 (C语言代码)浏览:1218 |
【亲和数】 (C语言代码)浏览:501 |