Java常考编程题(基础)--不断更新

来源:互联网 发布:opencv2.4.4 sift算法 编辑:程序博客网 时间:2024/05/18 01:57

Java常考编程题(基础)--不断更新

1.题目:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 

 public class lianxi11 { 

 public static void main(String[] args) { 

 int count = 0; 

 for(int x=1; x<5; x++) { 

 for(int y=1; y<5; y++) { 

 for(int z=1; z<5; z++) { 

 if(x != y && y != z && x != z) {

count ++; 

 System.out.println(x*100 + y*10 + z );

       } 

     }

   } 

 } 

 System.out.println("共有" + count + "个三位数");

 }

 }

2.题目:输入三个整数x,y,z,请把这三个数由小到大输出。   

 import java.util.*;

 public class lianxi15 { 
public static void main(String[] args) {   

  input fnc = new input();     

 int x=0, y=0, z=0;      

 System.out.print("输入第一个数字:");    

 x = fnc.input();   

    System.out.print("输入第二个数字:");    

   y = fnc.input();   

   System.out.print("输入第三个数字:");    

   z = fnc.input();     

   if(x > y) {     

  int t = x;    

  x = y;       y = t;    

  }      

if(x > z) {   

    int t = x;     

  x = z;       z = t;    

 }    

  if(y > z) {     

  int t = y;   

    y = z;     

  z = t;  

    }    

 System.out.println( "三个数字由小到大排列为: "+x + " " + y + " " + z);

 } 

}  

class input{  public int input() {    

  int value = 0;    

   Scanner s = new Scanner(System.in);   

   value = s.nextInt();   

   return value; 


3.题目:输出9*9口诀。   

   public class lianxi16 {  

public static void main(String[] args) {  

    for(int i=1; i<10; i++) {   

    for(int j=1; j<=i; j++) {    

     System.out.print(j + "*" + i + "=" + j*i + "    " );      

    if(j*i<10){System.out.print(" ");

}

 } 
          System.out.println();  

  } 

}

4.题目:对10个数进行排序   

 import java.util.*; 

public class lianxi28 {  

public static void main(String[] args) { 

Scanner s = new Scanner(System.in);   

 int[] a = new int[10];  

  System.out.println("请输入10个整数:");  

 for(int i=0; i<10; i++) {    

 a[i] = s.nextInt();  

 }    

 for(int i=0; i<10; i++) { 

   for(int j=i+1; j<10; j++) {    

  if(a[i] > a[j]) {    

   int t = a[i];    

   a[i] = a[j];    

   a[j] = t;     

 }    

 }    

}   

 for(int i=0; i<10; i++) {   

   System.out.print(a[i] + " "); 

   }

 } 

}

5.Java继承、多态面试题:

1.父类:

publicclassA {  

publicint a = 0;  

publicvoidfun(){  

System.out.println("-----A-----");  

}

}

子类:publicclassB extends A{ 

publicint a = 1;  

publicvoidfun(){  

System.out.println("-----B-----");  

}

主方法:

publicstaticvoidmain(String[] args){  

A classA =new B();  

System.out.println(classA.a);  

classA.fun();  

}  

}


2.父类:

public class Monkey{
public Monkey (String s)
{
System.out.println(s);
}
public void speak()
{
System.out.println("咿咿呀呀......");
}
}


子类:

public People extends Monkey {
public People() {}
public void speak()
{
System.out.println("小样的,不错嘛!会说话了!");
}
public void think()
{
System.out.println("别说话!认真思考!");
}
}


子类:

public class E{
public void main(String[] args) {
Monkey monkey = new Monkey("我是猴子");
monkey.speak();
People people = new People();
people.speak();
people.think();
}
}

0 0