Created
December 26, 2016 12:43
-
-
Save mokomokohitsuzi/c1f3761e3f2d38e2ceb3fe6329a5c2eb to your computer and use it in GitHub Desktop.
新・明解Java入門 演習7-4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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