如下所示,我在 mac Terminal 上输入了javac App.java,我看到operator五个错误,都是一样的。我不知道如何解决它,我很感激你的指点?
我已经导入 javabook.*;这是 Textpad 上的 JAVA 代码。
import javabook.*;
//import java.util.Scanner;
class App
{
public static void main(String args[])
{
//declare variable
String theNumber;
//declare object
Scanner someInput;
//input
System.out.println("Please enter area size : ");
someInput = new Scanner(System.in);
theNumber = someInput.nextLine();
//processing
if ( theNumber < 20 )
{
System.out.println( "It is too small." ) ;
}
else if ( theNumber > 20 && theNumber < 40 )
{
System.out.println( "It is perfect size." ) ;
}
else if ( theNumber > 40 && theNumber < 60 )
{
System.out.println( "It is too big." ) ;
}
//close the program without error
System.exit(0);
}
}
终端响应为App.java:28: operator < cannot be applied to java.lang.String,int if ( theNumber < 20 )
我会很感激你的帮助?
更新:
import javabook.*; //Same result Scanner or javabook. Tried both and it worked.
import java.util.Scanner; //this is required
class App
{
public static void main(String args[])
{
//declare variable
//String theNumber;
//int theNumber = Integer.parseInt(someInput.nextLine());
int theNumber; //need to convert your string theNumber to an int first. If you search for that, you'll find lots, both here and on the internet generally.
int a = Integer.parseInt(theNumber);
//theNumber = someInput.nextInt(); //this is commented out so now down to two errors
//declare object
Scanner someInput;
//input
System.out.println("Please enter area size : ");
someInput = new Scanner(System.in);
theNumber = someInput.nextLine();
//processing
if ( theNumber < 20 )
{
System.out.println( "It is too small." ) ;
}
else if ( theNumber > 20 && theNumber < 40 )
{
System.out.println( "It is perfect size." ) ;
}
else if ( theNumber > 40 && theNumber < 60 )
{
System.out.println( "It is too big." ) ;
}
//close the program without error
System.exit(0);
}
}