扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
public static void main(String args[]) {
创新互联公司是一家专业从事网站建设、成都做网站的网络公司。作为专业网站建设公司,创新互联公司依托的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、网络营销推广及网站设计开发服务!
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个开始数字:");
int a = 0;
try {
a = scanner.nextInt();
} catch (Exception e) {
System.out.println("输数字不合法");
return;
}
System.out.println("请输入一个结束数字:");
int b = 0;
try {
b = scanner.nextInt();
} catch (Exception e) {
System.out.println("输数字不合法");
return;
}
int sum = 0;
if (a b) {
for (int i = b; i = a; i++) {
sum = sum + i;
}
} else {
for (int i = a; i = b; i++) {
sum = sum + i;
}
}
System.out.println(sum);
}
把 人 封装成一个类Person,继承这个类 变有了 人应该有的属性
把 家庭成员关系封装一个类Family,继承这个类有了家庭关系的属性
把工作封装成一个接口Jop,继承这个接口实现了工作的方法
由于Person 和 Family 属性都是固定的,即每个人的情况基本都一样所以封装成类
Jop 则是因为 每个人的工作不一样,所有做成接口,究竟是什么样的工作让这个人来实现
这里Person 和 Family需要的属性并不多,所以我统一将他们封装了一个类 Person 实现代码如下:
public class XiaoHong extends Person implements Jop {
public XiaoHong(String name, String sex, String Father, String Mother,
String Son) {
super(name, sex, Father, Mother, Son);
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
XiaoHong xiaohong = new XiaoHong("小红","女","李雷", "韩梅梅", "讨厌啦!人家还小!");
System.out.println(xiaohong);
xiaohong.work();
}
@Override
public void work() {
System.out.println("我是一个高中生");
}
}
/********************************************************************************/
public class Person {
private String name = "I Don't Know";
private String sex = "I Don't Know";
private String Father = "I Don't Know";
private String Mother = "I Don't Know";
private String Son = "I Don't Know";
public Person(String name, String sex, String myFather, String myMother,
String mySon) {
super();
this.name = name;
this.sex = sex;
this.Father = myFather;
this.Mother = myMother;
this.Son = mySon;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getMyFather() {
return Father;
}
public String getMyMother() {
return Mother;
}
public String getMySon() {
return Son;
}
@Override
public String toString() {
return "Person [name=" + name + ", sex=" + sex + ", Father="
+ Father + ", Mother=" + Mother + ", Son=" + Son
+ "]";
}
}
/******************************************************************************/
public interface Jop {
public void work();
}
代码如下:
abstract class Worker {
protected String name;
public Worker(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void introduce();
}
public class Admins implements Userinterface{
private String name;
private String password;
private static int userNum;
public Admins() {
userNum ++;
}
public Admins(String name, String password) {
this.name = name;
this.password = password;
userNum ++;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
public String toString() {
return "Total: " + Admins.userNum + "\nName: " + this.name + "\nPassword: " + this.password;
}
public static void main(String [] args) {
Userinterface [] users = new Userinterface[]{new Admins("jeryy", "11111"), new Admins("green", "123123")};
for(Userinterface u: users) {
System.out.println(u);
}
}
}
interface Userinterface{
public void setPassword(String password);
public String getPassword();
public String toString();
}
public class Bins{
public static void main(String [] args) {
int len = 10;
int [] dist = new int [len];
for(int i = 0; i dist.length; i ++) {
dist[i] = (int)(Math.random() * 100);
}
java.util.Arrays.sort(dist);
System.out.println("生成数组如下: ");
for(int i: dist) {
System.out.print(i + "\t");
}
java.util.Scanner san = new java.util.Scanner(System.in);
System.out.print("输入要查找的数: ");
int key = san.nextInt();
int res = Bins.binSearch(dist, key);
//System.out.println(res);
String info = (res =0 ) ? (key + ":" + res) : "查找失败";
System.out.println(info);
}
public static int binSearch(int [] dist, int key) {
java.util.Arrays.sort(dist);
return java.util.Arrays.binarySearch(dist, key);
}
}
7.
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ";quot;
html
head
title计算器/title
style type="text/css"
input{
height:1.5em;}
.style1{
font-size:18px}
.height{
height:1.2em;}
/style
script language="javascript"
function computer(op){
var num1,num2,result;
num1 = parseFloat(document.form1.textname1.value);
num2 = parseFloat(document.form1.textname2.value);
if(op=="+")
result = num1 + num2;
document.form1.textResult.value=result;
if(op=="-")
result = num1 - num2;
document.form1.textResult.value = result;
if(op=="*")
result = num1 * num2;
document.form1.textResult.value = result;
if(op=="/" num2!=0)
result = num1/num2;
document.form1.textResult.value = result;
}
/script
/head
body
form name="form1"
center
table width="500" height="300" border="0" background="images/water001.jpg"
tr
td width="183" height="61"img src="images/baidu_logo.gif" width="182" height="54"//td
td width="219"span class="style1"百度计算器/span/td
/tr
tr
td colspan="2"span class="style1"第一个数:input type="text" name="textname1"//span/td
/tr
tr
td colspan="2"span class="style1"第二个数:input type="text" name="textname2"//span/td
/tr
tr
td colspan="2"
input class="height" type="button" value=" + " onClick="computer('+')"/nbsp;nbsp;nbsp;
input class="height" type="button" value=" - " onClick="computer('-')"/nbsp;nbsp;nbsp;
input class="height" type="button" value=" * " onClick="computer('*')"/nbsp;nbsp;nbsp;
input class="height" type="button" value=" / " onClick="computer('/')"/
/td
/tr
tr
td colspan="2"span class="style1"计算结果:input type="text" name="textResult"//span/td
/tr
/table
/center
/form
/body
/html
比如你是李刚,增加一个喊的方法,main也写上.main方法里不能再有别的方法,所以这样写
Class Person{
int age //年龄
String name //姓名
String gender //性别
public person(){}
public person(String name1){
name=name1;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
//你说的喊方法
public say(){
System.out.println("hello");
}
}
public class YourSelf{
public static void main(String args[]){
Person you=new Person("李刚");
you.say();
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流