扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
import java.awt.*;
创新互联公司成立于2013年,是专业互联网技术服务公司,拥有项目网站设计、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元路北做网站,已为上家服务,为路北各地企业和个人服务,联系电话:18980820575
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
public class Win extends JFrame implements Runnable
{
public static void main(String[] args)
{
new Win();
}
Win()
{
setLayout(new GridLayout(2,2));
add(new JLabel("当前时间:"));
add(timetxt=new JTextField(25));
add(new JLabel("当前数字:"));
add(numtxt=new JTextField(25));
// setSize(400,300);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setResizable(false);
setLocationRelativeTo(null);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exit=true;
dispose();
System.exit(0);
}
});
new Thread(this).start();
new Thread(new Runnable()
{
public void run()
{
while(!exit)
{
try
{
Thread.sleep(2000);
n++;
numtxt.setText(String.valueOf(n));
n%=10;
}
catch(Exception ex)
{
}
}
}
private int n=0;
}).start();
setVisible(true);
}
public void run()
{
while(!exit)
{
try
{
timetxt.setText(sdf.format(new Date()));
Thread.sleep(1000);
}
catch(Exception ex)
{
}
}
}
private boolean exit=false;
private JTextField timetxt,numtxt;
private final SimpleDateFormat sdf=new SimpleDateFormat("H:m:s");
}
so easy...
public class Account{
private String accNo;//账号;
private float leftAmount;//余额
private float rate;//年利率
public void setAccNo(String accNo){this.accNo = accNo;}
public String getAccNo(){return accNo;}
public void setLeftAmount(float leftAmount){this.leftAmount = leftAmount;}
public float getLeftAmount(){return leftAmount;}
public void setRate(float rate){this.rate = rate;}
public float getRate(){return rate;}
public void cunkuan(float amount){this.leftAmount += amount;}//存款
public void qukuan(float amount){this.leftAmount -= amount;}//取款
}
public class Jijie extends Account{
}
public class Xinyong extends Account{
private float limitAmount;//信用卡的透支额限制
}
Employee .java
public class Employee {
private int empId;
private String empName;
private String empsex;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpsex() {
return empsex;
}
public void setEmpsex(String empsex) {
this.empsex = empsex;
}
}
PartTimeEmployee.java
public class PartTimeEmployee extends Employee{
private double hourlyPay;
public double getHourlyPay() {
return hourlyPay;
}
public void setHourlyPay(double hourlyPay) {
this.hourlyPay = hourlyPay;
}
}
测试类Test.java
public class Test{
public static void main(String[] args) {
PartTimeEmployee pe=new PartTimeEmployee();
pe.setEmpId(1);
pe.setEmpName("莉莉");
pe.setEmpsex("女");
pe.setHourlyPay(50.0);
System.out.println("雇员编号:"+pe.getEmpId());
System.out.println("雇员姓名:"+pe.getEmpName());
System.out.println("雇员性别:"+pe.getEmpsex());
System.out.println("每小时工资:"+pe.getHourlyPay());
}
}
/**
* 学生类
* @author LuHuan
*
*/
public class Student {
private String id; //学号
private String name; //姓名
private int age; //年龄
private int score; //成绩
//声明一个空的构造方法
public Student(){}
//声明一个全部属性的构造方法
public Student(String id, String name, int age, int score) {
super();
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "学号=" + id + ", 姓名=" + name + ", 年龄=" + age + ", 成绩=" + score;
}
public void show() {
System.out.println(toString());
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//在这里设置年龄的范围
public void setAge(int age) {
if(age = 0 age = 100)
this.age = age;
else
System.out.println("年龄(取值介于0-100之间)");
}
public int getScore() {
return score;
}
//在这里设置成绩的范围
public void setScore(int score) {
if(score = 0 score = 100)
this.score = score;
else
System.out.println("成绩(取值介于0-100之间)");
}
测试类:
/**
* 测试类
* @author LuHuan
*
*/
public class Test {
public static void main(String[] args) {
Student s = new Student("10001", "黄达", 20, 99);
s.show(); //输出:学号=10001, 姓名=黄达, 年龄=20, 成绩=99
}
}
第1个文件:Person.java
public class Person{
private String name;
private String sex;
private String age;
private String country;
public void eat(){
System.out.println("Person吃饭");
}
public void sleep(){
System.out.println("Person睡觉");
}
public void work(){
System.out.println("Person工作");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
第2个文件:Student.java
public class Student extends Person{
private String school;
private String number;
public void work(){
System.out.println("学生在认真学习");
}
public static void main(String[] args){
Student s = new Student();
s.setName("张无忌 ");
s.setSex("男 ");
s.setAge("22 ");
s.setCountry("元朝 ");
s.school="明教 ";
s.number="1 ";
s.work();
s.sleep();
s.eat();
System.out.println(s.getName() + s.getSex() + s.getAge() + s.getCountry() + s.school + s.number);
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
第3个文件:Worker.java
public class Worker extends Person{
private String unit;
private String working_age;
public void work(){
System.out.println("工人努力工作赚钱");
}
public static void main(String[] args){
Worker w = new Worker();
w.setName("赵敏 ");
w.setSex("女 ");
w.setAge("20 ");
w.setCountry("元朝 ");
w.unit="绍敏郡主 ";
w.working_age="3 ";
w.work();
w.sleep();
w.eat();
System.out.println(w.getName() + w.getSex() + w.getAge() + w.getCountry() + w.unit + w.working_age);
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getWorking_age() {
return working_age;
}
public void setWorking_age(String working_age) {
this.working_age = working_age;
}
}
第4个文件:Cadre.java
public class Cadre extends Student{
private String position;
public void meeting(){
System.out.println("学生干部在开会");
}
public static void main(String[] args){
Cadre c = new Cadre();
c.setName("周芷若 ");
c.setSex("女 ");
c.setAge("23 ");
c.setCountry("元朝 ");
c.setSchool("峨眉 ");
c.setNumber("3 ");
c.position="峨眉派掌门 ";
c.meeting();
c.work();
c.sleep();
c.eat();
System.out.println(c.getName() + c.getSex() + c.getAge() + c.getCountry() + c.getSchool() + c.getNumber() + c.position);
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
首先回答您的do-while的问题,while中是条件为true的时候继续执行do里面的代码,当为false的时候结束执行。下面是您的问题的一种解决办法,忘采纳。
import java.util.Scanner;
public class JuiceVendingCashFlow {
public static int total = 0;
public static int customernum =0;
public static double income = 0;
public double income (double w,double t){
income += (w/100)*3.5+t*1.5;
total++;
customernum++;
return income;
}
public static void main(String[] args) throws Exception {
JuiceVendingCashFlow f= new JuiceVendingCashFlow();
double weight = 0;
System.out.println("Customer"+"#"+ ++customernum+" welcome!");
do {
System.out.print("how much juice are you after?");
try{
Scanner s = new Scanner(System.in);
weight = s.nextDouble();
if(weight0) {
throw new Exception();
}
} catch (Exception e) {
System.out.println("OVER");
}
if (weight=0) {
System.out.println("how many toppings would you like?");
Scanner b = new Scanner(System.in);
double topping = b.nextDouble();
f.income(weight, topping);
}
} while (weight=0);
System.out.println(income+" "+ --customernum);
}
}
此方法写的仓促其中如果topping为负值的时候会出现异常,您可以写一个异常抛出来解决问题。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流