English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

import javax.swing.*;




public class mohamed {




static void tripleValue (int x,int y )
{
int c;
c=x;
x=y;
y=c;
}



public static void main(String[] args) {

String o;
int a;
o=JOptionPane.showInputDialog("insert a");
a=Integer.parseInt(o);
String l;
int b;
l=JOptionPane.showInputDialog("insert b");
b=Integer.parseInt(l);

tripleValue(a,b) ;
System.out.println("x="+a);
System.out.println("y="+b);
i write the program please tell me where is the error

2006-10-23 01:11:16 · 5 answers · asked by Geek 2 in Computers & Internet Programming & Design

5 answers

The "quick and dirty" way is to use single-element int arrays instead of ints.

static void tripleValue(int[] x, int[] y)
{
int c = x[0];
x[0] = y[0];
y[0] = c;
}

I really don't know why people keep asking this, as such a function seems of no value. Is this some school homework question?

2006-10-24 08:35:23 · answer #1 · answered by vincentgl 5 · 0 0

Hai...

I think the problem is in the passing of the values to the function / procedure.

Since you are passing the variables the value, the values are swapping, but since you are not passing the variables by reference the memory location pointed by the variables x & y are not updating.

Retry by passing the address of the variables, (pass by reference)
In this way the address of the containing variables will be sent to the function and any change will immediately reflect in the original locations and in turn in the variable values.

Hope you have got the answer.
Happy Computing
Hare Krsna...

2006-10-23 01:18:04 · answer #2 · answered by keep_smiling_n_be_happy 2 · 0 0

the parameters used are passed by reference but is first copied to be used within the function. I dont have the correct code for you but you can read a book on java and see how java handels references in functions i think you might need to use the Integer class in java yes thats it use the Integer class as the type for your parameters (Integer x, Integer y) and then change the values of the integer object within the method

2006-10-23 01:30:11 · answer #3 · answered by Anonymous · 0 0

The variables are passed by value and objects are passed by reference to the function in java.
Since you are passing the variable only the value will be copied and any changes made to them will not be reflected, if you want the changes to be reflected the pass the objects.

so instead of using int give as integer in the function, that will make x,y as objects of integer type, and pass a,b (integer objects) to the function.

2006-10-23 01:33:39 · answer #4 · answered by Rahul T 1 · 0 0

hi.. in case you want to change 2 Numbers employ third variable for temporary shop values of both variables that are to be swapped.. void swap() { int x,y,temp(temporary variable); temp=x; //positioned fee of x into temp x=y; //positioned fee of y into x; y=temp; //positioned fee of temp it is unique fee of x to y; } yipee...we hav jus swapped 2 numbers (we may be able to also settle for those Values Of x & y From clientele) e.g. printf("enter the fee for x & y"); scanf("%d %d",&x,&y); cheer's Thanx... ITGuy

2016-12-05 03:23:56 · answer #5 · answered by ? 4 · 0 0

fedest.com, questions and answers