sumton.txt

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

TXT
31
字号
// Program to compute the sum of numbers from 0 to n
// where n is entered by the user

import javax.swing.JOptionPane;

public class SumToN
{

   public static void main(String[] args)
   {
	  int counter = 0;
	  int total = 0;

	  String numStr;

	  numStr = JOptionPane.showInputDialog(
	     "Enter a value for n:" );
	  int n = Integer.parseInt(numStr);

	  while (counter <= n)
	  {
	   	total += counter;
	   	counter++;
	  }
	  JOptionPane.showMessageDialog(null,
	     "Sum to " + n + " is " + total);

      System.exit(0);
   }
}

⌨️ 快捷键说明

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