Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created October 5, 2016 12:27
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/df6780d9d79e0ba33b3f1bb2aaceec53 to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/df6780d9d79e0ba33b3f1bb2aaceec53 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習2-7
//乱数のインポート文
import java.util.Random;
public class En2_7 {
public static void main(String[] args) {
// 乱数読み込み
Random rand = new Random();
// 0~8までの乱数作成し、それに+1する。
int ransu1 = rand.nextInt(9) + 1;
// 結果を出力
System.out.println(" 1~9までの乱数を生成:"+ransu1);
System.out.println();
// 0~8までの乱数作成し、それに+1する。
int ransu2 = rand.nextInt(9) + 1;
// 結果を出力する際にマイナスをかける。
System.out.println("-1~-9までの乱数を生成:"+-ransu2);
System.out.println();
// 0~89までの乱数作成し、それに+10する。
int ransu3 = rand.nextInt(90) + 10;
// 結果を出力
System.out.println("10~99までの乱数を生成:"+ransu3);
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment