【问题标题】:Trying to take the max and min values out of a user input and displaying them in Java Eclipse尝试从用户输入中获取最大值和最小值并在 Java Eclipse 中显示它们
【发布时间】:2016-08-08 22:07:05
【问题描述】:

好的,谢谢,现在代码可以在字符串数组中找到单词,但现在我需要从用户输入中获取最积极的词,从用户输入中获取最消极的词。所以如果我插入dreadful zone,dreadful是1.25,zone是2.66,所以dreadful是最消极的词,zone是最积极的词,但是代码是如何设置的,我不知道如何跟踪这些值确保它可以打印出正确的词作为最积极的词和作为帖子否定的词。因此打印用户输入的平均值。我尝试做并行数组,但我想避免这些。其他问题是能够从用户那里获取多个输入,直到他们使用键 ctrl z 或 ctrl d。 (有人告诉我它们是 Eclipse 的一部分,但我不知道如何使用它们。) 感谢您对如何进行的任何建议。

输出:

Please type one line of review and when you are done press either Ctr D or Ctr Z
dreadful zone
dreadful zone
The average is negative at 1.9583333333333333
Incomplete assignment

public class MovieReviewSentimentAnalysis {

    static Scanner userInput = new Scanner(System.in);

   public static void main(String[] args) {
      // TODO: complete me

       //make own arrays to pass by value
       //movieReviewComments = the text
       String[] movieReviewComments = new String[8529];
       //movieReviewScores = numeric values, avoid lit. values 
       int[] movieReviewScores = new int[8529];

       String userComment = "";

MovieReviewReader.readMovieReviews("movie_reviews.txt", movieReviewComments, movieReviewScores); //string, string array, and int array

       System.out.println("Please type one line of review and when you are done press either Ctr D or Ctr Z");
       userComment = userInput.nextLine();
       System.out.println(userComment);




//     String[] words = userComment.split("\\s+");
       String[] words2  = userComment.split("[\\W]"); //splits at "\W", or non-word characters
       double singularUserWordTotal = 0;
       double wordTotal = 0;
       double totalSumOfUserCommentWords = 0;
       double highestWordScore = 20;

       double lowestWordScoreTotal = 0;

       int locationOfWordInUserInput = 0;

       String userInputWord = "";
//     int itemCount = words.length;
       for (int i = 0; i < words2.length; i++)
       {
           userInputWord = words2[i];
           singularUserWordTotal = wordCount(userInputWord, movieReviewComments, movieReviewScores);
           wordTotal += singularUserWordTotal;
           totalSumOfUserCommentWords = wordTotal / words2.length;

//         locationOfWordInUserInput = i;
//         if(singularUserWordTotal > highestWordScore)
//         {
//             singularUserWordTotal = highestWordScore;
//         }
//         if(singularUserWordTotal < highestWordScore)
//         {
//             singularUserWordTotal = highestWordScore;
//             lowestWordScoreTotal = singularUserWordTotal;
//         }

       }

       displayScores(totalSumOfUserCommentWords);


//     System.out.println(reviewFile);
       System.out.println("Incomplete assignment");

       userInput.close();
   }

   public static double wordCount(String userInputWord, String[] movieReviewComments, int[] movieReviewScores)
   {
       double storeScore = 0;
       double totalSumOfReviewScores = 0;
       double numOfTimesWordAppears = 0;

       for (int i=0; i < (movieReviewComments.length); i++)
       {
           if (movieReviewComments[i].contains(userInputWord))
               //PUNCTUATION IS A PROBLEM (if it's at the end of the user input then it's fine though)
           {
               storeScore = movieReviewScores[i];
               totalSumOfReviewScores += storeScore;

               numOfTimesWordAppears++;

               //System.out.println("Found");

              //What if the word doesn't appear in the text file?????

           }else if (!movieReviewComments[i].contains(userInputWord))
                   {
                    numOfTimesWordAppears += 0;
                   }
//         else
//            System.out.println("You dun goofed"); //delete after fixing problem
       }
       double wordScoreAverage = totalSumOfReviewScores / numOfTimesWordAppears;
       return wordScoreAverage;
   }

   public static double displayScores(double userCommentTotal)
   {
       if(userCommentTotal > 2.01)
       {
           System.out.println("The average is positive at " + userCommentTotal);
       }else if(userCommentTotal < 2.01 && userCommentTotal > 1.99)
       {
           System.out.println("The average is neutral at " + userCommentTotal);
       }else
           System.out.println("The average is negative at " + userCommentTotal);

       return userCommentTotal;
   }

【问题讨论】:

  • 使用二维数组。允许数组的第一列保存单词,数组的第二列保存单词的数值。

标签: java eclipse max min ctrl


【解决方案1】:

您可以尝试为此使用 HashMap:

Map<String,Integer> h = new HashMap<String,Integer>();
h.put(word,word_score);
//get word score
int score = h.get(word)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2020-02-28
    • 2020-08-16
    • 1970-01-01
    • 2016-07-24
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多