⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 traveler.java

📁 模拟火星探测器火星漫步的小程序(JAVA编写)
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package marstraveler;import    java.io.*;   import    java.util.*;   /** * * @author Administrator */public class Traveler{    public Site location; //x,y坐标    public Site max;//高原右上角坐标    Traveler(int Mx,int My)    {        max=new Site(Mx,My);        location=new Site(0,0);    }    public void getInfor()    {        System.out.println("目前探测器的位置为:"+location.x+","+location.y);    }    public void set(int x,int y)    {        location.x=x;   //赋值方位        location.y=y;    }    public int moveSet(String path,String dir)    {            String a=new String();           try{           StringTokenizer t=new  StringTokenizer(path," ");//"|"为分隔           while(true)            {                a=t.nextToken();                if(a.equals("L"))                {                    if(dir.equals("N"))                    {                        dir="W";                        continue;                     }                     if(dir.equals("E"))                     {                          dir="N";                          continue;                      }                      if(dir.equals("S"))                      {                           dir="E";                           continue;                       }                       if(dir.equals("W"))                       {                            dir="S";                            continue;                        }                  }                  if(a.equals("R"))                   {                        if(dir.equals("N"))                        {                             dir="E";                             continue;                        }                         if(dir.equals("E"))                         {                              dir="S";                              continue;                         }                          if(dir.equals("S"))                         {                                dir="W";                                continue;                          }                          if(dir.equals("W"))                          {                                         dir="N";                                 continue;                           }                      }                      if(a.equals("M"))                      {                          int mid=0;                                 if(dir.equals("N"))                                 {                                     if((mid=this.location.y+1)>max.y)                                     {                                         System.out.println("错误:探测器移动出了有效范围!!");                                         return 1;                                     }                                     this.location.y++;                                 }                                 if(dir.equals("E"))                                 {                                     if((mid=this.location.x+1)>max.x)                                     {                                         System.out.println("错误:探测器移动出了有效范围!!");                                         return 1;                                     }                                     this.location.x++;                                 }                                 if(dir.equals("S"))                                 {                                     if((mid=this.location.y-1)<0)                                     {                                         System.out.println("错误:探测器移动出了有效范围!!");                                         return 1;                                     }                                      this.location.y--;                                 }                                 if(dir.equals("W"))                                 {                                     if((mid=this.location.x-1)<0)                                     {                                         System.out.println("错误:探测器移动出了有效范围!!");                                         return 1;                                     }                                      this.location.x--;                                 }                       }           }           }           catch(Exception e)           {               return 0;           }    }        /*try       {              String mid=new String();              while(true)              {                  if(path.nextToken()=="L")                  {                      while(path.nextToken()=="M")                                          }              }                }        catch(Exception e)        {            e.printStackTrace();        }*/        public String siteInitial(String s)    {                   int x=0;        int y=0;        String dir=new String();  //方向        StringTokenizer t=new  StringTokenizer(s," ");//"|"为分隔        x=Integer.parseInt(t.nextToken());        y=Integer.parseInt(t.nextToken());        if(x>max.x||y>max.y||x<0||y<0)        {            System.out.print("初始坐标越界!系统默认");            return("error");        }        else        {            set(x,y);                   //set初始位置            dir=t.nextToken();            return dir;        }    }    /*                      if(time==2)                 {                     StringTokenizer t1=new  StringTokenizer(s," ");//" "为分隔                     moveSet(t1,dir);                     move=1;                                                                                          }            }        }        catch(Exception e)        {        }    } */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -