Advertisement
Guest User

Untitled

a guest
Apr 13th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Renshu3_6 {
  4. public static void main(String[] args) {
  5. Random rand = new Random();
  6. Scanner scan = new Scanner(System.in);
  7.  
  8. System.out.println("数当てゲームをします チャンスは5回です");
  9. int ans = rand.nextInt(10);
  10. int num;
  11. outloop: for (int i = 0; i <= 4; i++) {
  12. do {
  13. System.out.print("0~9の数字を入力して下さい(中断は-1を入力)>");
  14. num = scan.nextInt();
  15. if (num == -1) {
  16. break outloop;
  17. }
  18. if (0 <= num && num <= 9) {
  19. break;
  20. }
  21. } while(true);
  22. if (num == ans) {
  23. System.out.println("当たりです!");
  24. break;
  25. } else {
  26. System.out.println("はずれです");
  27. }
  28. }
  29. System.out.println("正解は" + ans + "でした");
  30. System.out.println("ゲームを終了します");
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement