我正在编写一个程序,决定玩家是否有资格参加全明星赛。
我每次都得到“0”的输出。这是一个逻辑错误吗?别看了。
import javax.swing.JOptionPane;
public class IfElseBattingEverage
{
public static void main (String[] args)
{
int hits;
int atBats;
int batAvg;
// get number hits from user
hits = Integer.parseInt( JOptionPane.showInputDialog(null, "Enter number of hits") );
// get number of at bats from user
atBats = Integer.parseInt( JOptionPane.showInputDialog(null, "Enter number of at Bats") );
// calcualte batting average
batAvg = ((hits/atBats)*100);
System.out.println(batAvg);
// decision if...else
if (batAvg >= 30 )
{
System.out.println ("Congratulations! You are eligible for the all-star game, with a batting average of: " + batAvg + "%");
}
else
{
System.out.println ("Bummer! With a batting average of: " + batAvg + "% ,you are not eligible for the all-star game! Better luck next year!");
}
}
}
答案 0 :(得分:0)
这可能是你的问题:
batAvg = ((hits/atBats)*100);
我会用这个:
batAvg = (100*hits/atBats);
hits/atBats
是整数除法,主要导致0
结果(如果点击< atBats)