Javaの学習を始めて3日目くらいの人のための問題集。
簡単な問題と、解答例があるので写経するのにピッタリ。少し考えてわからなそうだったら答えを見て書き写すのが上達のコツ。
Q1
xに23,yに6を代入して以下の結果を求め出力せよ。
x+y
x-y
x*y
x/y
x%y
[実行例]
x+y=29
x-y=17
x*y=138
x/y=3
x%y=5
02 | public static void main(String[] args) { |
05 | System.out.println( "x+y=" +(x+y)); |
06 | System.out.println( "x-y=" +(x-y)); |
07 | System.out.println( "x*y=" +(x*y)); |
08 | System.out.println( "x/y=" +(x/y)); |
09 | System.out.println( "x%y=" +(x%y)); |
Q2
xに10を代入し、それに5を3回足し結果を出力せよ。(whileと+=を使うこと)
[実行例]
Ans:25
02 | public static void main(String[] args) { |
09 | System.out.println( "Ans:" + x); |
Q3
1~10の乱数を生成して、それが偶数か奇数かを判定
[実行例]
8は偶数(even)
02 | public static void main(String[] args) { |
03 | int num= new java.util.Random().nextInt( 10 )+ 1 ; |
05 | System.out.println(num+ "は偶数(even)" ); |
07 | System.out.println(num+ "は奇数(odd)" ); |
Q4
bottomに4.3を代入、heightに5.4を代入して、三角形の面積を求めよ。
[実行例]
底辺:4.3,高さ:5.4の三角形の面積は:11.61
2 | public static void main(String[] args) { |
5 | System.out.println( "底辺:" +bottom+ ",高さ:" +height+ "の三角形の面積は:" +(bottom*height/ 2 )); |
Q5
名前を入力させ、それを出力する。
[実行例]
あなたのなまえを入力してください>田中[enter]
こんにちは田中さん!
1 | public static void main(String[] args) { |
2 | System.out.print( "あなたのなまえを入力してください>" ); |
3 | String name= new java.util.Scanner(System.in).nextLine(); |
4 | System.out.println( "こんにちは" + name + "さん!" ); |
Q6
スカイツリーの高さを入力させ、それがあっていれば「OK!」間違っていれば「NG」と表示する。
[実行例]
スカイツリーの高さは?(m)>634[enter]
OK!
02 | public static void main(String[] args) { |
03 | System.out.print( "スカイツリーの高さは?(m)>" ); |
04 | int answer= new java.util.Scanner(System.in).nextInt(); |
06 | System.out.println( "OK!" ); |
08 | System.out.println( "NG" ); |
Q7
結婚しているかどうかを管理する変数isMarriedにtrueを代入する。
isMarriedがtrueならば「結婚しています。」
falseならば「結婚していません。」と出力する。
[実行例]
結婚しています。
1 | public static void main(String[] args) { |
2 | boolean isMarried= true ; |
4 | System.out.println( "結婚しています。" ); |
6 | System.out.println( "結婚していません。" ); |
コメント