Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created November 8, 2016 13: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/6e12fc204f0079f7ad3a717a7afa0722 to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/6e12fc204f0079f7ad3a717a7afa0722 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習4-14
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