Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 26, 2016 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokomokohitsuzi/c1f3761e3f2d38e2ceb3fe6329a5c2eb to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/c1f3761e3f2d38e2ceb3fe6329a5c2eb to your computer and use it in GitHub Desktop.
新・明解Java入門 演習7-4
import java.util.Scanner;
public class En07_04 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.println("1からnまでの全整数を加算します。");
int n;
do {
System.out.print("整数nを入力して下さい:");
n = stdIn.nextInt();
} while (n <= 1);
System.out.printf("1から%dまでの和は%dです。", n, sumUp(n));
}
// 合計を計算するメソッド
private static int sumUp(int n) {
//合計変数sum
int sum = 0;
for (int i = 0; i <= n; i++) {
sum += i;
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment