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

发布于 2024-11-17 23:53:49 字数 2926 浏览 1 评论 0 原文

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

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

如何让程序正常工作?

I´m new to programming, and are doing som exercices. In this exercice I am supposed to write a program that reads in three numbers from the user of the program. The program is supposed to find the smallest number, and print which one is the smallest number.
Here is my code:

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

And here is the error messages:

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

How can I get the program to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾听心声的旋律 2024-11-24 23:53:49

你不能做 thirdnumber <第二个数 < Java 中的firstnumber,将其解释为 (thirdnumber < secondarynumber) (thirdnumber < secondarynumber) firstnumber,将 truefalse 与数字进行比较。

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

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

You can't do thirdnumber < secondnumber < firstnumber in Java, as it interprets it as (thirdnumber < secondnumber) < firstnumber, comparing true or false to the number.

What you want is to combine two conditions like this:

if ( firstnumber < secondnumber && secondnumber < thirdnumber )
result = firstnumber + " is the smallest number!";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文