1.使用方法的形式打印99乘法表
package a;
public class fuxi {
public static void main(String[] args) {
// TODO Auto-generated method stub
fuxi.dayin();
}
public static void dayin(){
for(int j=1;j<=9;j++) {
for(int i=1;i<=j;i++){
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println();
}}}
2.使用方法的形式实现两个变量的交换(扩展,不使用第三变量实现交换)
package a;
public class fuxi {
public static void main(String[] args) {
fuxi.dayin();}
public static void dayin(){
int a ,b;
a = 1;
b=2;
a=a+b;
b=a-b;
a=a-b;
System.out. println("a="+a);
System.out. println("b="+b);
}}
3.提示用户输入一个数字,判断是否是水仙花数
package a;
import java.util.Scanner;
public class fuxi {
public static void dayin() {
System.out.println("判断是否为水仙花数?请输入一个小于1000的三位数");
Scanner s = new Scanner(System.in);// 声明Scanner接收的类
int a = s.nextInt();// 接收用户输入整形的数据
double x;
double y;
double z;
x = (a / 100) * (a / 100) * (a / 100);
y = (a / 10 % 10) * (a / 10 % 10) * (a / 10 % 10);
z = (a % 10) * (a % 10) * (a % 10);
if (x + y + z == a) {
System.out.println("是水仙花数");
}
else {
System.out.println("不是水仙花数");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
fuxi.dayin();
}
}
4.使用方法的形式打印100-999之间的水仙花数
package a;
public class fuxi {
public static void dayin() {
System.out.println("100-999之间的水仙花数是\n");
int i, j, k, n;
n = 100;
while (n < 1000) {
i = n / 100;
j = (n - i * 100) / 10;
k = n % 10;
if (i * i * i + j * j * j + k * k * k == n) {
System.out.println(n);
n++;
}
}
}
public static void main(String[] args) {
fuxi.dayin();
}
}
5.使用方法制作简单计算器
package a;
import java.util.Scanner;
public class fuxi {
public static void dayin() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入第一个数:");
int a1 = sc.nextInt();
System.out.println("请输入运算符号:");
String a2 = sc.next();
System.out.println("请输入第二个数:");
int a3 = sc.nextInt();
if (a2.equals("+")) {
int s = a1 + a3;
System.out.println("结果=" + s);
} else if (a2.equals("-")) {
int s = a1 - a3;
System.out.println("结果=" + s);
} else if (a2.equals("*")) {
int s = a1 * a3;
System.out.println("结果=" + s);
} else {
System.out.println("输入符号有误,请重新输入。");
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
fuxi.dayin();
}
}
6.提示用户输入两个数字,计算其数字的和,将结果返回出来,使用有参有返的方式来做
package a;
import java.util.Scanner;
public class fuxi {
public static int dayin(int a1, int a3) {
int s = a1 + a3;
System.out.println("结果:" + a1 + "+" + a3 + "=" + s);
return s;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int a1 = sc.nextInt();
System.out.println("请输入第二个数:");
int a3 = sc.nextInt();
fuxi.dayin(a1, a3);
}
}
7.定义人物类,人物包含属性编号,姓名,年龄和性别定义人物介绍的方法,创建人物对象,张三和李四,让张三和李四进行介绍自己
package a;
public class person {// 总结的步骤:1.先定义创建一个类
public static void main(String[] args) {
person a = new person("张三", "男", 20, "520");
person b = new person("李四", "女", 13, "1314");
person c = new person("王五", "男", 14, "7758258");
a.say();
b.say();
c.say();
}
public int age;
public String bianhao;
public String name;
public String sex;
// 介绍自己的方法
public person(String name, String sex, int age, String bianhao) {
this.name = name;
this.sex = sex;
this.age = age;
this.bianhao = bianhao;
}
public void say() {
System.out.println("我的姓名为" + this.name + ",性别为" + this.sex + ",年龄为"
+ this.age + ",编号为" + bianhao);
}
}
8. 定义一个用户类,包含用户名和密码属性,设置其值用户名为111密码为222,并有一个登录的方法,直接登录
package a;
import java.util.Scanner;
public class fuxi {
public static void main(String[] args) {
System.out.println("欢迎登录,请输入你的用户名");
Scanner s = new Scanner(System.in);
String uname = s.next();// 接收用户名
System.out.println("请输入你的用户密码");
String pwd = s.next();
yonghu yh = new yonghu();
boolean b = yh.bijiaoyixia(uname, pwd);
if (b) {
System.out.println("可以登录");
}
else {
System.out.println("不可以登录");
}
}
}
class yonghu {
String pwd = "222";
String uname = "111";
public boolean bijiaoyixia(String uname, String pwd) {
if (this.uname.equals(uname) && this.pwd.equals(pwd)) {
return true;
} else {
return false;
}
}
}
9.注册登录
package a;
import java.util.Scanner;
public class fuxi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
yonghu yh = new yonghu();
while (true) {
System.out.println("欢迎来到易买网,请选择你要执行的功能:1.登录2.注册");
int num = s.nextInt();
if (num == 1) {
System.out.println("欢迎登录,请输入你的用户名");
String uname = s.next();// 接收用户名
System.out.println("请输入你的用户密码");
String pwd = s.next();
boolean b = yh.bijiaoyixia(uname, pwd);
if (b) {
System.out.println("可以登录");
}
else {
System.out.println("不可以登录");
}
}
else if (num == 2) {
System.out.println("执行注册用户的功能");
System.out.println("请输入你的用户名");
}
String uname = s.next();
System.out.println("请输入你的密码");
String pwd = s.next();
yh.uname = uname;
yh.pwd = pwd;
}
}
}
class yonghu {
String pwd = "222";
String uname = "111";
public boolean bijiaoyixia(String uname, String pwd) {
if (this.uname.equals(uname) && this.pwd.equals(pwd)) {
return true;
} else {
return false;
}
}
}
10.定义矩形类,矩形包含私有属性宽和高,定义set方法为其赋值,定义面积和周长的方法,实例化两个矩形并调用方法计算周长和面积
package a;
public class juxing {
public static void main(String[] args) {
juxing jx = new juxing();
jx.chang = 1;
jx.kuan = 3;
juxing jx2 = new juxing();
jx2.chang = 2;
jx2.kuan = 4;
// jx.setKuan(11);
//
// jx.setChang(2);
int zhouchang = jx.zhouchang();
int mianji = jx.mianji();
int zhouchang2 = jx2.zhouchang();
int mianji2 = jx2.mianji();
System.out.println("周长为" + zhouchang + "面积" + mianji);
System.out.println("周长为" + zhouchang2 + "面积" + mianji2);
}
private int chang;
private int kuan;
public int getChang() {
return chang;
}
public int getKuan() {
return kuan;
}
public int mianji() {
return (this.kuan * this.chang);
}
public void setChang(int chang) {
this.chang = chang;
}
public void setKuan(int kuan) {
this.kuan = kuan;
}
public int zhouchang() {
return ((this.kuan + this.chang) * 2);
}
}
11.定义员工类,员工包含员工姓名,性别,职位和收入,收入的属性为私有的,不允许别人直接查看,如果想查看收入就必须通过特有的方法来查看,使用员工类创建员工刘备、张飞和关羽,分别查看几位员工的收入
package a;
import java.util.Scanner;
public class juxing {
public static void main(String[] args) {
System.out.println("请输入用户名(刘备,张飞,关羽)");
Scanner s1 = new Scanner(System.in);
String a = s1.next();
System.out.println("请输入用户密码(654321,54321,4321)");
Scanner s2 = new Scanner(System.in);
String b = s2.next();
juxing jx = new juxing();
jx.Name = "刘备";
jx.Sex = "男";
jx.ZhiWei = "CEO";
jx.ShouRu = 666666;
juxing d = new juxing();
d.Name = "张飞";
d.Sex = "男";
d.ZhiWei = "CEO";
d.ShouRu = 666665;
juxing e = new juxing();
e.Name = "关羽";
e.Sex = "男";
e.ZhiWei = "CEO";
e.ShouRu = 666664;
if (a.equals("刘备"))
{
if (b.equals("654321")) {
jx.setShouRu(666666);
int ShouRu = jx.mianji();
System.out.println("收入为" + ShouRu);
}
}
if (a.equals("张飞"))
{
if (b.equals("54321")) {
d.setShouRu(666665);
int ShouRu = d.mianji();
System.out.println("收入为" + ShouRu);
}
}
if (a.equals("关羽"))
{
if (b.equals("4321")) {
e.setShouRu(666664);
int ShouRu = e.mianji();
System.out.println("收入为" + ShouRu);
}
}
}
public String Name;
public String Sex;
private double ShouRu;
public String ZhiWei;
public int mianji() {
return (int) this.ShouRu;
}
public void setShouRu(int ShouRu) {
this.ShouRu = ShouRu;
}
public double ShouRu() {
return this.ShouRu;
}
}