首页 > 分享 > Java练习8

Java练习8

1、

求1000以内的完数
完数:如果一个数等于其所有因子之和,我们就称这个数为"完数",
比如6的因子为1,2,3 6 = 1 + 2 + 3,那么6就是一个完数

package com.ithema_01;

import java.util.ArrayList;

public class StudentText {

public static void main(String[] args) {

ArrayList<Integer> array=new ArrayList<Integer>();

for (int i=2;i<1001;i++){

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

if (i%j==0){

array.add(j);

}

}

int sum=1;

for (Integer x : array) {

sum += x;

}

if (sum==i){

System.out.println("1000以内的完数为:"+i);

}

array.clear();

}

}

}

2、

遍历二维数组打油诗

package com.ithema_01;

public class StudentText {

public static void main(String[] args) {

String[][] a={{"白日依山尽,"}, {"黄河入海流。"}, {"欲穷千里目,"},{"更上一层楼。"}};

for (String[] strings : a) {

for (String string : strings) {

System.out.println(string);

}

}

}

}

3、

斐波那契问题                    
已知:斐波那契数列的前几个数分别为0,1,1,2,3,5…8 从第三项开始,每一项都等于前两项的和. 
请接收用户输入的整数n,求出此数列的前n项.

package com.ithema_01;

import java.util.Scanner;

public class StudentText {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

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

int n= sc.nextInt();

int f1=1;

int f2=1;

int f3=0;

if (n%2==0) {

for (int i = 2; i <= n; i++) {

if (i % 2 == 0) {

System.out.println(f1);

System.out.println(f2);

}

f3 = f1 + f2;

f1 = f2;

f2 = f3;

}

}else {

System.out.println(f1);

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

if (i % 2 == 0) {

System.out.println(f1);

System.out.println(f2);

}

f3 = f1 + f2;

f1 = f2;

f2 = f3;

}

}

}

}

4、不死神兔

古典问题:生兔兔问题
有一对兔子,从出生后第3个月起都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,
假如兔子都不死,问每个月兔子的对数为多少?
程序分析:前两个月兔子的对数为1
从第三个月开始,兔子的对数变成了 2 3 5 8 13 21 …

package com.ithema_01;

import java.util.Scanner;

public class StudentText {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("请输入第几个月:");

int month= sc.nextInt();

int[] arr=new int[month];

arr[0]=1;

arr[1]=1;

for (int x=2;x< arr.length;x++){

arr[x]=arr[x-2]+arr[x-1];

}

System.out.println("第"+month+"个月兔子对数为"+arr[month-1]);

}

}

5、

打印水仙花数
水仙花数:是指一个三位数,其各位数字立方和等于该数字本身
例如:153就是一个水仙花数,因为153 = 1³ + 5³ + 3³

package com.ithema_01;

public class StudentText {

public static void main(String[] args) {

for (int i=100;i<1000;i++){

int ge=i%10;

int shi=i/10%10;

int bai=i/10/10%10;

if (ge*ge*ge+shi*shi*shi+bai*bai*bai==i){

System.out.println(i);

}

}

}

}

6、

面向对象 打印图形
需求:设计一个可以随机打印形状的代码

package com.ithema_01;

import java.util.Random;

public class StudentText {

public static void main(String[] args) {

Random a=new Random();

int n=a.nextInt(4);

int[] arr=new int[4];

switch (n) {

case 0 -> {

Line l1 = new Line();

arr[0] = l1.printLine();

}

case 1 -> {

Square s1 = new Square();

arr[1] = s1.printSquare();

}

case 2 -> {

Rectangle r1 = new Rectangle();

arr[2] = r1.printRectangle();

}

case 3 -> {

Prismatic p1 = new Prismatic();

arr[3] = p1.printPrismatic();

}

}

}

}

package com.ithema_01;

public class Line extends StudentText{

public int printLine(){

String a="*";

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

System.out.print(a);

}

System.out.println(a);

return 0;

}

}

package com.ithema_01;

