Created
October 31, 2016 10:16
-
-
Save mokomokohitsuzi/76ed9468e5a6681db12d77e5514c53cf to your computer and use it in GitHub Desktop.
新・明解Java入門 演習4-7
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_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