Generation

generate functionSun, 23 Apr 2023

以下の要件を満たすプログラムを作成せよ。 1. 1~5までの乱数を発生させ、変数に格納する。 2. 上記1.の変数の値を表示する。 3. 上記1.の変数の値が3に等しい場合は、ループを強制的に抜け出す。 4. 上記1.~3.をwhile ループで繰り返し実行する。ただし、while の条件式はtrue とすること。

import java.util.Random; public class MethodPractice { public static void main(String[] args) { Random random = new Random(); int x = random.nextInt(5) + 1; System.out.println(x); if(x == 3){ System.exit(1); } } }

Questions about programming?Chat with your personal AI assistant