if ( number1 == 1 )
if ( number2 == 2 )
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number1 > 10 )
JOptionPane.showMessageDialog(null, "结果2", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number2 < 20 )
JOptionPane.showMessageDialog(null, "结果3", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else
JOptionPane.showMessageDialog(null, "结果4", "输出结果", JOptionPane.INFORMATION_MESSAGE );
我的意图是。当程序同时满足age=19,number1=1,number2=2时,输出“结果1”否则继续执行下面的语句。当下面的if条件都不满足时。输出“结果4”。
可是。实际运行中。当age=19但number1不等于1或number2不等于2时。程序自动推出。而不是输出“结果4”
若把
if ( age == 19 )
if ( number1 == 1 )
if ( number2 == 2 )
改成
if ( age == 19 )
if ( number1 == 1 )
则没有问题。两行可以。而仨行则不行。。。不知为何?
小弟刚学java。请各位大虾指教。感激不敬
7 个解决方案
#1
else 总是与最近的if匹配
#2
if ( age == 19 && number1 == 1 && number2 == 2 )//这样
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
#3
其实你只要调试一下就知道,你的所有else都是对应于if ( number2 == 2 )这个if的,所有只有你的age=19而且number1=1的而number2<>的时候,才能会跳到else里去。要达到你的说的目的,其实你只要
if (( age == 19 )&&( number1 == 1 )&&( number2 == 2 ))
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number1 > 10 )
JOptionPane.showMessageDialog(null, "结果2", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number2 < 20 )
JOptionPane.showMessageDialog(null, "结果3", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else
JOptionPane.showMessageDialog(null, "结果4", "输出结果", JOptionPane.INFORMATION_MESSAGE );
if (( age == 19 )&&( number1 == 1 )&&( number2 == 2 ))
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number1 > 10 )
JOptionPane.showMessageDialog(null, "结果2", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number2 < 20 )
JOptionPane.showMessageDialog(null, "结果3", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else
JOptionPane.showMessageDialog(null, "结果4", "输出结果", JOptionPane.INFORMATION_MESSAGE );
#4
呀..是这样啊...怎么书上没介绍呢....有点像C 啊.
#5
受益匪浅
#6
你有没有看书呀!拜托
#7
*_*看了6章。。前面没介绍。。。今天看到第10章才看到。。。
#1
else 总是与最近的if匹配
#2
if ( age == 19 && number1 == 1 && number2 == 2 )//这样
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
#3
其实你只要调试一下就知道,你的所有else都是对应于if ( number2 == 2 )这个if的,所有只有你的age=19而且number1=1的而number2<>的时候,才能会跳到else里去。要达到你的说的目的,其实你只要
if (( age == 19 )&&( number1 == 1 )&&( number2 == 2 ))
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number1 > 10 )
JOptionPane.showMessageDialog(null, "结果2", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number2 < 20 )
JOptionPane.showMessageDialog(null, "结果3", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else
JOptionPane.showMessageDialog(null, "结果4", "输出结果", JOptionPane.INFORMATION_MESSAGE );
if (( age == 19 )&&( number1 == 1 )&&( number2 == 2 ))
JOptionPane.showMessageDialog(null, "结果1", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number1 > 10 )
JOptionPane.showMessageDialog(null, "结果2", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else if ( number2 < 20 )
JOptionPane.showMessageDialog(null, "结果3", "输出结果", JOptionPane.INFORMATION_MESSAGE );
else
JOptionPane.showMessageDialog(null, "结果4", "输出结果", JOptionPane.INFORMATION_MESSAGE );
#4
呀..是这样啊...怎么书上没介绍呢....有点像C 啊.
#5
受益匪浅
#6
你有没有看书呀!拜托
#7
*_*看了6章。。前面没介绍。。。今天看到第10章才看到。。。