Last active
November 6, 2016 13:20
-
-
Save mokomokohitsuzi/3565d3b99a880b829f9b18c847affbd5 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習4-13
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 En4_13 { | |
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 <= 0); | |
int sum = 0; // 合計変数sumを0で初期化 | |
for (int i = 1; i <= n; i++) { // iを1で初期化し、nまで繰り返す。 | |
sum += i; // sumにiを足す。 | |
} | |
System.out.println("1から" + n + "までの和は" + sum + "です。"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment