java试题1:
请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。 
java试题2:
编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。 
java试题3:
编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。
十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2,这次得到的余数就是次低位,如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。 
java试题4:
请用移位的方式打印出一个十进制整数的十六进制形式。提示:按每4个二进制位对整数进行移位和去高位处理,得到的结果就是十六进制数的一位,然后按下面三种方式之一(作为作业,要求每种方式都用到)计算出一个十六进制数值对应的十六进制形式: 
1)0-9之间的数值直接加上字符'0',9以上的数值减去10以后再加上字符'A' 
2)定义一个数组,其中包含0-F这些字符,然后用要计算的数值作为数组的索引号,即可获得其对应的十六进制数据。 
3)Character.forDigit静态方法可以将一个十六进制的数字转变成其对应的字符表示形式,例如,根据数值15返回字符'F'。

解决方案 »

  1.   

    前两个用C++我会做,用java初学者不好意思。不会。求教!用什么开发工具适合我这样的java初学者?》
      

  2.   

    第一个:用String的方法indexOf()来做。
    第二个:取数组的每个元素:
    class Test8{ public static void main(String[] args){

    int[] a = {1,2,3,4,5};
    for(int i = 0;i < a.length;i++){
         if(i != (a.length-1)){    
             System.out.print(a[i]+",");
         }
         else{
             System.out.print(a[i]);
         }
    }
    }
    }其它不会了。呵呵!
      

  3.   

    import java.io.IOException;
    import mySrc.Console;public class Search_char { /**
     * search the first positon of char which been tranfered
     * @param form main function
     *  @param return a int type on the condition of finding 
     *  @param return -1 if no found
     */
     static int Search(char aSt[],char aChar){
     boolean findFlag = false;
     int i =0;
     for (i=0;i<aSt.length;i++)
    {if ( aChar==aSt[i])
    {//System.out.print(" "+aSt[i]+" "+i );
     findFlag = true;
    break;
    }
    }
     if (findFlag)
      return i;
     else
      return -1;

    }

    public static void main(String[] args) throws IOException { String aStr=  new String("High Technology Zone");
    char[] charArray = new char[aStr.length()]; 
    aStr.getChars(0,aStr.length(),charArray,0);
    String str=Console.readLine("Please input a String");
    int aInt = Search(charArray,str.charAt(0));
    if (aInt==-1)
    System.out.println("NO FOUND the char "+str.charAt(0));
    else
    System.out.println("Find the char of " +str.charAt(0)+" at "+aInt + " among "+charArray.length);

    }}
    //第一题的一种方法
      

  4.   

    import java.io.IOException;
    import mySrc.Console;public class Search_char { /**
     * search the first positon of char which been tranfered
     * @param form main function
     *  @param return a int type on the condition of finding 
     *  @param return -1 if no found
     */
     static int Search(char aSt[],char aChar) throws IllegalArgumentException{
     boolean findFlag = false;
     
     if (aSt == null )
     //throw new Exception();
     throw new IllegalArgumentException();
      
      int i =0;
      for (i=0;i<aSt.length;i++)
    {if ( aChar==aSt[i])
    {//System.out.print(" "+aSt[i]+" "+i );
     findFlag = true;
    break;
    }
    }
     if (findFlag)
      return i;
     else
      return -1;

    }
     
     
    public static void main(String[] args) throws IOException { String aStr=  new String("High Technology Zone");
    //aStr=null;
    char[] charArray = new char[aStr.length()]; 
    aStr.getChars(0,aStr.length(),charArray,0);
    String str=Console.readLine("Please input a String");
    int aInt = Search(charArray,str.charAt(0));
    if (aInt==-1)
    System.out.println("NO FOUND the char "+str.charAt(0));
    else
    System.out.println("Find the char of " +str.charAt(0)+" at "+aInt + " among "+charArray.length);

    }}
    //第一题的一种方法
    //加上了异常处理
      

  5.   

    简单表达一下第一题public int searchChar(char[] srcCharArray,char toBeSearch) {
      String str = new String(srcCharArray);
      int index = str.indexOf(toBeSearch);
      return index;
    }
      

  6.   

    /*
     * author: vincey gale
     * mail : [email protected]
     * web : http://me.52inter.net
     * 此类只能用来处理long型以内的整数十进制到二进制的转换!
     */
    public class TenToTwo {
    public static void main(String[] args) throws Exception {
    String s=new String();
    System.out.print("请输入一个整数 : ");
    s=readString();
    while(isNumString(s)==0||isNumString(s)==-1){
    if(isNumString(s)==0){
    System.out.print("你输入的不是一个整数,请重新输入 : "); //如果输入的数不是一个整数,要求搬弄是非新输入
    s=readString();
    }
    else{
    System.out.print("你输入的数值太大,超过了处理范围,请重新输入 : ");
    s=readString();
    }
    }
    Integer ten=new Integer(s.trim());
    System.out.print(ten+" 的二进制编码是: ");
    toTwo(ten.doubleValue()); //调用toTwo(double db)进行结果打印
    System.out.println();
    } static void toTwo(double num){   //把十进制数转为二进制
    int LEN=16;
    if (num>65535||(0-num)>65535) //如果数较大,则用长度为32的数组进行存放它的二进制形式
    LEN=32;
    Integer two[]=new Integer[LEN]; //开辟用于存放二进制形式的数组
    for(int i=0;i<LEN;i++)
    two[i]=new Integer(0); //为数组各个元素赋初值0
    int len=0;
    if(num<0){ //对负数进行处理
    two[0]=new Integer(1);
    num=0-num;
    }
    do{ //实现了十进制到二进制的转换
    two[LEN-1-len]=new Integer(((int)num)%2); //
    len++; //
    num=Math.floor(num/2); //
    } //
    while(num>0); //实现了十进制到二进制的转换
    for(int i=0;i<LEN;i++){
    System.out.print(two[i]);
    if((i+1)%8==0)
    System.out.print(" ");
    }
    } static String readString() throws Exception{ //用于返回从流中读到的数据
    StringBuffer sb=new StringBuffer();
    char ch=' ';
    while((ch=(char)(System.in.read()))!='\r'){
    sb.append(ch);
    }
    System.out.println(sb.toString().trim());
    return sb.toString().trim();
    } static int isNumString(String s){ //对String对象里的元素进行逐个扫描,一但发现有非数字元素就返回0,(第一个元素可以存放负号(-))
    int rt=1, //默认返回值是1,即用户输入的是一个正数
    len=s.length();
    char[] c=new char[len];
    c=s.toCharArray();
    if((c[0]=='-'&&len-1>9)||len>9) //因为double型数据的最大长度就是10 :->
    rt=-1; //方法中使用-1表示数据过大
    else
    for(int i=1;i<len;i++){
    if(c[i]-'0'>9)
    rt=0; //使用0表示字符串内有非数字元素
    else
    rt=1;
    }
    return rt;
    }
    }第三题的一种解法,第四题的解法和它相似:
    就是一个小的方法有所改变
    static void toSixteen(double num){
    int LEN=8,mod=0,i=0;
    String str=new String("0123456789ABCDEF");
    char[] it=new char[LEN];
    char[] ch=str.toCharArray();
    for(i=0;i<LEN;i++)
    it[i]=0;
    i=0;
    do{
    mod=(int)num%16;
    it[LEN-1-i]=ch[(int)mod];
    num=Math.floor((double)(num/16));
    i++;
    }
    while(num>0);
    for(i=0;i<LEN;i++){
    System.out.print(it[i]);
    if((i+1)%4==0)
    System.out.print(" ");

    }
    }