这可能是我正在查看的一个简单错误,但是我的问题是,当我尝试返回字符串“ roman”时,它只会返回null。
这是主程序:

/* CCTPG-22 // Assignment 08: Roman Numerals
 * @author   Kevin R. Trujillo
 * @version  10/28/2015
 * Purpose:  Converts numbers into roman numerals format and vice-a-versa
 * Status:
 */

import java.util.*;   // For Scanner

public class RomanMain
{
  public static void main(String[] args)
  {

    Scanner in = new Scanner(System.in);

    RomanYear year1 = new RomanYear(1975);
    RomanYear year2 = new RomanYear(1988);
    RomanYear year3 = new RomanYear(2007);

    System.out.println(year1.toString());
    System.out.println(year2.toString());
    System.out.println(year3.toString());
    int diff21 = year2.getYearDecimal() - year1.getYearDecimal();
    int diff32 = year3.getYearDecimal() - year2.getYearDecimal();

    RomanYear y2MinusY1 = new RomanYear(diff21);
    RomanYear y3MinusY2 = new RomanYear(diff32);
    System.out.println("Year2 minus Year1 is: " + y2MinusY1.getYearRoman());
    System.out.println("Year3 minus Year2 is: " + y3MinusY2.getYearRoman());


  }

  // Add new methods here


} // No code can be here (outside the class)


这是RomanYear类:

/**
 * Auto Generated Java Class.
 */
public class RomanYear {

  private int decimal ;
  private String roman ;

 public RomanYear()   // default constructor
  {
  decimal = 0 ;
  roman = "" ;
  }
  public RomanYear(int newYear)
  {
    decimal = newYear ;
    roman = setYearDecimal(decimal);
  }

  public RomanYear(String newYear )
  {
    roman = newYear ;
    decimal = setYearRoman(roman) ;
  }

  public int setYearRoman(String roman)
  {

    String romanNumeral = roman.toUpperCase();
    for(int x = 0;x < romanNumeral.length(); x++)
    {
      char convertToDecimal = roman.charAt(x);
      // first step:  Easy stuff
      switch (convertToDecimal)
      {
        case 'M': decimal += 1000;   break;
        case 'D': decimal += 500;    break;
        case 'C': decimal += 100;    break;
        case 'L': decimal += 50;     break;
        case 'X': decimal += 10;     break;
        case 'V': decimal += 5;      break;
        case 'I': decimal += 1;      break;
        }
    }
    // Now adapt for specials
    if (romanNumeral.contains("IV"))
    {
        decimal-=2;
    }
    if (romanNumeral.contains("IX"))
    {
        decimal-=2;
    }
    if (romanNumeral.contains("XL"))
    {
        decimal-=20;
    }
    if (romanNumeral.contains("XC"))
    {
        decimal-=20;
    }
    if (romanNumeral.contains("CD"))
    {
        decimal-=200;
    }
    if (romanNumeral.contains("CM"))
    {
        decimal-=200;
    }
    return decimal ;
   }

  public String setYearDecimal(int yr)
  {
    if (decimal > 3999)
     {
      System.out.println("Decimal Number: " + decimal + " is over 3999. ") ;
      System.out.println("Please enter a new number") ;
      System.out.println("Program is ending.............") ;
      try {
        Thread.sleep(2000);                 //5000 milliseconds is one second.
          }
      catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
          }

      System.exit(0) ;
     }

    else
    {
    int digit;
      String roman = "";

      // 1000's column
      digit = yr/1000;
      for (int i = 0; i < digit; i++)
        roman = roman + "M";
      yr = yr % 1000;   // leaves 0 to 999

      // 100s column
      String [] hunds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
      digit = yr/100;
      roman = roman + hunds[digit];
      yr = yr % 100;   // leaves 0 to 99

       // 10s column
      String [] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
      digit = yr/10;
      roman = roman + tens[digit];
      yr = yr % 10;   // leaves 0 to 9

      // Ones column
      String [] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
      roman = roman + ones[yr];

    }
    return roman ;
  }

  public int getYearDecimal()
  {
    return decimal ;
  }

  public String getYearRoman()
  {
     return roman ;
  }

  public String toString()
  {

    System.out.print("Decimal: " + decimal + " as Roman Numerals is " ) ;
    return roman ;
  }

  /* ADD YOUR CODE HERE */

}


如果有人可以解释我在做什么错,那将不胜感激,而不仅仅是发布“正确的方法”来做。

谢谢!

最佳答案

在罗马类的顶部,创建一个名为roman的变量。

 public class RomanYear {

  private int decimal ;
  private String roman ;


在setYearDecimal中,创建一个名为roman的新变量。该局部变量隐藏了全局变量。

int digit;
String roman = "";


当您执行roman = roman + "M";时,您仅更新本地罗马变量。全局变量仍然为空。

您需要做的就是从第二个变量前面删除String

09-30 14:16