返回列表 發帖

[作業]猜數字遊戲(改編)

請修改猜數字遊戲,如圖


附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.             Scanner sc=new Scanner(System.in);
  7.             int a=1,b=99,ans,n=0;
  8.             double r=(int)(Math.random()*99+1);
  9.            
  10.             while(true)
  11.             {
  12.                 n++;
  13.                 if(a==b)
  14.                 {
  15.                         System.out.println("可惜了,答案是"+r);
  16.                         break;
  17.                 }
  18.                 System.out.print("猜一個"+a+"~"+b+"之間的數字: ");
  19.                 ans=sc.nextInt();
  20.                
  21.                 if(ans<r)
  22.                 {
  23.                         if(ans<a)
  24.                         {
  25.                                 System.out.println("問號!?");
  26.                         }
  27.                         else
  28.                         {
  29.                                 System.out.println("猜得太小!");
  30.                                 a=ans+1;
  31.                         }
  32.                 }
  33.                 else if(ans>r)
  34.                 {
  35.                         if(ans>b)
  36.                         {
  37.                                 System.out.println("問號!?");
  38.                         }
  39.                         else
  40.                         {
  41.                                 System.out.println("猜得太大!");
  42.                                 b=ans-1;
  43.                         }
  44.                 }
  45.                 else
  46.                 {
  47.                         System.out.println("答對了!");
  48.                         break;
  49.                 }
  50.             }
  51.             System.out.println("總共猜了"+n+"次");
  52.     }
  53. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. import java.util.Random;
  3. class Ch01
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner s = new Scanner(System.in);
  8.         int a=1, b=99,ans,n=0;//a= lowest value, b = highest value, ans is the variable we are trying to guess, n is the guesscount
  9.         int r=(int)(Math.random()*99+1); //value we are trying to guess
  10.         while(true)
  11.         {
  12.             n++;
  13.             if(a==b)//if the range for the answer is still the same you print the answer
  14.             {
  15.                 System.out.println("Oof the correct answer is "+r);
  16.                 break;
  17.             }
  18.             System.out.println("guess a value between "+a+"~"+b);
  19.             ans=sc.nextInt(); //input for the guessed number
  20.             if(ans<r)//
  21.             {
  22.                
  23.                 a=ans+1;
  24.                 if(ans<a)
  25.                 {
  26.                     System.out.println("?! What");
  27.                 }
  28.                 else
  29.                 {
  30.                     System.out.println("The value you guessed was too small ");
  31.                     a=ans+1;
  32.                 }
  33.             }
  34.             else if(ans>r)//50>30
  35.             {
  36.                
  37.                 if(ans>b)
  38.                 {
  39.                     System.out.println("?! what");
  40.                 }
  41.                 else
  42.                 {
  43.                     System.out.println("the value is too big");
  44.                     b=ans-1;
  45.                 }
  46.                 b=ans-1;

  47.             }
  48.             else
  49.             {
  50.                 System.out.println("You got the answer right! ");
  51.                 break;
  52.             }


  53.         }
  54.         System.out.println("You guessed in total of"+n+" times");;

  55.     }

  56. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.         public static void main(String[] args)
  5.         {   
  6.                 Scanner sc=new Scanner(System.in);
  7.                 int a=1,b=99,ans,n=0;
  8.                 double r=(int)(Math.random()*99+1);
  9.                 while(true)
  10.                 {
  11.                         n++;
  12.                         if(a==b)
  13.                         {
  14.                                 System.out.println("可惜了,答案是"+r);
  15.                                 break;
  16.                         }

  17.                         System.out.println("猜一個"+a+"~"+b+"之間的數:");
  18.                         ans=sc.nextInt();
  19.                         if(ans<r)
  20.                         {
  21.                                 if(ans<a)
  22.                                 {
  23.                                         System.out.println("是在哈囉?");
  24.                                 }
  25.                                 else
  26.                                 {
  27.                                         System.out.println("猜得太小");
  28.                                         a=ans+1;
  29.                                 }       
  30.                         }

  31.                         else if(ans>r)
  32.                         {
  33.                                 if(ans>b)
  34.                                 {
  35.                                         System.out.println("是在哈囉?");
  36.                                 }
  37.                                 else  
  38.                                 {
  39.                                         System.out.println("猜得太大");
  40.                                         b=ans-1;
  41.                                 }
  42.                         }
  43.                         else
  44.                         {
  45.                                 System.out.println("答對了");
  46.                                 break;
  47.                         }


  48.                 }
  49.                 System.out.println("總共猜了"+n+"次");
  50.         }

  51. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.        Scanner sc=new Scanner(System.in);
  7.        int a=1,b=99,ans,n=0;
  8.        double r=(int)(Math.random()*99+1);
  9.        while(true)
  10.        {
  11.                n++;
  12.                if(a==b)
  13.                {
  14.                        System.out.println("可惜了,答案是"+r);
  15.                        break;
  16.                }
  17.                System.out.println("猜一個"+a+"~"+b+"之間的數字: ");
  18.                ans=sc.nextInt();
  19.                if(ans>r)
  20.                {
  21.                    if(ans>b)
  22.                    {
  23.                            System.out.println("問號?!");
  24.                    }else
  25.                    {
  26.                            System.out.println("猜得太大!");
  27.                        b=ans-1;
  28.                    }   
  29.                       
  30.                }
  31.                else if(ans<r)
  32.                {
  33.                        if(ans<a)
  34.                    {
  35.                            System.out.println("問號?!");
  36.                    }else
  37.                    {
  38.                            System.out.println("猜得太小!");
  39.                        a=ans+1;
  40.                    }   
  41.                }
  42.                else
  43.                {
  44.                        System.out.println("答對了!");
  45.                        break;
  46.                }
  47.        }
  48.        System.out.println("總共猜了"+n+"次");
  49.     }
  50. }
