1.编写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print
类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的
立方和等于这个三位数本身,如: 371 = 33 + 73+ 13。)在主类E的main方法中来
测试类Print。
package liu0917;
public class Print
{
void output()
{
for(int i =100;i<=999;i++)
{
if(Math.pow(i/100,3)+Math.pow(i%10,3)+Math.pow(i/10%10, 3)==i)
{
System.out.println(i);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
package liu0917;
public class E {
public static void main(String[] args)
{
Print pr=new Print();
pr.output();
}
}
2.编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int
x),如果x的值是1,在控制台打印出大写的英文字母表;如果x的值是2,在
控制台打印出小写的英文字母表。其次,再定义一个主类——TestClass,在主类
的main方法中创建Print类的对象,使用这个对象调用方法output ()来打印出大
小写英文字母表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package liu0917;
public class Print2
{
int x;
void output()
{
if(x==1)
{
for(int i =65;i<=90;i++)//大写字母在char类型中的位置
{
char a =(char) i;
System.out.print(a);
}
}
else if(x==2)
{
for(int i =97;i<=122;i++)
{
char a =(char) i;
System.out.print(a);
}
}
else
{
System.out.println("输入有误");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package liu0917;
import java.util.Scanner;
public class TestClass2 {
public static void main(String[] args)
{
Print2 pr = new Print2();
Scanner sc = new Scanner(System.in);
System.out.println("请输入x的值");
pr.x=sc.nextInt();
pr.output();
}
}
3、
.按要求编写Java应用程序。
(1)建立一个名叫Cat的类:
属性:姓名、毛色、年龄
行为:显示姓名、喊叫
(2)编写主类:
创建一个对象猫,姓名为“妮妮”,毛色为“灰色”,年龄为2岁,在屏幕上输
出该对象的毛色和年龄,让该对象调用显示姓名和喊叫两个方法。
package com.hanqi;
public class Mao {
String name,maose;
int age;
void xingwei()
{
System.out.println("猫的姓名:"+name);
System.out.println("猫的叫声:"+"喵喵");
}
}
package com.hanqi;
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Mao ma=new Mao();
ma.name="妮妮";
ma.maose="灰色";
ma.age=2;
Scanner sc=new Scanner(System.in);
System.out.println("输入猫的颜色:");
String str=sc.nextLine();
System.out.println("输入猫的年龄:");
int in=sc.nextInt();
if(ma.maose.equals(str))
{
ma.xingwei();
}
else
{
System.out.println("查询不到猫信息");
}
}
}