我正在完成一项任务,虽然我已经完成了它的开始部分,但我仍然处于最后一步。这是我必须要做的最后一部分。
修改main方法,以便在调用
swap
方法之后,num1
和num2
的实际值在main
中交换,并显示在此处 输出。输出将有一个额外的行,显示 交换后main
中的两个数字的值。新线 在输出中需要“在主方法中交换数字后, num1是2,num2是1“。
代码:
public class PassByValue {
/** Main method */
public static void main(String[] args) {
// Declare and initialize variables
int num1 = 1;
int num2 = 2;
System.out.println("Before invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
//invoke the swap method to attempt to swap two variables
swap(num1, num2);
System.out.println("After invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
}
/** Swap two variables */
public static void swap(int n1, int n2) {
System.out.println("\tInside the swap method");
System.out.println("\t\tBefore swapping, n1 is " + n1
+ " and n2 is " +n2);
// Swap n1 with n2
int temp = n1;
n1 = n2;
n2 = temp;
System.out.println("\t\tAfter swapping, n1 is " + n1
+ " and n2 is " + n2);
}
}
答案 0 :(得分:0)
我将在这里向您展示两种方法,选择哪种更好。 第一:
public class Swap{
static int num1 = 1;//declare num1 and num2 as static
static int num2 = 2;
/** Swap two variables */
public static void swap(int n1, int n2) {
System.out.println("\tInside the swap method");
System.out.println("\t\tBefore swapping, n1 is " + n1
+ " and n2 is " +n2);
// Swap n1 with n2
int temp = n1;
n1 = n2;
n2 = temp;
System.out.println("\t\tAfter swapping, n1 is " + n1
+ " and n2 is " + n2);
num1=n1;//after swapping assign the new swapped values to num1 and num2. It will reflect in main as it is declared as static
num2=n2;
}
public static void main(String[] args) throws UnknownHostException {
// Declare and initialize variables
System.out.println("Before invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
//invoke the swap method to attempt to swap two variables
swap(num1, num2);
System.out.println("After invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
}
}
方法2:
public class Swap{
static int num1 = 1;
static int num2 = 2;
/** Swap two variables */
public static void swap() {//without functional arguments
System.out.println("\tInside the swap method");
System.out.println("\t\tBefore swapping, n1 is " + num1
+ " and n2 is " +num2);
// Swap n1 with n2
int temp = num1;//<-- swap values here and it will reflect
num1 = num2;
num2 = temp;
System.out.println("\t\tAfter swapping, n1 is " + num1
+ " and n2 is " + num2);
}
public static void main(String[] args) throws UnknownHostException {
// Declare and initialize variables
System.out.println("Before invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
//invoke the swap method to attempt to swap two variables
swap();//dont pass arguments as num1 and num2 are declared static, swap the values in the method itself
System.out.println("After invoking the swap method, num1 is " +
num1 + " and num2 is " + num2);
}
}
答案 1 :(得分:0)
我认为练习的目的是向你展示这是不可能的,因为Java只有“按值传递”。要做这样的事情,你需要传递一个包含值的对象的swap方法。例如,传递一个数组,然后swap
可以交换数组中的前两个值。或者定义一些其他对象。
答案 2 :(得分:0)
你的教授可能已经完成了这项任务,你可以理解Pass by value并通过java中的参考概念和OOPS传递。
首先在java中,method参数将作为pass by value发送。如果参数是原始类型,则将发送它们的值。 如果参数是“对象”,则对象的引用仍将作为值发送。
所以,在这里解决你的问题,你可以通过创建一个包含你的值的对象来解决它。
以下代码可以解决您的问题:
public class test {
/** Main method */
public static void main(String[] args) {
// Declare and initialize variables
Numberr num1 = new Numberr(1);
Numberr num2 = new Numberr(2);
System.out.println("Before invoking the swap method, num1 is " +
num1.n + " and num2 is " + num2.n);
//invoke the swap method to attempt to swap two variables
swap(num1, num2);
System.out.println("After invoking the swap method, num1 is " +
num1.n + " and num2 is " + num2.n);
}
/** Swap two variables */
public static void swap(Numberr n1, Numberr n2) {
System.out.println("\tInside the swap method");
System.out.println("\t\tBefore swapping, n1 is " + n1.n
+ " and n2 is " +n2.n);
// Swap n1 with n2
int temp = n1.n;
n1.n = n2.n;
n2.n = temp;
System.out.println("\t\tAfter swapping, n1 is " + n1 .n
+ " and n2 is " + n2.n);
}
}
class Numberr {
public Numberr(int i) {
// TODO Auto-generated constructor stub
this.n = i;
}
int n;
}