アプリ版:「スタンプのみでお礼する」機能のリリースについて

以前質問させて頂いた続きなのですがまた詰まってしまったので
知恵をお借りしたいと思います。

File Count.java
public class Count {
public int read(){
return 1;
}
public void write(int count){
(1)
}
}

File Test.java
//カウント処理
Count count = new Count();
int scount = count.read();
contribute.setAttribute("no", ""+scount);
scount = scount+1;
count.write(scount);


Test.javaファイルにおいて変数scountを使用する際に
1~10と読み込むたびに数字を一つづつ足していくという処理を行いたいのです。
(1)の部分にソースを書くことまでは指定されており、ここまでは間違えていないと言われているのですが(1)の部分をどんな風に作成すれば良いかがわかりません。
どのように書けば良いのでしょうか??
よろしくお願いします。

A 回答 (3件)

1から10までってことは繰り返し処理するってことですよね?



Count count = new Count(); →No1さんのCountクラスのように記述を修正
for ( int i = 0 ; i < 10 ; i ++ ){
int scount = count.read(); → i+1またはi=0;i<11にしてiをそのまま代入しても同じなんですが・・・
contribute.setAttribute("no", ""+scount);
scount = scount+1; →いらない
count.write(scount);
}

あと、(1)では何がしたいんですか?
    • good
    • 0
この回答へのお礼

わかりづらい質問で申し訳ありません。
やり方の指定がなくなったので、以下の方法で数字を取ることに成功しました。

File Test.java
//カウント処理
Count count = new Count();
int scount = count.read();
contribute.setAttribute("no", ""+scount);
scount = scount+1;
count.write(scount);

File Count.java
public class Count {
public int read(){
int result = 0;
try{
BufferedReader reader = new BufferedReader(new FileReader("countlog.txt"));
try{
String str = "";
str = reader.readLine();
result = Integer.parseInt(str);
}finally{
reader.close(); //必ずcloseを行ってください。
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public void write(int count){
String str = ""+count;
try{
FileWriter writer = new FileWriter("countlog.txt");
try{
char[] buff = str.toCharArray();
writer.write(buff);
}finally{
writer.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}

書いてもらったソースもこれからの参考にさせていただきます。
ありがとうございました。

お礼日時:2008/10/07 14:39

質問の内容がよくわからない。



1から10まで足し算した結果を知りたいってことですか?

そうだとすればreadが毎回1を返してたらダメですよね・・・

この回答への補足

回答どうもです。
やりたいことは
contribute.setAttribute("no", ""+scount);
でscountを1回使うと
xmlFILEに
<contribute no=1>
2回目以降も
<contribute no=2>
などと書き込む数字を増やしていきたいのです。
retune 1;
の部分は自分も疑問だったのですが問題ないと言われたのでそのまま使用していました。

補足日時:2008/10/07 10:37
    • good
    • 0

public class Test {



public static void main(String[] args) {
//カウント処理
Count count = new Count();
int scount = count.read();
// contribute.setAttribute("no", "" + scount);
count.write(scount);
}
}

class Count {

int count = 1;

int read() {
return count++;
}

void write(int scount) {
System.out.println(scount);
}
}

あんまり 求めてるモノに 沿ってないかもしれませんが…
 うちなりの ナナメ読み 勝手な解釈で 書いてみました。。
            あんまり参考ならなそうですね凹
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

こちらが説明下手なだけですんでorz
まだまだjavaというものに対する理解度が低いので
うまく求めるものを伝えることができませんでした。

書いて頂いたものは参考にさせて頂きます。

お礼日時:2008/10/07 10:54

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!