java: data exchange

java: data exchange

topic

问题描述
  编写一个程序,输入两个整数,分别存放在变量x和y当中,然后使用自己定义的函数swap来交换这两个变量的值。
  输入格式:输入只有一行,包括两个整数。
  输出格式:输出只有一行,也是两个整数,即交换以后的结果。
  要求:主函数负责数据的输入与输出,但不能直接交换这两个变量的值,必须通过调用单独定义的函数swap来完成,而swap函数只负责交换变量的值,不能输出交换后的结果。
输入输出样例
样例输入
4 7
样例输出
7 4

I think I write very good, but do not know where the problem

import java.util.Scanner;

public class swap交换 {
	public static void swap(int a,int b){
		int temp=a;
		a=b;
		b=temp;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		int b=sc.nextInt();
		swap(a,b);
		System.out.print(a+" "+b);
	}
	

}

I really do not understand why I would add two variables Why to die, and I finally put a, b reassigned Yeah. How to die, I was still inside this swap variables changed, it can not. You must be a, b to static, directly after the swap conversion.

A little ignorant! ! !

Correct codes

import java.util.Scanner;

public class swap交换 {
	public static int a,b;
	public static void swap(){
		int temp=a;
		a=b;
		b=temp;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		 a=sc.nextInt();
		 b=sc.nextInt();
		swap();
		System.out.print(a+" "+b);
	}
	

}

Published 146 original articles · won praise 3 · Views 2785

Guess you like

Origin blog.csdn.net/weixin_44522477/article/details/104561953