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
答案 0 :(得分:1)
UserInput
在get()
方法中定义。这意味着它不会在MixNumberWithUnit_AM()
方法中使用。
将变量声明为实例字段。并阅读变量scope!
答案 1 :(得分:0)
在u
中将userInput
替换为MixNumberWithUnit_AM
,反之亦然。
我希望您看到来自get()
的字符串传递给MixNumberWithUnit_AM
并获取名称u
。
答案 2 :(得分:0)
将您的userInput定义为全局变量,在
之后的开始处定义为String userInput
String unit;
Mix_AM mixNumber;
和你的get()和main方法,从userInput中删除String
,应该只是
userInput = scan.nextLine();
和
userInput=get();
但为什么要使用String u和userInput?