⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sumcalculator.java

📁 卡内基梅陇大学的网上教程 ssd3第一单元的源码
💻 JAVA
字号:
package unitOne;

import java.io.*;

/**
 * This application displays the sum of the integers in a specified
 * range.
 *
 * @author  iCarnegie
 * @version  1.0.0
 */
public class SumCalculator  {

    private static BufferedReader  stdIn =
        new BufferedReader(new  InputStreamReader(System.in));

    private static PrintWriter  stdOut =
        new PrintWriter(System.out, true);

    private static PrintWriter  stdErr =
        new PrintWriter(System.err, true);

    /**
     * Returns the sum of the integers in the specified
     * range [lower,upper].
     *
     * @param lower  the lower limit.
     * @param upper  the upper limit.
     * @return  the sum of the integers in the specified
     *          range [lower,upper].
     */
    public static int  sumRange(int lower, int upper)  {

        stdErr.println("sumRange called.  lower:" + lower
                       + "  upper:"  + upper);

        int  total = 0;

        for (int i = lower ; i <= upper; i++) {
            total += i;
            stdErr.println("    for loop,  i:" + i + "  total:"
                           + total);
        }

        return total;
    }

    /**
     * Displays the sum of the integers in a specified range.
     *
     * @param args  not used.
     */
    public static void  main(String[] args)  {

        int lower;
        int upper;

        try  {
            stdErr.print("lower limit:  ");
            stdErr.flush();
            lower = Integer.parseInt(stdIn.readLine());
            stdErr.print("upper limit:  ");
            stdErr.flush();
            upper = Integer.parseInt(stdIn.readLine());
            stdOut.println("The result is: " + sumRange(lower, upper));
        } catch (NumberFormatException  nfe)  {
            stdErr.println(nfe);
        } catch (IOException  ioe)  {
            stdErr.println(ioe);
        }

    }
}

⌨️ 快捷键说明

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