JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區 » 語法(Syntax)  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 變數應用技巧提問-新手
ethan0511





發文: 4
積分: 0
於 2010-03-12 13:12 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位大大好哇..我是一個JAVA的初學者,
老師在上禮拜出了一個範例,目的是要兩個變數數值互換,
我把程式大概寫出來,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Text
{
 public static void main(String args[])
  {
  int m=3;
  int n=5;
  int t;
 
  int t=n;
  int n=m;
  int m=t;
  }
  System.out.println(m);
  System.out.println(n);
}

解答 m=5;n=3

後來老師就出一道題給我們想,
如果不想用如上方設定一個t(暫存作用),
單純用m、n兩值,直接執行數值互換,該怎麼寫,
我想了很久還是想不出來,請各位大大好心讓小弟我解個惑,
不然一直想,晚上還會想到睡不著呢...


reply to postreply to post
作者 Re:變數應用技巧提問-新手 [Re:ethan0511]
juevampire

藍色吸血鬼:皮卡丘十萬伏特!!!



發文: 167
積分: 0
於 2010-03-12 13:28 user profilesend a private message to usersend email to juevampirereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
m = m + n;
n = m - n;
m = m - n;


reply to postreply to post
什麼時候我曾孫會叫哆啦A夢來照顧我?
㆝\('_')/ ㆞<(._.)> ㆟/(-_-)\
作者 Re:變數應用技巧提問-新手 [Re:ethan0511]
Kaworucloud





發文: 451
積分: 0
於 2010-03-12 13:44 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public static void main( String[] args ){
  swapWithoutTemp1( 5, 3 );
  swapWithoutTemp2( 5, 3 );
  swapWithoutTemp3( 5, 3 );
}
 
public static void swapWithoutTemp1( int x, int y ){
  System.out.printf( "Before swap1 x: %d, y: %d\n", x, y );
  x = x + y;
  y = x - y;
  x = x - y;
  System.out.printf( "After swap1 x: %d, y: %d\n", x, y );
}
 
public static void swapWithoutTemp2( int x, int y ){
  System.out.printf( "Before swap2 x: %d, y: %d\n", x, y );
  x = x ^ y;
  y = x ^ y;
  x = x ^ y;
  System.out.printf( "After swap2 x: %d, y: %d\n", x, y );
}
 
public static void swapWithoutTemp3( long x, long y ){
  System.out.printf( "Before swap3 x: %d, y: %d\n", x, y );
  x = ( x << 32 ) | y;
  y = ( x >> 32 ) & 0xFFFF;
  x = x & 0xFFFF;
  System.out.printf( "After swap3 x: %d, y: %d\n", x, y );
}


Kaworucloud edited on 2010-03-12 14:01
reply to postreply to post
巨乳は夢が詰まってるから大きい、貧乳はみんなに夢を与えているから小さいんだ!
作者 Re:變數應用技巧提問-新手 [Re:Kaworucloud]
ethan0511





發文: 4
積分: 0
於 2010-03-12 14:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
K大所寫的,對新手而言應該是太複雜了,
老師應該不會出到這樣的解答給我們解Dead
不過還是謝謝K大,THANKS!


reply to postreply to post
作者 Re:變數應用技巧提問-新手 [Re:juevampire]
ethan0511





發文: 4
積分: 0
於 2010-03-12 14:05 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
J大的解答...真的是讓我傻眼Shock
想請問這是不是設計常用的方式?
還是大家比較常用t值取代?
兩者撰寫比較的話,有何優缺點呢?


reply to postreply to post
作者 Re:變數應用技巧提問-新手 [Re:ethan0511]
roytsang





發文: 622
積分: 1
於 2010-03-12 15:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
一般做swapping 不會這樣做的,
用t 的好處就是cpu 不用計算,
如果不用t的好處就可省記憶體。


roytsang edited on 2010-03-12 15:29
reply to postreply to post
作者 Re:變數應用技巧提問-新手 [Re:ethan0511]
metavige

metavige

版主

發文: 2134
積分: 10
於 2010-03-12 21:14 user profilesend a private message to usersend email to metavigereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ethan0511 wrote:
J大的解答...真的是讓我傻眼Shock
想請問這是不是設計常用的方式?
還是大家比較常用t值取代?
兩者撰寫比較的話,有何優缺點呢?


simple is the best...often

程式是寫給人看的


reply to postreply to post
請各位新手參考 論壇規範Java 新手 FAQ
» JWorld@TW »  Java 新手區 » 語法(Syntax)

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8