新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
错在:
创新互联公司专注于钢城企业网站建设,成都响应式网站建设公司,商城网站开发。钢城网站建设公司,为钢城等地区提供建站服务。全流程定制网站,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
int s;
s=in.readLine();
in.readLine()得到的是String类型,不能直接转化为int,因此要改写为
int s;
s = Integer.valueOf(in.readLine());
并在函数声明出加上throws语句如下:
public static void main(String[] args) throws NumberFormatException, IOException
修改后代码为.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class jiaojiao implements dog, cat, niu {
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("输入序号以让各个动物叫,1是狗,2是猫,3是牛");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int s;
s = Integer.valueOf(in.readLine());
switch (s) {
case 1:
System.out.print("动物名字" + dog.name + "动物叫声" + dog.jiao);
;
break;
case 2:
System.out.print("动物名字" + cat.name + "动物叫声" + cat.jiao);
;
break;
case 3:
System.out.print("动物名字" + niu.name + "动物叫声" + niu.jiao);
break;
}
}
}
interface animal {
String jiao = " ";
String name = " ";
}
// 狗接口继承动物的接口
interface dog extends animal {
String jiao = "wangwang";
String name = "dogs";
}
// 猫接口继承动物的接口
interface cat extends animal {
String jiao = "miaomiao";
String name = "cats";
}
// 牛接口继承动作的接口
interface niu extends animal {
String jiao = "niuniu";
String name = "nius";
}
经测无误
一写就写多了,不过包含的东西比较多,java的重要知识点里面也有
这个结构是简单的处理的,有兴趣的话你可以想象下写成一个小的文字游戏
public class D {
public static void main(String[] args){
Cat cat1 = new Cat("Tom" , 10);
Cat cat2 = new Cat("Sun" , 15 , "苹果树");
Dog dog1 = new Dog("lufy" , 5);
Dog dog2 = new Dog("nan" , 20 , "泳池");
cat1.start();
cat2.start();
dog1.start();
dog2.start();
}
}
abstract class Creature extends Thread
{
protected int life; //生物的寿命
protected String name = "";
protected String objname = "";
public void call(){};
public void eat(){};
public void active(int state){};
}
class Cat extends Creature
{
public Cat(String name , int life)
{
this.name = name;
this.life = life;
System.out.println("小猫" + name + "诞生了 , 寿命" + life);
}
public Cat(String name , int life ,String objname)
{
this.name = name;
this.life = life;
this.objname = objname;
System.out.println("小猫" + name + "诞生了 , 寿命" + life + "喜欢" + objname );
}
public void climb()
{
System.out.println("小猫" + name + "在爬树" );
}
public void climb(String objname)
{
System.out.println("小猫" + name + "在爬" + objname + "树");
}
@Override
public void call() {
System.out.println("小猫" + name + "停下来在喵喵的叫" );
}
@Override
public void eat() {
System.out.println("小猫" + name + "饿了,它在吃东西" );
}
@Override
public void active(int state) {
switch(state)
{
case 1:
if(objname.length() 0)
climb(objname);
else
climb();
break;
case 2:
call();
break;
case 3:
eat();
break;
}
}
@Override
public void run() {
//你可以尝试在这里做一个状态机,控制动物的行为
int i = 0 ;
int state = 0 ;
while(i life)
{
double rnd = Math.random();
state = (int)(rnd * 4);//随机出状态
active(state);
i++;
}
System.out.println("小猫" + name + "它的生命结束了,享年" + life);
}
}
class Dog extends Creature
{
public Dog(String name , int life)
{
this.name = name;
this.life = life;
System.out.println("小狗" + name + "诞生了 , 寿命" + life );
}
public Dog(String name , int life , String objname)
{
this.name = name;
this.life = life;
this.objname = objname;
System.out.println("小狗" + name + "诞生了 , 寿命" + life + "喜欢" + objname );
}
public void swim()
{
System.out.println("小狗" + name + "在游泳" );
}
public void swim(String objname)
{
System.out.println("小狗" + name + "在" + objname + "游泳");
}
@Override
public void call() {
System.out.println("小狗" + name + "停下来在喵喵的叫" );
}
@Override
public void eat() {
System.out.println("小狗" + name + "饿了,它在吃东西" );
}
@Override
public void active(int state) {
switch(state)
{
case 1:
if(objname.length() 0)
swim(objname);
else
swim();
break;
case 2:
call();
break;
case 3:
eat();
break;
}
}
@Override
public void run() {
//你可以尝试在这里做一个状态机,控制动物的行为
int i = 0 ;
int state = 0 ;
while(i life)
{
double rnd = Math.random();
state = (int)(rnd * 4);//随机出状态
active(state);
i++;
}
System.out.println("小狗" + name + "它的生命结束了,享年" + life);
}
}
interface animal {
void shout();
}
class Dog implements animal{
public void shout(){
System.out.println("汪汪叫");
}
}
class Cat implements animal{
public void shout(){
System.out.println("喵喵叫");
}
}
class Bird implements animal{
public void shout(){
System.out.println("唧唧叫");
}
}