尝试从用户输入中取出最大值和最小值并在Java中显示它们

问题描述:

好的,谢谢,现在代码可以在字符串数组中找到该单词,但现在我需要从用户输入中获取最正面的单词和来自用户输入的最负面的词。所以,如果我插入可怕的区域,可怕的是1.25和区域是2.66,那么可怕的是最消极的词,区是最积极的词,但是如何设置代码,我不知道如何跟踪这些值,然后确保它可以打印出正确的单词作为最正面的单词并将该单词作为后负片打印出来。所以打印用户输入的平均值。我尝试过并行阵列,但我想避免这些。其他问题是能够从用户接受多个输入,直到他们使用按键ctrl z或ctrl d。 (我被告知他们是Eclipse的一部分,但我不知道如何使用它们。) 谢谢你有关如何继续的任何建议。尝试从用户输入中取出最大值和最小值并在Java中显示它们

输出:

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; 
    } 
+0

使用二维数组。允许Array的第一列包含Word,而Array的第二列包含单词数值。 – DevilsHnd

你可以尝试用HashMap这样的:

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