sentinel.txt

来自「java的一经典教程」· 文本 代码 · 共 43 行

TXT
43
字号
// Program to add a series of numbers entered by the user
// The adding ends when the user enters the number zero

import javax.swing.JOptionPane;

public class Sentinel
{

	public static void addSeries()
	{
		final int SENTINEL = 0;
		int latestNum;
		int total = 0;
		String numStr;

		JOptionPane.showMessageDialog(null,
		   "Enter a series of numbers, zero to end: " );

		numStr = JOptionPane.showInputDialog(
		   "First number?  " );
	  	// convert from String to int
		latestNum = Integer.parseInt(numStr);

		while (latestNum != SENTINEL)
		{
			total = total + latestNum;
			numStr = JOptionPane.showInputDialog(
			   "Next number?");
			latestNum = Integer.parseInt(numStr);
		}

		JOptionPane.showMessageDialog(null,
		   "Total is " 	+ total);
	}


   public static void main(String[] args)
   {
	    addSeries();

        System.exit(0);
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?