
正文
java猪形状源代码 java画一只猪
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
一道Java初学者练习题,拜托了各位编程大神!
你好,提问者:
如果解决了你的问题,请采纳,若有疑问请追问,谢谢!
package com.zyx.cn.baidu_test;
/**
* 动物类
* @author yltd
*/
public interface Animal {
//voice方法
public void voice();
}
package com.zyx.cn.baidu_test;
/**
* 猫类
* @author yltd
*
*/
public class Cat implements Animal {
@Override
public void voice() {
System.out.println("Cat");
}
}
package com.zyx.cn.baidu_test;
//猪类
public class Pig implements Animal {
@Override
public void voice() {
System.out.println("Pig");
}
}
package com.zyx.cn.baidu_test;
//狗类
public class Dog implements Animal {
@Override
public void voice() {
System.out.println("Dog");
}
}
package com.zyx.cn.baidu_test;
/**
* 宠物店
* @author yltd
*/
public class Store {
public Animal getInstance(String srt){
Animal animal =null;
if("dog".equals(srt)){
animal = new Dog();
}else if("pig".equals(srt)){
animal = new Pig();
}else{
animal = new Cat();
}
return animal;
}
}
package com.zyx.cn.baidu_test;
/**
* 测试类
* @author yltd
*/
public class AnimalTest {
public static void main(String[] args) {
Store store =new Store();
Animal animal_1 = store.getInstance("cat");
animal_1.voice();
System.out.println("=============");
Animal animal_2 = store.getInstance("dog");
animal_2.voice();
System.out.println("=============");
Animal animal_3 = store.getInstance("pig");
animal_3.voice();
}
}
结果:
Cat
=============
Dog
=============
Pig
相关问答
Q1: 用java编写一个猪的程序,有一个母猪,从第二年开始生4头猪,第N年有多少头猪
楼主是不是说第2年开始每年都生4头母猪,并且新生的猪也从出生后第二年开始每年生4头呢?
是这样的话就是和递增牛群问题一样了。
第n年的总数=第n-1年的总数+第n年的新生数
=第n-1年的总数+4*第n-1年的总数//第n-1年之前出生的猪都于第n年生了4头
=5*第n-1年的总数
于是可以写出如下递归算法
public static int all(int n)
{
if(n==1)
return 1;
else if(n1)
return 5*all(n-1);
else
return 0;
}
Q2: 使用java编程:当A=0时,输出“白猪”;当A=1时,输出“红猪”;当A=其他数时,输出“变形猪
int A ;
if(A == 0){
System.out.println("白猪");
}else {
if(A == 1){
System.out.println("红猪");
}else{
System.out.println("变形猪");
}
}
Q3: 用JAVA给动物编码,要求五个动物,分别是:狗、猪、兔、鸡、羊为A数组,1到20为B数组,要求编写出C数组
import java.util.ArrayList;
import java.util.*;
public class BianHao {
/**
*
*/
public static void main(String[] args) {
//a字符串数组 length=5
String[] a = { "狗", "猪", "兔", "鸡", "羊" };
//b数组 length=20
int[] b = new int[20];
//设置标志位
boolean flag=false;
//产生随机数 并判断是否有重复的数值,如果flag=true说明产生的随机数没有重复的 退出wile循环
while(flag==false){
for (int i = 0; i b.length; i++) {
b[i]=(int)(Math.random()*62+1);
}
//判断每个元素的值是否相等 如果有就为false 从新产生随机数
flag=pd(b);
}
//对数组b进行排序,按升序
Arrays.sort(b);
//创建c数组
ListString c=new ArrayListString();
for (int j = 0; j a.length; j++) {
for (int h = 0; h b.length; h++) {
//对c数组进行填充值
c.add(a[j] + b[h] + "");
}
}
//对c数组进行遍历
for (String str : c) {
System.out.print(str+",");
}
}
public static boolean pd(int[] arr) {
//按位比较
for (int k = 0; k arr.length - 1; k++) {
for (int j = k + 1; j arr.length; j++) {
if (arr[k] == arr[j]) {
return false;
}
}
}
return true;
}
}
小伙儿 看行不 行的话 最佳啊!!分!分!
Q4: import java.util.*; public class test4{ public static void main(String[] args);
import java.util.*;
public class test4 {
public static void main(String[] args) {
System.out.println("输入一个整数: ");
Scanner in = new Scanner(System.in);
int score = in.nextInt();
if (score == 0) {
System.out.println("白猪");
}
if (score == 1) {
System.out.println("黑猪");
}
if (score == 2) {
System.out.println("红猪");
}
if (score 2) {
System.out.println("变形猪");
}
}
}
java猪形状源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java画一只猪、java猪形状源代码的信息别忘了在本站进行查找喔。







