プログラミング学習 masa blog (pa○zaで就職できるのか!?)

masaです。 持病(アトピー)と格闘しながら、家事育児を行い 且つプログラミング学習をしつつ転職を夢見る一般男性。 主に、プログラミング学習、アトピー、育児について思ったことを自分のメモがわり書いていくつもりです。 恥ずかしいし頭悪いからあんまり見ないでね。

java 整数 出し方 randomの書き方

ただの覚書

 

数の表示とサイコロ

public class Main { public static void main(String args) {

double rand = Math.random() * 〇〇 + △△;  //△△から〇〇個の整数 System.out.println(rand); int number = (int)rand; //整数にする キャスト System.out.println(number);

 }

}

  

サイコロであれば

public class Main { public static void main(String args) {

double rand = Math.random() * 6 + 1;   // 1から6個の整数

System.out.println(rand); int number = (int)rand; //整数にする キャスト System.out.println(number);

 }

}

 

 

public class Main {public class Main { public static void main(String args) {

int apple_price =  350;  //リンゴの単価

int apple_num = (int)(Math.random() * 10 + 1);    //リンゴを買う数(1~10)

System.out.println("リンゴの単価" + apple_price + "円 ");

System.out.println("リングを買う数" + apple_num + "個");

int total = apple_price * apple_num ; System.out.println("合計金額" +  total + "円");

    }

  }

 

public class Main {

  public static void main(String args) {

    int age = (int)(Math.random() * 5 + 18);  //18~22までのランダムな数字

     System.out.print(age + "才は");

   }

 }