複製代碼

TOP

  1. import java.lang.System;
  2. import java.util.Scanner;
  3. public class Ch01
  4. {
  5.     static Scanner s=new Scanner(System.in);
  6.     public static void main(String[] args)
  7.     {
  8.             int x,y,z,a,b;//x=答案、y=最低值、z=最高值、a=次數、b=答案
  9.             x=(int)(Math.random()*99+1);
  10.             y=1;
  11.             z=99;
  12.             a=0;
  13.             while(true)
  14.             {
  15.                         a+=1;
  16.                     if(y==z)
  17.                     {
  18.                             System.out.println("可惜了,答案是"+x);
  19.                             break;
  20.                     }
  21.                     System.out.print("猜一個"+y+"~"+z+"之間的數字:");
  22.                     b=s.nextInt();
  23.                     if(b>x)
  24.                     {
  25.                                 if(b>z)
  26.                             {
  27.                                  System.out.println("問號!");
  28.                             }
  29.                                 else
  30.                                 {
  31.                                         System.out.println("猜得太大了!");
  32.                                 z=b-1;
  33.                                 }
  34.                     }
  35.                     else if(b<x)
  36.                     {
  37.                             if(b<y)
  38.                             {
  39.                                     System.out.println("問號!");
  40.                             }
  41.                             else
  42.                             {
  43.                                     System.out.println("猜得太小了!");
  44.                             y=b+1;
  45.                             }
  46.                     }
  47.                     else
  48.                     {
  49.                             System.out.println("恭喜你猜對了!");
  50.                             System.out.println("總共猜了"+a+"次!");
  51.                         break;
  52.                     }
  53.             }
  54.         }

  55. }
複製代碼

TOP

