java 使用drjava编译器给我的错误比其他编译器给我的错误要多

kyks70gy  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(298)
import java.io.*;
import java.util.*;

public class GentCanadian
{
  public static void main (String[] args) throws IOException
  {  
    BufferedReader objReader = new BufferedReader (new InputStreamReader (System.in));

    String lowerInput = ""; // declaring variable to work outside the loop

    while (!lowerInput.equals("quit"))
    {
      System.out.print ("Please enter a sentence to translate (Enter 'quit' to leave)");
      String input = objReader.readLine ();

      lowerInput = input.toLowerCase(); // any form of "quit" will make the loop exit

      Translate newSentence = new Translate(input);
      String output = newSentence.getOutput();

      System.out.println (output); // printing output
    }
  }
}

class Translate
{
  // declaring variables
  private String word = "";
  private String newWord = "";
  private String line = "";
  private String output = "";
  private char lastLetter;
  private char secondLastLetter;
  private int wordLength;

  Translate(String l)
  {
    line = l;
    Translation();
    getOutput(); // accessor method
  }

  private void Translation()
  {
    Scanner freader = new Scanner(line);
    StringBuffer newPhrase = new StringBuffer ("");

    while (freader.hasNext())
    {
      String word = freader.next();
      wordLength = word.length(); // gets the length of each word

      if (wordLength >= 5)
      {
        lastLetter = word.charAt(wordLength - 1);
        secondLastLetter = word.charAt(wordLength - 2);

        if (secondLastLetter == 'o' && lastLetter == 'r') // last two letters must be "OR"
        {
          String word2 = word.substring(0, wordLength - 2); // extracts all letters but the "OR"
          newWord = word2.concat("our"); // adds "OUR" to the end of the word
        }
        else
        {
          newWord = word; // keeps the same word
        }
      }
      else
      {
        newWord = word;
      }

      newPhrase.append(newWord);
      newPhrase.append(' '); // add a space
      output = newPhrase.toString(); // convert back to a string
    }
  }

  String getOutput()
  {
    return output; // returns the whole output
  }
}

我在drjava上遇到了一个错误,这是我的类应该使用的编译器,我在其他任何java编译器上都没有收到。在第17行,当我有语句lowerinput=input.tolowercase()时;它给了我一个错误,说我不能给lowerinput赋值,因为它是不可变的,并且已经给了一个值。如果有人知道我可以如何解决这个错误,请回应,因为我需要找出这个问题。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题