Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created October 31, 2016 10:16
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/76ed9468e5a6681db12d77e5514c53cf to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/76ed9468e5a6681db12d77e5514c53cf to your computer and use it in GitHub Desktop.
新・明解Java入門 演習4-7
import java.util.Scanner;
public class En4_7 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int n;
do {
System.out.print("何個表示しますか:");
n = stdIn.nextInt();
if (n <= 0) {
System.out.println("1以上の整数値を入力して下さい。");
}
} while (n <= 0);// nが0以下の場合、ループする。
int x = 1;
while (x <= n) {
// xの値が奇数(xを2で割った余りが1)の場合"*"を、
// 0の場合、"+"を出力。
System.out.print(x % 2 == 1 ? "*" : "+");
x++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment