| 情報学部 | 菅沼ホーム | 全体目次 | 演習解答例 | 付録 | 索引 |

if (論理式) {
文1(複数の文も可)
}
else {
文2(複数の文も可)
}
・・・・・・
if (論理式1) {
・・・・・・
if (論理式2) {
・・・・・・
}
else {
・・・・・・
}
・・・・・・
}
else {
・・・・・・
}
01 if (a == b) {
02 max = y;
03 min = z;
04 if (min < 0.0) {
05 min = 0.0;
06 a = b;
07 }
08 }
09 else {
10 max = s;
11 min = g;
12 }
13 x = 9;
14 y = 10;
( A ~ F の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
/****************************/
/* 円周と面積の計算 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double pi = 3.141592654, r;
try {
// 半径の入力
System.out.print("円の半径は? ");
r = Double.parseDouble(in.readLine());
// 計算と出力
if (r > 0.0) {
double enshu = 2.0 * pi * r;
double men = pi * r * r;
System.out.println("円周=" + enshu + " 面積=" + men);
}
}
catch (IOException ignored) {}
}
}
( A ~ C の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
/****************************/
/* 坪と㎡の間の単位変換 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double x, y;
int sw;
try {
// データの入力
System.out.print("変換方向(0:坪→㎡,1:㎡→坪)は? ");
sw = Integer.parseInt(in.readLine());
System.out.print("変換するデータは? ");
x = Double.parseDouble(in.readLine());
// 変換と出力
if (sw == 0) { // 坪から㎡
y = 3.3 * x;
System.out.println(" xは " + y);
}
else { // ㎡から坪
y = x / 3.3;
System.out.println(" xは " + y);
}
}
catch (IOException ignored) {}
}
}
( A ~ J ,及び,K ~ N の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
if (式1) {
if (式2)
文1;
}
else
文2; if (式1) if (式2) 文1; else 文2;
if (式1) if (式2) 文1; else 文2;
if (a > b && c > a) {
amax = c;
amin = b;
}
if (a > b && c < b) {
amax = a;
amin = c;
}
・・・・・
/**************************************/
/* 3つのデータの最大値と最小値の計算 */
/* coded by Y.Suganuma */
/**************************************/
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double a, b, c, amin, amax;
try {
// データの入力
System.out.print("1つ目のデータを入力して下さい ");
a = Double.parseDouble(in.readLine());
System.out.print("2つ目のデータを入力して下さい ");
b = Double.parseDouble(in.readLine());
System.out.print("3つ目のデータを入力して下さい ");
c = Double.parseDouble(in.readLine());
// a>bの場合
if (a > b) {
if (c > a) {
amax = c;
amin = b;
}
else {
amax = a;
if (c < b)
amin = c;
else
amin = b;
}
}
// a≦bの場合
else {
if (c > b) {
amax = c;
amin = a;
}
else {
amax = b;
if (c < a)
amin = c;
else
amin = a;
}
}
// 出力
System.out.println("最大値="+ amax + " 最小値=" + amin);
}
catch (IOException ignored) {}
}
}
import java.io.*;
public class Test1 {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double a, b, c, amin, amax;
try {
// データの入力
System.out.print("1つ目のデータを入力して下さい ");
a = Double.parseDouble(in.readLine());
System.out.print("2つ目のデータを入力して下さい ");
b = Double.parseDouble(in.readLine());
System.out.print("3つ目のデータを入力して下さい ");
c = Double.parseDouble(in.readLine());
// 初期設定
amax = a;
amin = a;
// 残りの2つのデータと比較する
if (b > amax)
amax = b;
else {
if (b < amin)
amin = b;
}
if (c > amax)
amax = c;
else {
if (c < amin)
amin = c;
}
// 出力
System.out.println("最大値="+ amax + " 最小値=" + amin);
}
catch (IOException ignored) {}
}
}
( A ~ E,及び,F ~ J の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
/****************************/
/* データの比較 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int x = 10, y = 20, z = 30;
int data, sw;
try {
// データの入力
System.out.print("データを入力して下さい ");
data = Integer.parseInt(in.readLine());
// 判定と出力
if (data == x)
System.out.println("xと等しい");
else {
if (data == y)
System.out.println("yと等しい");
else {
if (data == z)
System.out.println("zと等しい");
else
System.out.println("いずれとも等しくない");
}
}
}
catch (IOException ignored) {}
}
}
/****************************/
/* データの比較 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test1 {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int x = 10, y = 20, z = 30;
int data, sw;
try {
// データの入力
System.out.print("データを入力して下さい ");
data = Integer.parseInt(in.readLine());
// 判定と出力
if (data == x)
System.out.println("xと等しい");
else if (data == y)
System.out.println("yと等しい");
else if (data == z)
System.out.println("zと等しい");
else
System.out.println("いずれとも等しくない");
}
catch (IOException ignored) {}
}
}
switch (式) {
[case 定数式1 :]
[文1]
[case 定数式2 :]
[文2]
・・・・・
[default :]
[文n]
}
( A ~ C の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
switch (data) {
case x :
printf("xと等しい\n");
break;
case y :
printf("yと等しい\n");
break;
case z :
printf("zと等しい\n");
break;
default :
printf("いずれとも等しくない\n");
}
/****************************/
/* データの比較 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int x = 10, y = 20, z = 30;
int data, sw;
try {
// データの入力と判定準備
System.out.print("データを入力して下さい ");
data = Integer.parseInt(in.readLine());
if (data == x)
sw = 0;
else {
if (data == y)
sw = 1;
else
sw = (data == z) ? 2 : 3;
}
// 判定と出力
switch (sw) {
case 0 :
System.out.println("xと等しい");
break;
case 1 :
System.out.println("yと等しい");
break;
case 2 :
System.out.println("zと等しい");
break;
default :
System.out.println("いずれとも等しくない");
}
}
catch (IOException ignored) {}
}
}
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double data;
double sum = 0.0;
data = Double.parseDouble(in.readLine());
sum += data;
data = Double.parseDouble(in.readLine());
sum += data;
data = Double.parseDouble(in.readLine());
sum += data;
data = Double.parseDouble(in.readLine());
sum += data;
data = Double.parseDouble(in.readLine());
sum += data;
System.out.println("和=" + sum);
}
}
01 import java.io.*;
02 public class Test {
03 public static void main(String args[]) throws IOException
04 {
05 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
06 double data;
07 double sum = 0.0;
08 for (int i1 = 0; i1 < 5; i1++) {
09 data = Double.parseDouble(in.readLine());
10 sum += data;
11 }
12 System.out.println("和=" + sum);
13 }
14 }
if (論理式) {
文1(複数の文も可)
}
else {
文2(複数の文も可)
}
for (式1; 式2; 式3) {
文(複数の文も可)
}
・・・・・
<while文>
while (式) {
文(複数の文も可)
}
・・・・・
<do while文>
do {
文(複数の文も可)
} while (式) ;
・・・・・
式1;
while (式2) {
文(複数の文も可)
式3;
}
for (式1; 式2; 式3) {
・・・・・
for (式4; 式5; 式6) {
・・・・・
}
・・・・・
}
for (i1 = 0; i1 < 10; i1 = i1+1) {
a = b + c;
for (i2 = 0; i2 < 5; i2 = i2+1) {
bcd = a / y;
aa = b;
・・・・・・・・
}
sum = c + d;
}
( A ~ J ,及び,K ~ U の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
/****************************/
/* 平均値の計算 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
import java.util.*;
public class Test1 {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double sum1, sum2;
int n, x, y;
String line;
StringTokenizer str;
// 初期設定
sum1 = 0.0;
sum2 = 0.0;
n = 0;
try {
// データの数の読み込み
System.out.print("人数は? ");
n = Integer.parseInt(in.readLine());
// データの読み込み
for (int i1 = 0; i1 < n; i1++) {
System.out.print("英語と数学の点は? ");
line = in.readLine();
str = new StringTokenizer(line, " ");
x = Integer.parseInt(str.nextToken());
y = Integer.parseInt(str.nextToken());
sum1 += x;
sum2 += y;
}
}
catch (IOException ignored) {}
// 結果の計算と出力
if (n <= 0)
System.out.println("データがない!");
else {
double mean1 = sum1 / n;
double mean2 = sum2 / n;
System.out.println(" 英語=" + mean1 + " 数学=" + mean2);
}
}
}
/****************************/
/* 平均値の計算 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
import java.util.*;
public class Test2 {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double sum1, sum2;
int n, x, y;
String line;
StringTokenizer str;
// 初期設定
sum1 = 0.0;
sum2 = 0.0;
n = 0;
try {
// データの数の読み込み
System.out.print("人数は? ");
n = Integer.parseInt(in.readLine());
// データの読み込み
int i1 = 0;
while (i1 < n) {
System.out.print("英語と数学の点は? ");
line = in.readLine();
str = new StringTokenizer(line, " ");
x = Integer.parseInt(str.nextToken());
y = Integer.parseInt(str.nextToken());
sum1 += x;
sum2 += y;
i1++;
}
}
catch (IOException ignored) {}
// 結果の計算と出力
if (n <= 0)
System.out.println("データがない!");
else {
double mean1 = sum1 / n;
double mean2 = sum2 / n;
System.out.println(" 英語=" + mean1 + " 数学=" + mean2);
}
}
}
01 import java.io.*;
02 import java.util.*;
03 public class Test {
04 public static void main(String args[])
05 {
06 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
07 double sum1, sum2;
08 int n, x, y;
09 String line;
10 StringTokenizer str;
11 // 初期設定
12 sum1 = 0.0;
13 sum2 = 0.0;
14 n = 0;
15 try {
16 // データの読み込み
17 System.out.print("英語と数学の点は? ");
18 line = in.readLine();
19 str = new StringTokenizer(line, " ");
20 x = Integer.parseInt(str.nextToken());
21 y = Integer.parseInt(str.nextToken());
22 while (x >= 0 && y >= 0) {
23 n++;
24 sum1 += x;
25 sum2 += y;
26 System.out.print("英語と数学の点は? ");
27 line = in.readLine();
28 str = new StringTokenizer(line, " ");
29 x = Integer.parseInt(str.nextToken());
30 y = Integer.parseInt(str.nextToken());
31 }
32 }
33 catch (IOException ignored) {}
34 // 結果の計算と出力
35 if (n <= 0)
36 System.out.println("データがない!");
37 else {
38 double mean1 = sum1 / n;
39 double mean2 = sum2 / n;
40 System.out.println(" 英語=" + mean1 + " 数学=" + mean2);
41 }
42 }
43 }
01 import java.io.*;
02 import java.util.*;
03 public class Test {
04 public static void main(String args[])
05 {
06 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
07 double sum1, sum2;
08 int n, x, y;
09 String line;
10 StringTokenizer str;
11 // 初期設定
12 sum1 = 0.0;
13 sum2 = 0.0;
14 n = 0;
15 try {
16 // データの読み込み
17 do {
18 System.out.print("英語と数学の点は? ");
19 line = in.readLine();
20 str = new StringTokenizer(line, " ");
21 x = Integer.parseInt(str.nextToken());
22 y = Integer.parseInt(str.nextToken());
23 if (x >= 0 && y >= 0) {
24 n++;
25 sum1 += x;
26 sum2 += y;
27 }
28 } while (x >= 0 && y >= 0);
29 }
30 catch (IOException ignored) {}
31 // 結果の計算と出力
32 if (n <= 0)
33 System.out.println("データがない!");
34 else {
35 double mean1 = sum1 / n;
36 double mean2 = sum2 / n;
37 System.out.println(" 英語=" + mean1 + " 数学=" + mean2);
38 }
39 }
40 }
01 /************************************/
02 /* ファイル入出力(平均値の計算後) */
03 /* coded by Y.Suganuma */
04 /************************************/
05 import java.io.*;
06 import java.util.*;
07
08 public class Test {
09 public static void main(String args[]) throws IOException
10 {
11 /*
12 データの読み込み
13 */
14 BufferedReader in = new BufferedReader(new FileReader("input"));
15 double sum1 = 0.0, sum2 = 0.0;
16 String line; // 1行文の文字列
17 int n = 0;
18 while ((line = in.readLine()) != null) {
19 StringTokenizer str = new StringTokenizer(line, " ");
20 sum1 += Double.parseDouble(str.nextToken());
21 sum2 += Double.parseDouble(str.nextToken());
22 n++;
23 }
24 in.close();
25 /*
26 結果の計算と出力
27 */
28 if (n <= 0)
29 System.out.println("データがない!");
30 else {
31 double mean1 = sum1 / n;
32 double mean2 = sum2 / n;
33 PrintStream out = new PrintStream(new FileOutputStream("output"));
34 out.println("人数 " + n + " 英語=" + mean1 + " 数学=" + mean2);
35 out.close();
36 }
37 }
38 }
( A ~ F の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
/************************************/
/* クラス平均と最も良いクラスの出力 */
/* coded by Y.Suganuma */
/************************************/
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
double max = 0.0;
int max_c = 0;
try {
// データの入力と平均値の計算
System.out.print("クラスの数は? ");
int n = Integer.parseInt(in.readLine());
// クラスの数だけ繰り返す
for (int i1 = 0; i1 < n; i1++) {
System.out.print((i1+1) + " 番目のクラスの人数は ");
int m = Integer.parseInt(in.readLine());
double mean = 0.0; // この初期設定はここで必要
// クラスの人数だけ繰り返す
for (int i2 = 0; i2 < m; i2++) {
System.out.print(" " + (i2+1) + " 番目の人の点数は ");
double x = Double.parseDouble(in.readLine());
mean += x;
}
mean /= m;
if (i1 == 0 || mean > max) {
max = mean;
max_c = i1 + 1;
}
}
}
catch (IOException ignored) {}
// 結果の出力
System.out.println("最大平均値はクラス " + max_c + " の " + max + " 点");
}
}
( A ~ D の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
/**************************************/
/* 正しいデータの再入力(do-while文) */
/* coded by Y.Suganuma */
/**************************************/
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int x, y;
try {
do {
System.out.print("1番目の正の整数を入力して下さい ");
x = Integer.parseInt(in.readLine());
System.out.print("2番目の正の整数を入力して下さい ");
y = Integer.parseInt(in.readLine());
} while (x <= 0 || y <= 0);
System.out.println("和は=" + (x+y));
}
catch (IOException ignored) {}
}
}
( A ~ D の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
/****************************/
/* 最大値の計算 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = 5, max = 0;
for (int i1 = 0; i1 < n; i1++) {
System.out.print("データを入力してください ");
int x = Integer.parseInt(in.readLine());
if (x > max)
max = x;
}
System.out.println(" 最大値=" + max);
}
}
/****************************/
/* 最大値の計算 */
/* coded by Y.Suganuma */
/****************************/
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("データを入力してください ");
int max = Integer.parseInt(in.readLine()); // 最初のデータでmaxを初期化
int n = 5;
for (int i1 = 1; i1 < n; i1++) { // i1を1から始める
System.out.print("データを入力してください ");
int x = Integer.parseInt(in.readLine());
if (x > max)
max = x;
}
System.out.println(" 最大値=" + max);
}
}
if (x < 0 && x > max)
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = 5, max = 0, sw = 0; // maxに対して任意の値で初期設定する必要
try {
for (int i1 = 0; i1 < n; i1++) {
System.out.print("データを入力してください ");
int x = Integer.parseInt(in.readLine());
if (x < 0 && (sw == 0 || x > max)) {
max = x;
sw = 1;
}
}
System.out.println(" 最大値=" + max);
}
catch (IOException ignored) {}
}
}
import java.io.*;
import java.util.*;
/*
for 文
*/
001 public class Test {
002 public static void main(String args[])
003 {
004 // (1)入力
005 int i, no = -1, sum1 = 0, sum2 = 0;
006 Scanner sc = new Scanner(System.in);
007 for (; no < 0; ) {
008 System.out.printf("0以上の整数を入力してください ");
009 no = sc.nextInt();
010 }
011 // (2)*と和1
012 for (i = 1; i <= no; i++) { // for (i = 0; i < no; i++) {
013 System.out.printf("*");
014 sum1 += i; // sum1 += (i + 1);
015 }
016 if (no > 0)
017 System.out.printf("\n");
018 System.out.printf("sum1 = %d\n", sum1);
019 // (3)和2
020 System.out.printf("データ? ");
021 i = sc.nextInt();
022 for (; i != 0; ) {
023 sum2 += i;
024 System.out.printf("データ? ");
025 i = sc.nextInt();
026 }
027 System.out.printf("sum2 = %d\n", sum2);
028 }
029 }
/*
while 文
*/
030 public class Test {
031 public static void main(String args[])
032 {
033 // (1)入力
034 int i, no = -1, sum1 = 0, sum2 = 0;
035 Scanner sc = new Scanner(System.in);
036 while (no < 0) {
037 System.out.printf("0以上の整数を入力してください ");
038 no = sc.nextInt();
039 }
040 // (2)*と和1
041 i = 1;
042 while (i <= no) {
043 System.out.printf("*");
044 sum1 += i;
045 i++;
046 }
047 if (no > 0)
048 System.out.printf("\n");
049 System.out.printf("sum1 = %d\n", sum1);
050 // (3)和2
051 System.out.printf("データ? ");
052 i = sc.nextInt();
053 while (i != 0) {
054 sum2 += i;
055 System.out.printf("データ? ");
056 i = sc.nextInt();
057 }
058 System.out.printf("sum2 = %d\n", sum2);
059 }
060 }
/*
do while文
*/
061 public class Test {
062 public static void main(String args[])
063 {
064 // (1)入力
065 int i, no = -1, sum1 = 0, sum2 = 0; // noに対する初期設定は必要なし
066 Scanner sc = new Scanner(System.in);
067 do {
068 System.out.printf("0以上の整数を入力してください ");
069 no = sc.nextInt();
070 } while (no < 0);
071 // (2)*と和1
072 // i = 1;
073 // do {
074 // System.out.printf("*");
075 // sum1 += i;
076 // i++;
077 // } while (i <= no);
078 // if (no > 0)
079 // System.out.printf("\n");
080 if (no > 0) {
081 i = 1;
082 do {
083 System.out.printf("*");
084 sum1 += i;
085 i++;
086 } while (i <= no);
087 System.out.printf("\n");
088 }
089 System.out.printf("sum1 = %d\n", sum1);
090 // (3)和2
091 System.out.printf("データ? ");
092 i = sc.nextInt();
093 // do {
094 // sum2 += i;
095 // System.out.printf("データ? ");
096 // i = sc.nextInt();
097 // } while (i != 0);
098 if (i != 0) {
099 do {
100 sum2 += i;
101 System.out.printf("データ? ");
102 i = sc.nextInt();
103 } while (i != 0);
104 }
105 System.out.printf("sum2 = %d\n", sum2);
106 }
107 }
( A ~ D の部分を可能な限り一つの変数,定数,演算子等で,埋めてください.その際,文字を削除してから,正しい答えを半角文字で,かつ,余分なスペースを入れないで入力してください.)
/**********************************/
/* データの和(負のデータで終了) */
/* coded by Y.Suganuma */
/**********************************/
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException
{
/*
データ数の入力
*/
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("データ数は? ");
int n = Integer.parseInt(in.readLine());
/*
和の計算
*/
int sum = 0;
for (int i1 = 0; i1 < n; i1++) {
System.out.print(" データを入力して下さい ");
int x = Integer.parseInt(in.readLine());
if (x < 0)
break;
else
sum += x;
}
/*
出力
*/
System.out.println("和=" + sum);
}
}
import java.io.*;
public class Test {
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int x = 0, sum = 0; // x の初期化を忘れないこと
try {
// データ数の入力
System.out.print("データ数は? ");
int n = Integer.parseInt(in.readLine());
// 和の計算
for (int i1 = 0; i1 < n && x >= 0; i1++) {
System.out.print(" データを入力して下さい ");
x = Integer.parseInt(in.readLine());
if (x >= 0)
sum += x;
}
// 出力
System.out.println("和=" + sum);
}
catch (IOException ignored) {}
}
}
/**********************************/
/* データの和(負のデータを除外) */
/* coded by Y.Suganuma */
/**********************************/
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException
{
/*
データ数の入力
*/
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("データ数は? ");
int n = Integer.parseInt(in.readLine());
/*
和の計算
*/
int sum = 0;
for (int i1 = 0; i1 < n; i1++) {
System.out.print(" データを入力して下さい ");
int x = Integer.parseInt(in.readLine());
if (x < 0)
continue;
else
sum += x;
}
/*
出力
*/
System.out.println("和=" + sum);
}
}
01 /****************************/
02 /* 変数の有効範囲 */
03 /* coded by Y.Suganuma */
04 /****************************/
05 import java.io.*;
06 import java.util.*;
07
08 public class Test {
09 public static void main(String args[]) throws IOException
10 {
11 int max;
12 Console con = System.console();
13 // データの入力
14 String line = con.readLine("2つのデータをスペースで区切って入力してください ");
15 StringTokenizer str = new StringTokenizer(line, " ");
16 int a = Integer.parseInt(str.nextToken());
17 int b = Integer.parseInt(str.nextToken());
18 // if 文
19 if (a >= b) {
20 max = a;
21 // int a = -5;
22 int min = 0;
23 if (a > b)
24 a = b;
25 System.out.println("最小値 = " + min + ", a = " + a);
26 }
27 else {
28 max = b;
29 int min = a;
30 System.out.println("最小値 = " + min + ", a = " + a);
31 }
32 // 繰り返し文
33 for (int i1 = a; i1 <= b; i1++) {
34 int x = i1 + 5;
35 System.out.println("x = " + x);
36 }
37 // 出力
38 double x = -2.5;
39 System.out.println("最大値 = " + max + ", a = " + a + ", x = " + x);
40 // System.out.println("最小値 = " + min + " i1 = " + i1);
41 }
42 }
最小値 = 1, a = 1 x = 6 x = 7 最大値 = 2, a = 1, x = -2.5
最小値 = 0, a = 1 x = 6 最大値 = 2, a = 1, x = -2.5
| 情報学部 | 菅沼ホーム | 全体目次 | 演習解答例 | 付録 | 索引 |