错误消息:运算符 <不能应用于 boolean,int
我是编程新手,正在做一些练习。在本练习中,我应该编写一个程序,从程序用户那里读取三个数字。该程序应该找到最小的数字,并打印哪个是最小的数字。 这是我的代码:
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
如何让程序正常工作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能做
thirdnumber <第二个数 < Java 中的firstnumber
,将其解释为(thirdnumber < secondarynumber)
(thirdnumber < secondarynumber)
firstnumber
,将true
或false
与数字进行比较。你想要的是结合这样的两个条件:
You can't do
thirdnumber < secondnumber < firstnumber
in Java, as it interprets it as(thirdnumber < secondnumber) < firstnumber
, comparingtrue
orfalse
to the number.What you want is to combine two conditions like this: