Created
November 8, 2016 13:43
-
-
Save mokomokohitsuzi/6e12fc204f0079f7ad3a717a7afa0722 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習4-14
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_14 { | |
public static void main(String[] args) { | |
Scanner stdIn = new Scanner(System.in); | |
int n; | |
do { | |
System.out.print("nの値:"); | |
n = stdIn.nextInt(); | |
} while (n <= 0); | |
int sum = 0; | |
for (int i = 1; i <= n; i++) { | |
sum += i;// sum= sum+i | |
// iを表示。(ループする度に1ずつ増える) | |
System.out.print(i); | |
// iがnと同値ならば=を、それ以外は+を表示 | |
System.out.print(i == n ? " = " : " + "); | |
} | |
System.out.println(sum); // 合計を表示 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment