0
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 MixNumberWithUnit_AM(Mix_AM m, String u){
            mixNumber = m;
            unit =u;
         MixNumberWithUnit_AM ft=null;
         MixNumberWithUnit_AM in=null;
         int ftpos =userInput.indexOf("ft");
         int inpos = userInput.indexOf("in");
         int appos = userInput.indexOf("'");
         int quotepos = userInput.indexOf("\"");

   if(ftpos !=-1 && inpos !=1){ // if both "feet" and "inches" exist
        String ftString=userInput(0,ftpos+2);
        String inString=userInput(ftpos+2);
        ft= new MixNumberWithUnit_AM(ftString);
        in= new MixNumberWithUnit_AM(inString);
        }

   else if(ftpos !=-1 && quotepos ==-1){ //if "feet" exist and quote doesn't exist
        ft=new MixNumberWithUnit_AM("userInput");
        in= new MixNumberWithUnit_AM("0in"); // intialize to zero inches
        }

    else if(inpos !=1 && appos==-1){ //if inches exist and apostrophe doesn't exist
        ft=new MixNumberWithUnit_AM("0ft");
        in=new MixNumberWithUnit_AM("userInput");
        }

   else if(appos !=-1 && quotepos !=-1){ //if apostrophe and quote  exist
        String ftString=userInput(0,ftpos+1);
        String inString=userInput(ftpos+1);
        ft= new MixNumberWithUnit_AM(ftString);
        in= new MixNumberWithUnit_AM(inString);
          }
   else if (quotepos !=-1 && ftpos ==-1){ //if quote exists and feet doesn't exist
        ft=new MixNumberWithUnit_AM("0ft");
        in=new MixNumberWithUnit_AM("userInput");
          }
   else if (ftpos !=-1 && appos==-1){
          ft=new MixNumberWithUnit_AM("userInput");
        in= new MixNumberWithUnit_AM("0in");// intialize to zero inches
        }

    else if (ftpos ==-1 && appos !=-1){
          ft=new MixNumberWithUnit_AM("0ft");
        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 displayMix() {
         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 MixNumberWithUnit_AM add(MixNumberWithUnit_AM m1,MixNumberWithUnit_AM m2){
             mix data = Mix_AM.add(m1.mixNumber,m2.mixNumber); // mix data "can be named different"
         return( new MixNUmberWithUnit_AM(data,m1.unit));
            }

      public static void main(String[] args) {
        System.out.println("Enter first measurement: ");
        String userInput=get();
    MixNumberWithUnit_AM m1= parseInput("userInput");
    System.out.println(m1[0].display()+ m2[1].display());

        System.out.println("Enter second measurement: ");
        userInput = get();
        MixNumberWithUnit_AM m2=parseInput(userInput);
    System.out.println(m3[0].display()+m2[1].display());
    MixNumberWithUnit_AM m3 = MixNumberWithUnit_AM.add(m1,m2);        
    System.out.println(m3[0].display()+m2[1].display());

      }//main

   }//class

编译此代码后,我收到此错误,我无法弄清楚它是什么。我猜这与我的代码没有 userInput 声明有关,但我不知道把它放在哪里。

MixNumberWithUnit_AM.java:18: error: cannot find symbol
          int ftpos =userInput.indexOf("ft");
                     ^   symbol:   variable userInput   location:classMixNumberWithUnit_AM
4

3 回答 3

1

UserInputget()方法中定义。MixNumberWithUnit_AM()这意味着它将在该方法中不可用。

将变量声明为实例字段。并阅读变量范围

于 2013-10-10T20:48:52.277 回答
0

在 中替换uuserInput或反之亦然MixNumberWithUnit_AM。我希望您看到字符串 fromget()被传递给MixNumberWithUnit_AM并获取 name u

于 2013-10-10T20:54:21.493 回答
0

将您的 userInput 定义为全局变量,就像String userInput在您之后 的开头一样

String unit;
Mix_AM mixNumber;

并且您的 get() 和 main 方法,String从 userInput 中删除,应该只是

userInput = scan.nextLine(); 

 userInput=get();

但是你为什么要使用 String u 和 userInput?

于 2013-10-10T20:54:30.780 回答