编程题:
1.编写彩票中奖号码,红球1-33,6个,蓝球1-12,1个;
public class Test {
public int[] getNumber(){
int a[] = new int[6];
for(int i=0; i<a.length;i++){
a[i] = (int)(Math.random()*33+1);
for(int j=0; j<i; j++){
boolean bool = false;
if(a[i] == a[j]){
bool = true;
}
while(bool){
a[i] = (int)(Math.random()*33+1);
j = 0;
break;
}
}
}
for(int i=0 ;i<a.length;i++){
for(int j = 0;j<i;j++){
if(a[i] < a[j]){
int c = a[i];
a[i] = a[j];
a[j] = c;
}
}
}
int b[] = new int[7];
for(int i = 0;i<a.length;i++){
b[i] = a[i];
}
b[6] = (int)(Math.random()*12+1);
return b;
}
public static void main(String args[]){
Test test = new Test();
int a[] = test.getNumber();
System.out.println("彩票中奖号码为");
for(int i = 0;i<a.length;i++){
if( i!= 6){
System.out.println("红球: "+ a[i]);
}else{
System.out.println("蓝球:" + a[i]);
}
}
}
}
2.一只公鸡5文钱,一只母鸡3文钱,1文钱买3只小鸡,100文钱买100只鸡,用编程实现;
public class Test {
public static void main(String args[]){
int x,y,z,j=0;
for( x = 1; x < 20; x++){
for( y = 1;y <33; y++){
z = 100-x-y;
if(z%3 == 0 && 5*x+3*y+z/3 == 100){
System.out.println(++j + "公鸡数为:" + x + "母鸡数为:" + y + "小鸡数" + z);
}
}
}
}
}
3.看如下代码,写出结果:
class A {
void f(){System.out.println("A.f");}
}
class B extends A{
void f(int i){System.out.println("B.f");}
}
class C extends B{
void f(){System.out.println("C.f");}
}
public class Test {
public static void main(String args[]){
A a = new A();
a.f();
B b = new B();
b.f();
C c = new C();
c.f();
}
}
结果为:
A.f
A.f
C.f
4.看如下代码,写出结果:
interface A{
int x = 0;
}
class B{
int x = 1;
}
public class Test extends B implements A{
public void list(int x){
System.out.println(x);
}
public static void main(String args[]){
new Test().list(x);
}
}
以上代码在调用new Test().list(x);报错The field x is ambiguous
5.打印近似圆,给定不同半径,圆的大小随之发生改变。
public class Firstpart{
private double cx ;
public static String prictScape(int x){
String s = "";
for(int i = 0;i < x ;i++){
s += " ";
}
return s;
}
class Cricle{
private int x;
private int y;
private int radix;
Cricle(int x,int y,int radix){
this.x = x;
this.y = y;
this.radix = radix;
}
double calrdx(double cy){
cx = x - Math.sqrt( radix * radix - Math.pow( cy - y, 2) );
return cx;
}
void printCircle(){
for(int ry = y + radix; ry >= y - radix ;ry -= 2){
System.out.print(Firstpart.prictScape( (int)(this.calrdx(ry)) ) + "*");
System.out.println(Firstpart.prictScape( ( x - (int)(this.calrdx(ry)) )*2 ) +"*");
}
}
}
public static void main(String[] args){
Firstpart star = new Firstpart();
Cricle circle = star.new Cricle(10,10,9);
circle.printCircle();
}
}