gpt4 book ai didi

java - 无法通过解析和用户输入找到符号错误

转载 作者:行者123 更新时间:2023-12-01 14:01:45 25 4
gpt4 key购买 nike

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

最佳答案

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

将变量声明为实例字段。并阅读变量 scope !

关于java - 无法通过解析和用户输入找到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19305893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com