break在
while循环中使用带
if条件的语句检查
thesum >100。希望这些提示能帮助您获得所需的行为。由于您还需要计算平均值,因此将中断逻辑放在avg后面:
while (input != 0) { // Use JOptionPane method to accept input from user input = Float.parseFloat( JOptionPane.showInputDialog( null, "Please enter a number. Enter 0 to quit: ")); // Invoke sum method and pass input and summation to sum method theSum = (sum(input, theSum)); // Invoke avg method and pass summation and counter to avg average = (avg(theSum, counter)); // Increment the counter variable counter++; if (theSum > 100) break;}// Invoke display method and pass summation, average, and counter variables to itdisplay(theSum, average, counter);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)