public class Prismatic extends StudentText{

public int printPrismatic(){

String a="*";

System.out.println(" "+a);

System.out.println(a+" "+a);

System.out.println(" "+a);

return 0;

}

}

package com.ithema_01;

public class Rectangle extends StudentText{

public int printRectangle(){

String a="*";

System.out.println(a+a+a);

System.out.println(a+" "+a);

System.out.println(a+" "+a);

System.out.println(a+a+a);

return 0;

}

}

package com.ithema_01;

public class Square extends StudentText{

public int printSquare(){

String a="*";

System.out.println(a+" "+a+" "+a+" "+a);

System.out.println(a+" "+" "+" "+" "+" "+a);

System.out.println(a+" "+" "+" "+" "+" "+a);

System.out.println(a+" "+a+" "+a+" "+a);

return 0;

}

}

7、

面向对象 设计士兵类
需求:设计士兵与武器AK47类,并完成前进、进攻、发射子弹、装载子弹的功能

package com.ithema_01;

public class StudentText {

public static void main(String[] args) {

Soldier s=new Soldier();

s.forward();

s.attack();

AK a=new AK();

a.fireBullet();

a.loadBullets();

}

}

package com.ithema_01;

public class Soldier {

public void forward(){

System.out.println("前进");

}

public void attack(){

System.out.println("进攻");

}

}

package com.ithema_01;

public class AK {

public void fireBullet(){

System.out.println("发射子弹");

}

public void loadBullets(){

System.out.println("装载子弹");

}

}

8、

设计宠物类,用户可以自由选择养猫还是养狗,可以给宠物起名字,
还可以实现喂食互动的功能,宠物需要有饱食度和快乐度

package com.itheima_02;

import java.util.Scanner;

public class StudentDemo {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("请选择你的宠物:");

System.out.println("1、猫");

System.out.println("2、狗");

int a= sc.nextInt();

if (a==1){

Pets p=new Pets();

p.giveName();

p.feedCat();

p.interactionPets();

}else {

Pets p=new Pets();

p.giveName();

p.feedDog();

p.interactionPets();

}

}

}

package com.itheima_02;

import java.util.Scanner;

public class Pets {

private String name="a";

public Pets() {

}

public Pets(String name) {

this.name = name;

}

public void giveName(){

Scanner sc=new Scanner(System.in);

System.out.println("请给它起一个你喜欢的名字:");

name=sc.nextLine();

}

public void feedCat(){

Scanner sc=new Scanner(System.in);

System.out.println("请选择你要喂食的东西");

System.out.println("1、猫粮");

System.out.println("2、小鱼干");

int a=sc.nextInt();

if (a==1){

Cat c=new Cat();

c.eatCatFood();

}else {

Cat c=new Cat();

c.driedFish();

}

}

public void feedDog(){

Scanner sc=new Scanner(System.in);

System.out.println("请选择你要喂食的东西");

System.out.println("1、狗粮");

System.out.println("2、骨头");

int a=sc.nextInt();

if (a==1){

Dog d=new Dog();

d.eatDogFood();

}else {

Dog d=new Dog();

d.eatBone();

}

}

public void interactionPets(){

Scanner sc=new Scanner(System.in);

System.out.println("请选择你的互动方式");

System.out.println("1、抚摸");

System.out.println("2、跑动");

int a=sc.nextInt();

if (a==1){

System.out.println("欢乐度+5");

}else {

System.out.println("欢乐度+3");

}

}

}

package com.itheima_02;

public class Dog extends Pets{

public void eatDogFood(){

System.out.println("饱食度+3");

}

public void eatBone(){

System.out.println("饱食度+2");

}

}

package com.itheima_02;

public class Cat extends Pets{

public void eatCatFood(){

System.out.println("饱食度+3");

}

public void driedFish(){

System.out.println("饱食度+2");

}

}

9、

嵌套分支的练习
需求:提示并接收用户输入的月份,判断并输出属于哪个季节
1-12月是合法数据 3~5春天 6~8夏天 9~11秋天 其他情况冬天

package com.itheima_02;

import java.util.Scanner;

