错误消息:运算符<不能应用于boolean,int

时间:2011-07-02 21:19:51

标签: java compiler-errors

我是编程新手,正在做som的练习。在这个练习中,我应该编写一个程序,从程序的用户读取三个数字。该程序应该找到最小的数字,并打印哪一个是最小的数字。 这是我的代码:

import javax.swing.JOptionPane;

public class Smallestnumber
{
  public static void main( String args[] )
    {
    // Defining variables:
    String firstnumberstring;
    String secondnumberstring;
    String thirdnumberstring;
    String result;
    int firstnumber;
    int secondnumber;
    int thirdnumber;

    // Making input frames:
    firstnumberstring = JOptionPane.showInputDialog( "Write first number!" );
    secondnumberstring = JOptionPane.showInputDialog( "Write second number!");
    thirdnumberstring = JOptionPane.showInputDialog( "Write third number!" );

    // Converting stringvalues to int values:
    firstnumber = Integer.parseInt( firstnumberstring );
    secondnumber = Integer.parseInt( secondnumberstring );
    thirdnumber = Integer.parseInt( thirdnumberstring );

    // Initialising printstring to an empty string:
    result = "";

    if ( firstnumber < secondnumber < thirdnumber )
    result = firstnumber + " is the smallest number!";

    if ( firstnumber < thirdnumber < secondnumber )
    result = firstnumber + " is the smallest number!";

    if ( secondnumber < firstnumber < thirdnumber )
    result = secondnumber + " is the smallest number!";

    if ( secondnumber < thirdnumber < firstnumber )
    result = secondnumber + " is the smallest number!";

    if ( thirdnumber < firstnumber < secondnumber )
    result = thirdnumber + " is the smallest number!";

    if ( thirdnumber < secondnumber < firstnumber )
    result = thirdnumber + " is the smallest number!";

    // Making conclusion box:
    JOptionPane.showMessageDialog( null, result, "Conclusion:", JOptionPane.INFORMATION_MESSAGE );

    } // End of main method
} // End of class Smallestnumber

以下是错误消息:

Smallestnumber.java:29: operator < cannot be applied to boolean,int
    if ( firstnumber < secondnumber < thirdnumber )
                                    ^
Smallestnumber.java:32: operator < cannot be applied to boolean,int
    if ( firstnumber < thirdnumber < secondnumber )
                                   ^
Smallestnumber.java:35: operator < cannot be applied to boolean,int
    if ( secondnumber < firstnumber < thirdnumber )
                                    ^
Smallestnumber.java:38: operator < cannot be applied to boolean,int
    if ( secondnumber < thirdnumber < firstnumber )
                                    ^
Smallestnumber.java:41: operator < cannot be applied to boolean,int
    if ( thirdnumber < firstnumber < secondnumber )
                                   ^
Smallestnumber.java:44: operator < cannot be applied to boolean,int
    if ( thirdnumber < secondnumber < firstnumber )
                                    ^
6 errors

如何让程序运行?

1 个答案:

答案 0 :(得分:9)

您无法在Java中执行thirdnumber < secondnumber < firstnumber,因为它将(thirdnumber < secondnumber) < firstnumber解释为truefalse与该数字进行比较。

你想要的是结合两个这样的条件:

if ( firstnumber < secondnumber && secondnumber < thirdnumber )
result = firstnumber + " is the smallest number!";