我正在编写一个程序,用户将不断输入数字,直到使用循环输入 0。然后它将显示输入的最大数字、输入的最小数字以及所有数字的平均值。这就是我必须开始的:
import javax.swing.JOptionPane;
public class MaxMinAvg
{
public static void main(String args[])
{
String inString = " ";
int count = 0;
inString = JOptionPane.showInputDialog("Enter a String");
//perform until the end of file
while(inString.length() != 0)
{
count++;
System.out.println("record " + count + " is " + inString);
// second read
inString = JOptionPane.showInputDialog("Enter a String");
} // end while loop
System.out.println("All done");
}
}
最佳答案
尝试将输入字符串与退出条件进行比较:
import javax.swing.JOptionPane;
public class MaxMinAvg
{
public static void main(String args[])
{
String inString = " ";
int count = 0;
inString = JOptionPane.showInputDialog("Enter a String");
//perform until the end of file
while(inString.length() != 0 && !inString.equal("0"))
{
count++;
System.out.println("record " + count + " is " + inString);
// second read
inString = JOptionPane.showInputDialog("Enter a String");
} // end while loop
System.out.println("All done");
}
}
关于java - 输入0时如何结束循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61037613/