黑马程序员—JAVA 基础测试2

package com.itheima;


/**
*6.用控制台程序倒着输出九九乘法表;输出结果按下图所示:


1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81


……


1*3=3 2*3=6 3*3=9


1*2=2 2*2=4


1*1=1
* @author Administrator
*/


class Test6 {
public static void main(String[] args) {
for (int i=9; i>=1;i-- ){
for (int j=1;j<=i ;j++ ){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
}
package com.itheima;


/**
7.声明类Person,包含2个成员变量:name、age。定义函数sayHello(),
调用时输出:我叫***,今年***岁了。声明类Chinese继承Person。
* @author Administrator
*/


import java.io.*;
public class Test7 {
public static void main(String[] args) throws Exception{
System.out.println("请输入“人名”和他的“年龄”");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String q=br.readLine();
Integer p=Integer.parseInt(br.readLine());
Chinese man=new Chinese(q,p);
man.sayHello();
}
}


class Person{
private String name;
private int age;
Person(String name,int age){
this.name=name;
this.age=age;
}
public void sayHello(){
System.out.println("我的名字叫:"+name+",今年"+age+"岁了");
}
}


class Chinese extends Person {
Chinese(String name,int age){
super(name,age);
}
public void sayHello(){
super.sayHello();
}
}

package com.itheima;
/**
8. throw和throws有什么区别? try、catch、finally分别在什么情况下使用?
答:
一.throw用于抛出一个异常类对象,通常用于处理自定义异常类情况,如 throw new MyException() 
throws则是在方法声明时告诉调用者该方法需要抛出什么类型的异常。 
而异常的捕获,处理交由调用该方法者去实施,如 int parselnt(String s)throws NumberFormatException。 
并且throw和throws的用法是完全不同的,throw是在方法体内部抛出异常, 
并且要与throws或者try。。。catch语句块结合使用。否则程序无法通过编译。 
而throws是在方法声明时抛出异常。使程序不会产生编译异常,但是要在调用方法时 
使用try。。。catch捕获异常进行分析处理 
二.try、catch、finally是进行异常处理的关键字,在程序中的语句需要 
正常执行但有可能发生异常时,用 try{需要执行的语句} 
catch(Exception) 
{对异常进行处理的语句} 
finally{一定会被处理的语句} 
此方法可以友好的处理程序的异常,同时也方便了后期的排除和维护。
* @author Administrator
*/ 


public class Test8 {


public static void main(String[] args) {
System.out.println("over");
}


}

package com.itheima;


/**
9.求1000!的结果中包含多少个0.1000! = 1×2×3×4×5×...×999×1000.
思路:一个数 n 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数
的因子中 2 和 5 的个数, 而 2 的个数是远远多余 5 的个数的, 因
此求出 5 的个数即可。
1000/5=200
1000/25=40
1000/125=8
1000/625=1
200+40+8+1=249 
* @author Administrator
*/
class Test9{
public static void main(String[] args) {
int max = 1000, temp = 1, sum = 0;
for(int i = 1, j = 1; i <= max; i++, j = i % 10) {
if(j == 0 || j == 2 || j == 5) {
temp *= i;
while(temp % 10 == 0) {
sum++;
temp /= 10;
}
temp %= max;
}
}
System.out.println(sum);
}
}

package com.itheima;


/**
* 10. 一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带
一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃
鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。编程解决
猫狗鱼过河问题。
思路:先带猫过去,然后回来把狗带过,把猫带回来 。
再把鱼带过去,再回来带猫 
* @author Administrator
*/


import java.util.ArrayList; 
import java.util.List; 
public class Test10 { 
ArrayList<String> here = new ArrayList<String>(); 
ArrayList<String> there = new ArrayList<String>(); 

public boolean isSafty(List list) { 
if (list.contains("dog") && list.contains("cat") 
|| list.contains("cat") && list.contains("fish")) { 
return false; 

return true; 


public Test10() { 
here.add("Dog"); 
here.add("cat"); 
here.add("fish"); 


public void toTake() { 
String str = here.get(0); 
here.remove(str); 
if (isSafty(here)) { 
System.out.println("1农夫带着" + str + "去往对岸,这边还剩下" + here + ",对岸有" + there); 
toThere(str); 

}

 else { 

here.add(str); 
toTake(); 



public void toThere(String s) { 

if (isSafty(there)) { 
there.add(s); 
if(here.isEmpty()){ 
System.out.println("农夫,"+there+"都被你带过来了"); 
return; 

if(isSafty(there)){ 
System.out.println("2农夫回到原点,对岸有" + there); 
toTake(); 

}

else{ 

String temp=there.get(0); 
there.remove(temp); 
System.out.println("3农夫带着"+temp+"回到原点,这边有" + here + ",对岸有" + there); 
here.add(temp); 
toTake(); 

else { 

there.remove(s); 
toTake(); 



public static void main(String[] args) { 
new Test10().toTake(); 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值