当前位置:编程学习 > JAVA >>

一道java面试题,求解!!

一个字符串:“你好world吗”,如果输入3或4,则返回“你好”,如果输入10或11则返回“你好world吗”。
用一个函数实现。 --------------------编程问答-------------------- public String RString(String s, int n) {
if (n == 3 || n == 4) {
String[] result = s.split("");
String res = result[1] + result[2];
return res;

}
if (n == 10 || n == 11) {
return s;
}
return null;
}

这样么? --------------------编程问答-------------------- 楼主啊。你这个都不会还去面试? --------------------编程问答--------------------
引用 1 楼 kenar89 的回复:
public String RString(String s, int n) {
if (n == 3 || n == 4) {
String[] result = s.split("");
String res = result[1] + result[2];
return res;

}
if (n == 10 || n == 11) {
return s;
}
retur……

不是这样的,用byte[]数组,从byte数组里面一个字节一个字节的分析。 --------------------编程问答--------------------
引用 2 楼 dryzeng 的回复:
楼主啊。你这个都不会还去面试?

那你帮我想想啊,先谢谢你!我不知道怎么从字节数组中判断是汉字还是字母,如果这个字节属于汉字的一部分,再怎么处理。 --------------------编程问答-------------------- 怎么从字节数组中判断是汉字还是字母  同样求教 --------------------编程问答-------------------- public class Cheng {
    public static void main(String[] args) {
        int i=0;
        String tom="你好world吗";
        String s=tom.substring(0,2);
        Scanner rd=new Scanner(System.in);
        System.out.println("请输入一个整数:");
           i=rd.nextInt();
        switch(i){
            case 3:System.out.println(s);break;
            case 4:System.out.println(s);break;
            case 11:System.out.println(tom);break;
            case 12:System.out.println(tom);break;
            default:
       }
    }
} --------------------编程问答-------------------- public class Cheng {
    public static void main(String[] args) {
        int i=0;
        String tom="你好world吗";
        String s=tom.substring(0,2);
        Scanner rd=new Scanner(System.in);
        System.out.println("请输入一个整数:");
           i=rd.nextInt();
        switch(i){
            case 3:System.out.println(s);break;
            case 4:System.out.println(s);break;
            case 11:System.out.println(tom);break;
            case 12:System.out.println(tom);break;
            default:
       }
    }
} --------------------编程问答-------------------- public class Cheng {
    public static void main(String[] args) {
        int i=0;
        String tom="你好world吗";
        byte d[]=tom.getBytes();
        String s=new.String(d,0,6);
        Scanner rd=new Scanner(System.in);
        System.out.println("请输入一个整数:");
           i=rd.nextInt();
        switch(i){
            case 3:System.out.println(s);break;
            case 4:System.out.println(s);break;
            case 11:System.out.println(tom);break;
            case 12:System.out.println(tom);break;
            default:
       }
    }
} --------------------编程问答-------------------- 这到底是什么世道呀?唉,除了感叹还是感叹,我去面试时怎么就没发现这种类型的呢 --------------------编程问答-------------------- 不知道是不是楼主需要的!
package chineseTest;

import java.io.UnsupportedEncodingException;

import org.junit.Test;


public class ChineseTest {

@Test
public void testChar(){
String str = "你好wor我ld吗";
try {
System.out.println(this.returnStr(str, 12));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String returnStr(String str,int len) throws UnsupportedEncodingException{
byte[] strByte = null ;
StringBuffer sb = new StringBuffer();
if(str != null && len < str.getBytes("GBK").length){
for(int i = 0 ; i< len ; i++){
sb.append(str.charAt(i));
if(this.isChinese(str.charAt(i))){
len--;
}
}
}
return sb.toString();
}
public boolean isChinese(char ch) throws UnsupportedEncodingException{
if(String.valueOf(ch).getBytes("GBK").length>1){
return true;
}else{
return false;
}
}
}
--------------------编程问答-------------------- import java.util.Scanner;


public class TheMain {
  public static void main(String[] args) {
  int i=0;
  byte tom[ ] = "你好world吗".getBytes( );
  String s1 = new String( tom, 0, 4 );
  Scanner reader = new Scanner(System.in);
  System.out.println( "请输入一个整数:" );
  i = reader.nextInt( );
  switch( i ){
  case 3:System.out.println( s1 );break;
  case 4:System.out.println( s1 );break;
  case 11:System.out.println( tom );break;
  case 12:System.out.println( tom );break;
  default:
  }
  }
} --------------------编程问答-------------------- 全部输入字符串,自个写去。刚没写好!
引用 11 楼 zdxing007 的回复:
import java.util.Scanner;


public class TheMain {
  public static void main(String[] args) {
  int i=0;
  byte tom[ ] = "你好world吗".getBytes( );
  String s1 = new String( tom, 0, 4 );
  Scann……
--------------------编程问答-------------------- 我表示不太懂这题啥意思 --------------------编程问答--------------------
引用 4 楼 fu504224384 的回复:
引用 2 楼 dryzeng 的回复:
楼主啊。你这个都不会还去面试?

那你帮我想想啊,先谢谢你!我不知道怎么从字节数组中判断是汉字还是字母,如果这个字节属于汉字的一部分,再怎么处理。


哪个说要判断是不是汉字?楼上那么多,你看嘛,用substring简单明了。 --------------------编程问答--------------------
引用 10 楼 pingchangxinli 的回复:
不知道是不是楼主需要的!

Java code
package chineseTest;

import java.io.UnsupportedEncodingException;

import org.junit.Test;


public class ChineseTest {

    @Test
    public void testChar(){
   ……

谢谢你! --------------------编程问答--------------------

import java.util.Scanner;


public class Student{
public static void main(String args[])
{
subString();
}
public  static String subString()
{
String string="你好world吗";
Scanner scanner=new Scanner(System.in);
int i=scanner.nextInt();
if(i==3||i==4)
{
System.out.println(string.subSequence(0, 2));
return string.substring(0,2); 

}
if(i==10|| i==11)
{
System.out.println(string);
return string;
}
return null;
}
}
--------------------编程问答--------------------

import java.util.Scanner;


public class Student{
public static void main(String args[])
{
subString();
}
public  static String subString()
{
String string="你好world吗";
Scanner scanner=new Scanner(System.in);
try {
int i=scanner.nextInt();
if(i==3||i==4)
{
System.out.println(string.subSequence(0, 2));
return string.substring(0,2); 

}
if(i==10|| i==11)
{
System.out.println(string);
return string;
}
return null;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.toString()+"非正常输入!");
return null;
}
}
}
刚刚哪个忘记异常了  不知道是不是楼主想要的 --------------------编程问答-------------------- 是不是需要尝试将两个byte转成字符与unicode进行比对,再处理? --------------------编程问答-------------------- 都别吵了, 按照楼主的意思, 其实是这样的: 

// n = Input
if(n == 3 || n == 4)
    return "你好";
else if(n == 11 || n == 12)
    return "你好world吗?"
else
    handleBadInput(n);

这不就行了吗? 完全符合楼主的意思(至少楼主在0层楼上是这么说的).  --------------------编程问答--------------------
引用 8 楼 fenggedeshiji 的回复:
public class Cheng {
    public static void main(String[] args) {
        int i=0;
        String tom="你好world吗";
        byte d[]=tom.getBytes();
        String s=new.String(d,0,6);
        Sc……

天啊,这是最基础的啊 --------------------编程问答-------------------- if(n == 3 || n == 4)
    return "你好";
else if(n == 11 || n == 12)
    return "你好world吗?"
else
    handleBadInput(n);


--------------------编程问答--------------------


public static void main(String[] args) throws Exception{
String str = "我a爱中华abc我爱传智def';
String str = "我ABC汉";
int num = trimGBK(str.getBytes("GBK"),5);
System.out.println(str.substring(0,num) );
}

public static int  trimGBK(byte[] buf,int n){
int num = 0;
boolean bChineseFirstHalf = false;
for(int i=0;i<n;i++)
{
if(buf[i]<0 && !bChineseFirstHalf){
bChineseFirstHalf = true;
}else{
num++;
bChineseFirstHalf = false;
}
}
return num;
}

--------------------编程问答--------------------

Scanner scanner = new Scanner(System.in);
String str = "你好word吗";
System.out.println("请输入一个数:"); 
int a = scanner.nextInt();
if(a == 3 || a == 4) {
System.out.println(str.substring(0, 2)); 
} else if(a == 10 || a == 11) {
System.out.println(str);
} else {
System.out.println("你输入有误,重新输入:");
a = scanner.nextInt();
}

--------------------编程问答-------------------- 19# 21#忒搞~ --------------------编程问答--------------------
引用 24 楼 seadplus 的回复:
19# 21#忒搞~

--------------------编程问答-------------------- 我想知道为什么会出这么个面试题? 
--------------------编程问答-------------------- 作业题吧,楼主!

public static String SubString(String str, int len)
{
byte[] b = null;
try
{
b = str.getBytes("Unicode");
}
catch (Exception ex)
{
ex.printStackTrace();
}
int i = 2;
int n = 0;
for (; i < b.length && n < len; i++)
{
if (i % 2 == 1)
{
n++;
}
else
{
if (b[i] != 0)
{
n++;
}
}
}
if (i % 2 == 1)
{
if (b[i - 1] != 0)
{
i--;
}
else
{
i++;
}
}
String temp = null;
try
{
temp = new String(b, 0, i, "Unicode");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return temp;
}
--------------------编程问答-------------------- 这个题目超难啊 --------------------编程问答-------------------- 同意19L --------------------编程问答-------------------- 要不要这么恼火啊,我看大家思维好复杂啊,这个是基础题啊。
21楼回答的不错啊,就是建立一个函数,
然后在主函数中直接调用里面的,输入3或者4=你好,
输入11或者12=你好我world吗
怎么看你们都弄出什么数据来了,
应该就是一个判断吧,
反正我是新手,学习JAVA才几天,所以思维没你们那么复杂。。
下面是代码:
class  interview
{
public static void world(int a)
{
if (a==3 || a==4)
{
System.out.println("你好");
}
else if (a==11 ||a==12)
{
System.out.println("你好World吗");
}
else
System.out.println("请输入整数");
}

public static void main(String[] args) 
{
world (4);
}
}

求各位大神嘴下留德,我只是新手,用新人的方式...表喷~~ --------------------编程问答-------------------- public class Fun {
private static String hello = "你好";
private static String helloWorld = "你好World吗";
private static String error = "老子不玩了";
public static void main(String[] args) {

while(true){
System.out.println("请输入数字");
Scanner sc = new Scanner(System.in);
String inString = sc.next();
if(error.equalsIgnoreCase(inString)){
System.err.println("不送走好");
break;
}
test(inString);
}
}
public static void test(String inString){
if("3".equalsIgnoreCase(inString)){
System.out.println(hello);;
} else if("4".equalsIgnoreCase(inString)){
System.out.println(hello);;
} else if("12".equalsIgnoreCase(inString)){
System.out.println(helloWorld);;
} else if("11".equalsIgnoreCase(inString)){
System.out.println(helloWorld);;
} else{
System.err.println("请输入3-4,11-12数字");
}
}
} --------------------编程问答--------------------
引用 30 楼  的回复:
要不要这么恼火啊,我看大家思维好复杂啊,这个是基础题啊。
21楼回答的不错啊,就是建立一个函数,
然后在主函数中直接调用里面的,输入3或者4=你好,
输入11或者12=你好我world吗
怎么看你们都弄出什么数据来了,
应该就是一个判断吧,
反正我是新手,学习JAVA才几天,所以思维没你们那么复杂。。
下面是代码:
class  interview
{
public stat……
你太搞了,完全把题目的意思给弄错了 --------------------编程问答--------------------
引用 30 楼  的回复:
要不要这么恼火啊,我看大家思维好复杂啊,这个是基础题啊。
21楼回答的不错啊,就是建立一个函数,
然后在主函数中直接调用里面的,输入3或者4=你好,
输入11或者12=你好我world吗
怎么看你们都弄出什么数据来了,
应该就是一个判断吧,
反正我是新手,学习JAVA才几天,所以思维没你们那么复杂。。
下面是代码:
class interview
{
public static……


你楼主的题目没看清楚 --------------------编程问答-------------------- 汉字占两个字节

public static void main(String[] args)
{
String str="你好world吗";
byte[] d=str.getBytes();

Scanner in=new Scanner(System.in);
System.out.print("n:");
int n=in.nextInt();

if(n==3||n==4)
{
String s=new String(d,0,4);
System.out.println(s);
}
else if(n==10||n==11)
{
String s=new String(d,0,11);
System.out.println(s);
}
} --------------------编程问答-------------------- 你好world吗 
很显然,一个汉字 两个长度空间  
3,或4代表的是好字的
10或11代表吗字
你(1,2),好(3,4)w(5)o(6)r(7)l(8)d(9)吗(10,11)
所以要转 
--------------------编程问答--------------------
引用 22 楼  的回复:
Java code



public static void main(String[] args) throws Exception{
        String str = "我a爱中华abc我爱传智def';
        String str = "我ABC汉";
        int num = trimGBK(str.getBytes("GBK"),5);
  ……


这楼的能解决LZ的问题,想法灰常不错的 --------------------编程问答-------------------- 学习学习 --------------------编程问答-------------------- #35楼那个分析得很对··学习学习 --------------------编程问答-------------------- import java.util.Scanner;
public class Cheng 
{
  public static void main(String[] args) 
 {
  int i=0;
  String tom="你好world吗";
  String s=tom.substring(0,2);
  Scanner rd=new Scanner(System.in);
  System.out.println("请输入一个整数:");
  i=rd.nextInt();
  switch(i)
   {
  case 3:System.out.println(s);break;
  case 4:System.out.println(s);break;
  case 11:System.out.println(tom);break;
  case 12:System.out.println(tom);break;
  default:
   }
  }
}
1.String s=new String(d,0,9);
2.String t = new String(d,9,2);
为什么第一行是(d,0.9);而不是(d,1,9);
还有第二行为什么是(d,9,2)而不是(d,10,2);
难倒是从(d,n,2);中第n项以后的两项吗? --------------------编程问答-------------------- 学习中。。。。看到你们这里面的前辈的想法都不错
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,