Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Renshu3_6 {
- public static void main(String[] args) {
- Random rand = new Random();
- Scanner scan = new Scanner(System.in);
- System.out.println("数当てゲームをします チャンスは5回です");
- int ans = rand.nextInt(10);
- int num;
- outloop: for (int i = 0; i <= 4; i++) {
- do {
- System.out.print("0~9の数字を入力して下さい(中断は-1を入力)>");
- num = scan.nextInt();
- if (num == -1) {
- break outloop;
- }
- if (0 <= num && num <= 9) {
- break;
- }
- } while(true);
- if (num == ans) {
- System.out.println("当たりです!");
- break;
- } else {
- System.out.println("はずれです");
- }
- }
- System.out.println("正解は" + ans + "でした");
- System.out.println("ゲームを終了します");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement