问题
I have the following code:
for(int sentenceCounter = 0; sentenceCounter < sentences.length; sentenceCounter++)
{
double score = 0;
String[] sentence = sentences[sentenceCounter].split(" ");
for (int titleCounter = 0; titleCounter < titles.length; titleCounter++)
{
for (int wordCounter = 0; wordCounter < sentence.length; wordCounter++)
{
if (titles[titleCounter].equals(sentence[wordCounter]))
{
if(titleCounter == 0)
{
score += titleValue * hundred / 100;
sentenceScore.put(sentenceCounter, score);
System.out.println(sentenceCounter + " " + wordCounter + " " + score);
}else
{
score += titleValue - titleSubtract * hundred / 100;
sentenceScore.put(sentenceCounter, score);
System.out.println(sentenceCounter + " " + wordCounter + " " + sentenceScore.get(0));
}
System.out.println(sentenceCounter);
}
}
}
}
I am trying to reset the score counter after the sentenceCounter increases, however when I set score = 0 at the right after the for loop of sentenceCounter it does not reset itself. So, how do I reset it?
The steps of the program is as follows: 1. Break title into single words. 2. Break sentence into single words. 3. See if sentence contains word from title. If yes then increase score of sentence by X. 4. Repeat for next word in title. 5. If sentence contains another word from title add to previous score. 6. Repeat this for next sentence but first set score counter to zero. This is where I am stuck.
This is the output I am observing from above code:
0 0 10.0
0
0 1 19.3
0
1 5 19.3
1
As you can see sentence two starts at 19.3 instead of starting at zero.
Here is the program in full:
import java.util.HashMap;
import java.util.Map;
public class MainDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String fullTitle = "Facebook traffic website";
String[] titles = fullTitle.split(" ");
Double titleValue = 10.0;
Double sentenceSubtract = 13.0;
Double titleSubtract = 0.70;
Double hundred = 100.0;
HashMap<Integer, String> titleArray = new HashMap<Integer, String>();
for(int i =0; i < titles.length; i++)
{
String t = titles[i];
titleArray.put(i, t);
}
String article = "Facebook traffic is growing fast. It is the second largest website. ";
HashMap<Integer, Double> sentenceScore = new HashMap<Integer, Double>();
String[] sentences = article.split("\\. ");
String[] str;
for(int sentenceCounter = 0; sentenceCounter < sentences.length; sentenceCounter++)
{
double score = 0;
String[] sentence = sentences[sentenceCounter].split(" ");
for (int titleCounter = 0; titleCounter < titles.length; titleCounter++)
{
for (int wordCounter = 0; wordCounter < sentence.length; wordCounter++)
{
if (titles[titleCounter].equals(sentence[wordCounter]))
{
if(titleCounter == 0)
{
score += titleValue * hundred / 100;
sentenceScore.put(sentenceCounter, score);
System.out.println(sentenceCounter + " " + wordCounter + " " + score);
}else
{
score += titleValue - titleSubtract * hundred / 100;
sentenceScore.put(sentenceCounter, score);
System.out.println(sentenceCounter + " " + wordCounter + " " + sentenceScore.get(0));
}
System.out.println(sentenceCounter);
}
}
}
}
}
}
回答1:
Your score counter is being reset every iteration. The problem is that you are printing a different set of values if titleCounter != 0. The print to console that you are expecting to print score, is actually defined to print sentenceScore.get(0)
.
This is correct. sentenceScore.get(0) would be the score value from last iteration before it was reset to 0.
回答2:
You don't need to reset a field you define in the body of the loop to zero, it will be re-created on the next iteration already -
for(int sentenceCounter = 0; sentenceCounter < sentences.length; sentenceCounter++)
{
double score = 0; // <-- create a score variable, set it to 0.
System.out.println("Score is " + score);
来源:https://stackoverflow.com/questions/25331323/reset-counter-to-zero-in-java