public class StudentDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("请输入你的月份:");

int month = sc.nextInt();

if (month < 1 || month > 12) {

System.out.println("你输入的星期数有误");

} else if (month == 3||month==4||month==5) {

System.out.println("春天");

} else if (month == 6||month==7||month==8) {

System.out.println("夏天");

} else if (month == 9||month==10||month==11) {

System.out.println("秋天");

} else {

System.out.println("冬天");

}

}

}

10、

对K个不同字符的全排列组成的数组, 面试官从中随机拿走了一个, 剩下的数组作为输入,
    请帮忙找出这个被拿走的字符串? 比如[“ABC”, “ACB”, “BAC”, “CAB”, “CBA”] 返回 “BCA”

package com.itheima_02;

import java.util.Random;

import java.util.Scanner;

public class StudentDemo {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("请输入要输入的字符组个数:");

int a= sc.nextInt();

String[] arr=new String[a];

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

System.out.println("输入字符串:");

String b = sc.next();

arr[i] = b;

}

Random c=new Random();

int num= c.nextInt(a);

System.out.println(arr[num]);

}

}

11、

一个数组里有奇数有偶数(乱序),调整数组顺序使奇数位于偶数前面。

package com.itheima_02;

import java.util.Random;

import java.util.Scanner;

public class StudentDemo {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("请输入要输入的字符个数:");

int a= sc.nextInt();

int[] arr=new int[a];

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

System.out.println("输入字符:");

int b = sc.nextInt();

arr[i] = b;

}

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

int c=arr[i];

if ((c%2)!=0){

System.out.print(c);

}

}

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

int c=arr[i];

if ((c%2)==0){

System.out.print(c);

}

}

}

}

12、(待改进)

有一只小鱼,它上午游泳150公里,下午游泳100公里,晚上和周末都休息(实行双休日),
假设从周x(1<=x<=7)开始算起,请问这样过了n天以后,小鱼一共累计游泳了多少公里呢

输入两个整数x,n(表示从周x算起,经过n天,n在long int范围内)。

输出一个整数,表示小鱼累计游泳了多少公里。

package com.itheima_02;

import java.util.Scanner;

public class StudentDemo {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("请输入小鱼开始游泳是周几:");

int x= sc.nextInt();

if (x<1||x>7){

System.out.println("请输入正确数字");

return;

}

System.out.println("请输入小鱼游了几天:");

int n= sc.nextInt();

if (x<=5){

int a=6-x;

int b=n-(a+2);

if (b<=0){

System.out.println("小鱼游泳了"+a*250+"公里");

}else {

int b3=b%7;

int b2=b/7;

if (b3==0) {

System.out.println("小鱼游泳了" + (a * 250 + b2 * 5 * 250) + "公里");

}else {

if (b3<=5){

System.out.println("小鱼游泳了" + ((a+b3) * 250 + b2 * 5 * 250) + "公里");

}else {

System.out.println("小鱼游泳了" + ((a+5) * 250 + b2 * 5 * 250) + "公里");

}

}

}

}else {

int a=8-x;

int b=n-a;

if (b>=0){

System.out.println("小鱼游泳了"+b*250+"公里");

}else {

System.out.println("小鱼游泳了"+0+"公里");

}

}

}

}

相关知识

编写Java程序模拟主人养宠物 java写一个宠物系统
专业java:void($={0})java:void($={0})java:void(LOGO设计免费生成器
Java练习
java版本电子宠物游戏代码
Java笔记:多态polymorphic
Java毕设项目宠物咖啡馆平台系统(java+VUE+Mybatis+Maven+Mysql)
Java StringBuilder类常用方法介绍
JAVA计算机毕业设计宠物托管系统Mybatis+系统+数据库+调试部署
电子宠物游戏java
java计算机毕业设计宠物管理系统(开题+程序+论文)

网址: Java练习8 https://m.mcbbbk.com/newsview249670.html

所属分类:萌宠日常
上一篇: 怎样对哈士奇进行“前进”训练
下一篇: 封神太子2宠物训练点数第二天会不