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

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 寫檔問題
勾勾





發文: 14
積分: 0
於 2011-12-10 17:10 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
我想要請教一個觀念

正常的讀寫檔我可以寫得出來

但是我要的答案是在method時 我就不會讀了
讀檔的程式碼是不是只能放在main method裡面?

若只能在main Method的話 我該如何讀到其他method裡面的答案?
請問該加什麼語法或是有什麼解決方法?

如同下面的程式碼 我已經寫了讀檔與寫檔 但是寫出來的一直都是空白筆記本

(有點長見諒)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.util.Scanner;
import java.io.*;
 
public class RailroadWithStacks {
 
  /**
   * @param args
   */
  // //////////////////////////////////////////////////////////
 
  // data members
  public static ArrayStack[] track; // array of holding tracks
  public static int numberOfCars;
  public static int numberOfTracks;
  public static int smallestCar; // smallest car in any holding track
  public static int itsTrack; // holding track with car smallestCar
 
  /** output the smallest car from the holding tracks */
  public static void outputFromHoldingTrack() {
    // remove smallestCar from itsTrack
    track[itsTrack].pop();
    System.out.println("pop " + smallestCar + " from" + " H" + itsTrack);
 
    // find new smallestCar and itsTrack by checking top of all stacks
    smallestCar = numberOfCars + 2;
    for (int i = 1; i <= numberOfTracks; i++)
      if (!track[i].empty()
          && ((Integer) track[i].peek()).intValue() < smallestCar) {
        smallestCar = ((Integer) track[i].peek()).intValue();
        itsTrack = i;
      }
  }
 
  /**
   * put car c into a holding track
   * 
   * @return false if there is no feasible holding track for this car
   */
  public static boolean putInHoldingTrack(int c) {
    // find best holding track for car c
    // initialize
    int bestTrack = 0, // best track so far
    bestTop = numberOfCars + 1; // top car in bestTrack
 
    // scan tracks
    for (int i = 1; i <= numberOfTracks; i++)
      if (!track[i].empty()) {// track i not empty
        int topCar = ((Integer) track[i].peek()).intValue();
        if (c < topCar && topCar < bestTop) {
          // track i has smaller car at top
          bestTop = topCar;
          bestTrack = i;
        }
      } else // track i empty
      if (bestTrack == 0)
        bestTrack = i;
 
    if (bestTrack == 0)
      return false; // no feasible track
 
    // add c to bestTrack
    track[bestTrack].push(new Integer(c));
    System.out.println("push " + c + " into " + "H" + bestTrack);
 
    // update smallestCar and itsTrack if needed
    if (c < smallestCar) {
      smallestCar = c;
      itsTrack = bestTrack;
    }
 
    return true;
  }
 
  /**
   * rearrange railroad cars beginning with the initial order
   * inputOrder[1:theNumberOfCars]
   * 
   * @return true if successful, false if impossible.
   */
  public static boolean railroad(int[] inputOrder, int theNumberOfCars,
      int theNumberOfTracks) {
    numberOfCars = theNumberOfCars;
    numberOfTracks = theNumberOfTracks;
 
    // create stacks track[1:numberOfTracks] for use as holding tracks
    track = new ArrayStack[numberOfTracks + 1];
    for (int i = 1; i <= numberOfTracks; i++)
      track[i] = new ArrayStack();
 
    int nextCarToOutput = 1;
    smallestCar = numberOfCars + 1; // no car in holding tracks
 
    // rearrange cars
    for (int i = 1; i <= numberOfCars; i++)
      if (inputOrder[i] == nextCarToOutput) {// send car inputOrder[i]
                          // straight out
        System.out.println("output" + " " + inputOrder[i]);
        nextCarToOutput++;
 
        // output from holding tracks
        while (smallestCar == nextCarToOutput) {
          outputFromHoldingTrack();
          nextCarToOutput++;
        }
      } else
      // put car inputOrder[i] in a holding track
      if (!putInHoldingTrack(inputOrder[i]))
        return false;
 
    return true;
  }
 
  // //////////////////////////////////////////////////
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
 
    java.io.File file = new java.io.File("D:\\B111IN.txt");
    // Create a Scanner for the file
    Scanner input = new Scanner(file);
 
    String s = input.nextLine();
    while (input.hasNext()) {
 
      s = input.next();
 
    }
 
    int x[] = new int[10];
    x[0] = 0;
    for (int i = x.length - 1; i > 0; i--) {
      x[i] = s.charAt(s.length() - i) - '0';
 
    }
 
    input.close();
 
    java.io.File file1 = new java.io.File("D:\\B111OUT.txt");
    java.io.PrintWriter output = new java.io.PrintWriter(file1);
    railroad(x, 9, 3);
    output.close();
  }
 
}
 
class ArrayStack {
  int top;
  Object[] stack;
 
  public ArrayStack(int initialCapacity) {
    if (initialCapacity < 1)
      throw new IllegalArgumentException("initialCapacity must be >= 1");
    stack = new Object[initialCapacity];
    top = -1;
  }
 
  /** create a stack with initial capacity 10 */
  public ArrayStack() {
    this(10);
  }
 
  public boolean empty() {
    return top == -1;
  }
 
  public Object peek() {
    if (empty()) {
      System.out.print("Empty Stack");
 
    }
    return stack[top];
  }
 
  public void push(Object theElement) {
 
    if (top == stack.length - 1)
      System.out.println("Out of array length");
 
    stack[++top] = theElement;
  }
 
  public Object pop() {
    if (empty()) {
      System.out.print("Empty Stack");
 
    }
    Object topElement = stack[top];
    stack[top--] = null; // enable garbage collection
    return topElement;
  }
 
}


勾勾 edited on 2011-12-10 17:17
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
974 寫檔問題 勾勾 5472 2011-12-10 17:10
» JWorld@TW »  Java SE 討論區

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8