本帖最後由 夏子甯 於 2022-2-19 14:19 編輯
  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Scanner sc=new Scanner(System.in);
  7.                 int a=1,b=99,ans,n=0;
  8.                 int r=(int)(Math.random()*99+1);
  9.                 while(true)
  10.                 {
  11.                         n++;
  12.                         if(a==b)
  13.                         {
  14.                                 System.out.println("可惜了,答案是"+r);
  15.                                 break;
  16.                         }
  17.                         System.out.println("猜一個"+a+"~"+b+"之間的數字:");
  18.                         ans=sc.nextInt();
  19.                         if(ans<r && ans>=a && ans<=b)
  20.                         {
  21.                                 System.out.println("猜得太小!");                           
  22.                                 a=ans+1;
  23.                         }
  24.                         else if(ans>r && ans>=a && ans<=b)
  25.                         {
  26.                                 System.out.println("猜得太大!");
  27.                                 b=ans-1;
  28.                         }
  29.                         else if(ans<a || ans>b)
  30.                         {
  31.                                 System.out.println("問號?");
  32.                         }
  33.                         else
  34.                         {
  35.                                 System.out.println("答對了!");
  36.                                 System.out.println("總共猜了"+n+"次");
  37.                                 break;
  38.                         }
  39.                 }
  40.         }
  41. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class CH01 {

  3.         public static void main(String[] args) {
  4.                 Scanner sc=new Scanner(System.in);
  5.                 int ans=(int)(Math.random()*99+1);
  6.                 int n,a=1,b=99,x=0;
  7.                 while(true)
  8.                 {
  9.                         x++;
  10.                         System.out.println("猜一個"+a+"~"+b+"之間的數字:");
  11.                         n=sc.nextInt();
  12.                         if(n>ans)
  13.                         {
  14.                                 if(n>b || n<a)
  15.                                 {
  16.                                     System.out.println("問號");
  17.                                            continue;
  18.                             }
  19.                             System.out.println("你猜得太大了");
  20.                             b=n-1;
  21.                         }
  22.                         else if(n<ans)
  23.                         {
  24.                                 if(n>b || n<a)
  25.                                 {
  26.                                         System.out.println("問號");
  27.                                         continue;
  28.                                 }
  29.                             System.out.println("你猜得太小了");
  30.                             a=n+1;
  31.                         }
  32.                         else
  33.                         {
  34.                             System.out.println("恭喜你猜對了");
  35.                             System.out.println("你猜了"+x+"次");
  36.                             break;
  37.                         }
  38.                         if(a==b)
  39.                         {
  40.                                 System.out.println("太可惜了,答案是"+ans);
  41.                                 return;
  42.                         }
  43.                 }
  44.         }      
  45. }
複製代碼

TOP

本帖最後由 盧禹廷 於 2022-2-19 14:25 編輯
  1. import java.util.Scanner;
  2. public class Ch01 {

  3.         public static void main(String[] args) {
  4.                 Scanner sc=new Scanner(System.in);
  5.                 int ans=(int)(Math.random()*99+1);
  6.                 int n,a=1,b=99,x=0;
  7.                 while(true)
  8.                 {
  9.                         x++;
  10.                         System.out.println("猜一個"+a+"~"+b+"之間的數字:");
  11.                         n=sc.nextInt();
  12.                         if(n>ans)
  13.                         {
  14.                                 if(n>b || n<a)
  15.                                 {
  16.                                         System.out.println("問號");
  17.                                         continue;
  18.                                 }
  19.                                 else
  20.                                 {
  21.                                         System.out.println("你猜得太大了");
  22.                                         b=n-1;
  23.                                 }

  24.                         }
  25.                         else if(n<ans)
  26.                         {
  27.                                 if(n>b || n<a)
  28.                                 {
  29.                                         System.out.println("問號");
  30.                                         continue;
  31.                                 }
  32.                                 else
  33.                                 {
  34.                                         System.out.println("你猜得太小了");
  35.                                         a=n+1;
  36.                                 }

  37.                         }
  38.                         else
  39.                         {
  40.                                 System.out.println("恭喜你猜對了");
  41.                                 System.out.println("你猜了"+x+"次");
  42.                                 break;
  43.                         }
  44.                         if(a==b)
  45.                         {
  46.                                 System.out.println("太可惜了,答案是"+ans);
  47.                                 return;
  48.                         }
  49.                 }
  50.         }      
  51. }
複製代碼

TOP

返回列表