2
import java.util.Arrays;
import java.util.Scanner;
class MixNumberWithUnit_AM{
   String unit;
   Mix_AM mixNumber;



   public MixNumberWithUnit_AM (String str) {
      String[] abc= parseUnit(str);
      mixNumber= new Mix_AM(abc[0]);
      unit = abc[1];
   }

   public static MixNumberWithUnit_AM position(String str){
         String userInput= str;
         MixNumberWithUnit_AM ft;
         MixNumberWithUnit_AM in;
         int ftpos = userInput.indexOf("ft");
         int inpos = userInput.indexOf("in");
         int appos = userInput.indexOf("'");
         int quotepos = userInput.indexOf("\"");

   if(ftpos !=-1 && inpos !=1){ // This is if both exist
        String ftString= userInput.substring(0,ftpos+2);
        String inString= userInput.substring(ftpos+2);
        ft= new MixNumberWithUnit_AM(ftString);
        in= new MixNumberWithUnit_AM(inString);
        }

   else if(ftpos !=-1 && inpos ==-1){ //if "feet" exist (only have feet)
        ft= new MixNumberWithUnit_AM(userInput);
        in= new MixNumberWithUnit_AM(" ");// intialize to zero inches
        }

    else if(ftpos ==-1 && inpos !=-1){
        ft= new MixNumberWithUnit_AM(" ");
        in= new MixNumberWithUnit_AM(userInput);
        }

   else if(appos !=-1 && quotepos !=-1){
        String ftString= userInput.substring(0,ftpos+1);
        String inString= userInput.substring(ftpos+1);
        ft= new MixNumberWithUnit_AM(ftString);
        in= new MixNumberWithUnit_AM(inString);
        }
   else if(appos !=-1 && quotepos ==-1){
        ft= new MixNumberWithUnit_AM(userInput);
        in= new MixNumberWithUnit_AM(" ");
        }        

   else if (appos ==-1 && quotepos ==-1){
        ft= new MixNumberWithUnit_AM(" ");
        in= new MixNumberWithUnit_AM(userInput);
        }
    }


   public static String[] parseUnit (String userInput){
      String str= userInput.trim(); 
      int len = str.length();
      String s2=str.substring(len-2); //get the last 2 characters
      String p1="",p2="";

      if(s2.equals("in") || s2.equals("ft") ){
         p1=str.substring(0,len-2).trim();
         p2=s2;
      }
      else{
         s2=str.substring(len-1); //get the last 2 characters
         if (s2.equals("'") || s2.equals("\"") ){
            p1=str.substring(0,len-1).trim();
            p2= s2;
         }
         else{
            p1=""; p2=""; System.out.println("error:input w/o units");}
      }//outer if

      String[] rtn={p1,p2};
      return rtn;
   }//parse

   public String displayMixNumberWithUnit() {
      String str=mixNumber.displayMix();
      if(str.equals("0")){
         str="";
      }
      else{
         str= str+unit; 
      }
      return str;
   }//display

      public static String get(){
         Scanner scan = new Scanner (System.in);
         String userInput = scan.nextLine();
         userInput = userInput.trim();
         return (userInput); 
      } //get       


   public static void main(String[] args, String userInput) {
       String userinput=get();
       MixNumberWithUnit_AM m1=position(userInput);
       System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());

        userInput = get();
       MixNumberWithUnit_AM m2=position(userInput);
      System.out.println(m2[0].displayMixNumberWithUnit()+m2[1].displayMixNumberWithUnit()); 
        }//main

}//class

每当我使用 jGrasp 编译此代码时,我都会收到数组错误。我相信我没有正确声明我的数组。我不太了解数组的工作原理。有人可以帮我弄清楚。

MixNumberWithUnit_AM.java:105: error: array required, but MixNumberWithUnit_AM found
       System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
                            ^
MixNumberWithUnit_AM.java:105: error: array required, but MixNumberWithUnit_AM found
       System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
                                                             ^
MixNumberWithUnit_AM.java:109: error: array required, but MixNumberWithUnit_AM found
      System.out.println(m2[0].displayMixNumberWithUnit()+m2[1].displayMixNumberWithUnit());
                           ^
MixNumberWithUnit_AM.java:109: error: array required, but MixNumberWithUnit_AM found
      System.out.println(m2[0].displayMixNumberWithUnit()+m2[1].displayMixNumberWithUnit());
                                                            ^
4 errors
4

3 回答 3

3
   MixNumberWithUnit_AM m1=position(userInput);
   System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());

变量m1不是数组。它是一个对象引用。

于 2013-10-14T19:39:10.023 回答
0

您的position()方法的返回类型为MixNumberWithUnit_AM. 正如这条线所见

MixNumberWithUnit_AM m1=position(userInput);

当然后尝试以m1数组的方式访问您正在执行的变量时,当它不是数组时,它是MixNumberWithUnit_AM这一行中的类型对象

System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());

如果您的对象MixNumberWithUnit_AM有一个字段是一个数组,那么您将不得不像访问它一样

m1.arrayField[0]....
于 2013-10-14T19:39:51.283 回答
0

您已声明MixNumberWithUnit_AM position(String str)函数以返回 class 的对象MixNumberWithUnit_AM。但是在您的以下代码片段中:

   MixNumberWithUnit_AM m1=position(userInput);  // m1 is returned as object, 
                //you are even declaring m1 as object
    System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
   // you are accessing it as array
于 2013-10-14T19:40:35.383 回答