解题思路:需要返回两个值,使用数组进行返回
注意事项:返回值类型为数组 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语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:1206 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:466 |
剪刀石头布 (C语言代码)浏览:1755 |
C语言训练-亲密数 (C语言代码)浏览:682 |
母牛的故事 (C语言代码)浏览:945 |
数组与指针的问题浏览:718 |
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:575 |
Quadratic Equation (C语言代码)浏览:989 |
C语言程序设计教程(第三版)课后习题10.7 (用指针求解)浏览:1477 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:513 |