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

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 想要用鍵盤輸入一組整數
catwife2





發文: 3
積分: 0
於 2005-02-15 22:38 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
大家幫我看看 我是新手 搞不懂哪裡錯了 謝謝歐
想要用鍵盤輸入一組整數(不設限有幾組)
然後列出 其中的 最大值 最小值 平均值
想試試看用ArrayList轉到Array的這個方法
在這個前提下 怎麼改正這個錯誤
看很多文章也找了書 還是搞不懂怎麼修改
因為是初學者 程式寫的很糟糕也很可笑
請高手幫幫忙別見笑 先謝謝了
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
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.*;
import java.util.*;
public class HK11{
    public static void main(String [] args){
        ArrayList temp = new ArrayList() ;
        System.out.println( "請輸入整數: (輸入 q 離開)" );
        BufferedReader keyIn=new BufferedReader(new InputStreamReader(System.in));
        String s = null;
        try{
            while( true ){
                s = keyIn.readLine();
                if(s.equals("q"))
               break;
                System.out.print("key in: ");
                int nu = Integer.parseInt ( s );
                temp.add(new Integer(nu));
            }
        }
        catch (IOException e){
      e.printStackTrace();
    }
        Integer [] ok = (Integer[])temp.toArray(new Integer[temp.size()]);
        int tempNu = 0;
        int sum = 0;
        int half = 0;
        for(int j=0 ; j<(ok.length) ; j++){
 
            sum = sum + ok[j];
            for(int k=(ok.length-1) ; k>j ; k--){
                if(ok[j] < ok[k]){
                tempNu = ok[k];
                ok[k] = ok[j];
                ok[j] = tempNu;
                }
            }
        }
        half = sum / (ok.length);
        System.out.println(ok[0]);
        System.out.println(ok[(ok.length-1)]);
        System.out.println(half);
    }
}

===錯誤訊息===
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
C:\j2sdk1.4.2_06\bin\javac.exe HK11.java
HK11.java:28: operator + cannot be applied to int,java.lang.Integer
            sum = sum + ok[j];
                      ^
HK11.java:30: operator < cannot be applied to java.lang.Integer,java.lang.Integer
                if(ok[j] < ok[k]){
                         ^
HK11.java:31: incompatible types
found   : java.lang.Integer
required: int
                tempNu = ok[k];
                           ^
HK11.java:33: incompatible types
found   : int
required: java.lang.Integer
                ok[j] = tempNu;
                        ^
4 errors
Finished executing
 


HK11.java (1.27k)


catwife2 edited on 2005-02-15 23:53
reply to postreply to post
作者 Re:大家幫我看看 想要用鍵盤輸入一組整數 [Re:catwife2]
fish1217

一步一腳印



發文: 696
積分: 6
於 2005-02-15 23:04 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)程式碼標記


reply to postreply to post
作者 Re:大家幫我看看 想要用鍵盤輸入一組整數 [Re:catwife2]
catwife2





發文: 3
積分: 0
於 2005-02-15 23:41 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
謝謝提醒 我來改一下

reply to postreply to post
作者 Re:想要用鍵盤輸入一組整數 [Re:catwife2]
robin

雞排小弟



發文: 481
積分: 1
於 2005-02-16 07:40 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
幫你翻譯一下,有錯的話還請多多指教
catwife2 wrote:

===錯誤訊息===
1
2
3
4
C:\j2sdk1.4.2_06\bin\javac.exe HK11.java
HK11.java:28: operator + cannot be applied to int,java.lang.Integer
            sum = sum + ok[j];
                      ^


"+"這個運算元,不能被應用在Integer這個物件
catwife2 wrote:
HK11.java:30: operator < cannot be applied to java.lang.Integer,java.lang.Integer
if(ok[j] < ok[k]){
^


"<"這個運算元,不能被應用在Integer這個物件
catwife2 wrote:
1
2
3
4
5
HK11.java:31: incompatible types
found   : java.lang.Integer
required: int
                tempNu = ok[k];
                           ^


不相容的形態,tempNu為int,ok[k]為Integer
catwife2 wrote:
1
2
3
4
5
HK11.java:33: incompatible types
found   : int
required: java.lang.Integer
                ok[j] = tempNu;
                        ^


不相容的形態,tempNu為int,ok[k]為Integer
catwife2 wrote:
1
2
3
4 errors
Finished executing
 



reply to postreply to post
作者 Re:想要用鍵盤輸入一組整數 [Re:robin]
fish1217

一步一腳印



發文: 696
積分: 6
於 2005-02-16 12:48 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)
primary type (基礎型別) 的 case converse (型別轉換)
(implicit converse (隱含轉換) and explicit(強制轉換))
2)
primary type (基礎型別) 與 reference type (參考型別) 中的
wrapper class (外包類別) 的轉換
3)
reference type(參考型別) 與 equals method
primary type (基礎型別) 與 logic instruction(邏輯運算)


fish1217 edited on 2005-02-16 12:53
reply to postreply to post
» JWorld@TW »  Java 新手區

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