JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區 » 數獨、猜數字  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 猜電腦給的數字(陽春邏輯版)
feather0508





發文: 1
積分: 0
於 2017-11-30 12:11 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
程式新手完全用邏輯寫出來的....
判斷無論如何都看不懂的話,就留言吧,解釋後應該就很簡單了
請多指教
==========以下為程式原碼=====================
import java.util.Random;
import java.util.Scanner;
public class mygame
{
  
  
  public static void main(String args[])
  {
    int one=0,two=0,three=0,four=0,a=-1,b=-1,c=-1,d=-1,n=5;  
    //四個0-9間不同數字
    while((one==two) || (one==three) || (one==four) || (two==three) || (two==four) || (three== four))
    {
      one=new Random().nextInt(10);
      two=new Random().nextInt(10);
      three=new Random().nextInt(10);
      four=new Random().nextInt(10);
      
    }
    Scanner sc=new Scanner(System.in);
    System.out.println("請輸入想猜測的次數");
    n=sc.nextInt();
    System.out.println("請猜測電腦選擇的不重複的0-9的數字四個");
    
    for(int i=1;i<=n;i++)
    {
      //try-catch如有人輸入非數字時處理
      try
      {
        System.out.println("請輸入第一個數字)");
        a=sc.nextInt();
        System.out.println("請輸入第二個數字)");
        b=sc.nextInt();
        System.out.println("請輸入第三個數字)");
        c=sc.nextInt();
        System.out.println("請輸入第四個數字)");
        d=sc.nextInt();
      }
      catch(Exception e)
      {
        System.out.println("請乖乖輸入數字喔");
      }
    
      //4A判斷
      if((a==one) && (b==two) && (c==three) && (d==four))
      {
        System.out.println("4A,恭喜猜對了");
        break;
      }
      //3A判斷
      else if(((a==one)&&(b==two)&&(c==three))||
      ((a==one)&&(b==two)&&(d==four))||
      ((a==one)&&(c==three)&&(d==four))||
      ((b==two)&&(c==three)&&(d==four)))
      {
        System.out.print("3A");
        //3A1B判斷
        if((a==two)||(a==three)||(a==four)||
        (b==one)||(b==three)||(b==four)||
        (c==one)||(c==two)||(c==four)||
        (d==one)||(d==two)||(d==three))
        {
          System.out.print("1B");
        }
      }
      //2A判斷
      else if(((a==one)&&(b==two))||((a==one)&&(c==three))||((a==one)&&(d==four))||
      ((b==two)&&(c==three))||((b==two)&&(d==four))||
      ((c==three)&&(d==four)))
      {
        System.out.print("2A");
        //2A2B判斷
        if((((a==two)||(a==three)||(a==four))&&(b==one)||(b==three)||(b==four))||
        (((a==two)||(a==three)||(a==four))&&((c==one)||(c==two)||(c==four)))||
        (((a==two)||(a==three)||(a==four))&&((d==one)||(d==two)||(d==three)))||
        (((b==one)||(b==three)||(b==four))&&((c==one)||(c==two)||(c==four)))||
        (((b==one)||(b==three)||(b==four))&&((d==one)||(d==two)||(d==three)))||
        (((c==one)||(c==two)||(c==four))&&((d==one)||(d==two)||(d==three)))
        )
        {
          System.out.print("2B");
        }
        //2A1B判斷
        else if((a==two)||(a==three)||(a==four)||
        (b==one)||(b==three)||(b==four)||
        (c==one)||(c==two)||(c==four)||
        (d==one)||(d==two)||(d==three)
        )
        {
          System.out.print("1B");
        }
      }
      //1A判斷
      else if((a==one)||(b==two)||(c==three)||(d==four))
      {
        System.out.print("1A");
        //1A3B判斷
        if((((a==two)||(a==three)||(a==four))&&((b==one)||(b==three)||(b==four))&&((c==one)||(c==two)||(c==four)))||
        (((a==two)||(a==three)||(a==four))&&((b==one)||(b==three)||(b==four))&&((d==one)||(d==two)||(d==three)))||
        (((a==two)||(a==three)||(a==four))&&((c==one)||(c==two)||(c==four))&&((d==one)||(d==two)||(d==three)))||
        (((b==one)||(b==three)||(b==four))&&((c==one)||(c==two)||(c==four))&&((d==one)||(d==two)||(d==three)))
        )
        {
          System.out.print("3B");
        }
        //1A2B判斷
        else if((((a==two)||(a==three)||(a==four))&&(b==one)||(b==three)||(b==four))||
        (((a==two)||(a==three)||(a==four))&&((c==one)||(c==two)||(c==four)))||
        (((a==two)||(a==three)||(a==four))&&((d==one)||(d==two)||(d==three)))||
        (((b==one)||(b==three)||(b==four))&&((c==one)||(c==two)||(c==four)))||
        (((b==one)||(b==three)||(b==four))&&((d==one)||(d==two)||(d==three)))||
        (((c==one)||(c==two)||(c==four))&&((d==one)||(d==two)||(d==three)))
        )
        {
          System.out.print("2B");
        }
        //1A1B判斷
        else if((a==two)||(a==three)||(a==four)||
        (b==one)||(b==three)||(b==four)||
        (c==one)||(c==two)||(c==four)||
        (d==one)||(d==two)||(d==three))
        {
          System.out.print("1B");
        }
      }
      else
      {
        //4B判斷
        if(((a==two)||(a==three)||(a==four))&&
        ((b==one)||(b==three)||(b==four))&&
        ((c==one)||(c==two)||(c==four))&&
        ((d==one)||(d==two)||(d==three))
        )
        {
          System.out.print("4B");
        }
        //3B判斷
        else if((((a==two)||(a==three)||(a==four))&&((b==one)||(b==three)||(b==four))&&((c==one)||(c==two)||(c==four)))||
        (((a==two)||(a==three)||(a==four))&&((b==one)||(b==three)||(b==four))&&((d==one)||(d==two)||(d==three)))||
        (((b==one)||(b==three)||(b==four))&&((c==one)||(c==two)||(c==four))&&((d==one)||(d==two)||(d==three)))
        )
        {
          System.out.print("3B");
        }
        //2B判斷
        else if((((a==two)||(a==three)||(a==four))&&(b==one)||(b==three)||(b==four))||
        (((a==two)||(a==three)||(a==four))&&((c==one)||(c==two)||(c==four)))||
        (((a==two)||(a==three)||(a==four))&&((d==one)||(d==two)||(d==three)))||
        (((b==one)||(b==three)||(b==four))&&((c==one)||(c==two)||(c==four)))||
        (((b==one)||(b==three)||(b==four))&&((d==one)||(d==two)||(d==three)))||
        (((c==one)||(c==two)||(c==four))&&((d==one)||(d==two)||(d==three)))
        )
        {
          System.out.print("2B");
        }
        //1B判斷
        else if((a==two)||(a==three)||(a==four)||
        (b==one)||(b==three)||(b==four)||
        (c==one)||(c==two)||(c==four)||
        (d==one)||(d==two)||(d==three)
        )
        {
          System.out.print("1B");
        }
        //無A無B
        else
        {
          System.out.print("都不是喔~");
        }
    
      }
      //n為使用者輸入的欲猜測次數 i為第幾迴圈
      System.out.println("========還剩下"+(n-i)+"次========");
    }
    
    //電腦一執行抓的隨機答案
    System.out.println("\n 答案: \n第一個數字="+one+"\n第二個數字="+two+"\n第三個數字="+three+"\n第四個數字="+four);
  }
  
  
}


reply to postreply to post
» JWorld@TW »  Java 程式分享區 » 數獨、猜數字

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8