import java.awt.*;
創(chuàng)新互聯(lián)公司成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元路北做網(wǎng)站,已為上家服務(wù),為路北各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話: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("當(dāng)前時(shí)間:"));
add(timetxt=new JTextField(25));
add(new JLabel("當(dāng)前數(shù)字:"));
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;//賬號(hào);
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;
}
}
測(cè)試類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("雇員編號(hào):"+pe.getEmpId());
System.out.println("雇員姓名:"+pe.getEmpName());
System.out.println("雇員性別:"+pe.getEmpsex());
System.out.println("每小時(shí)工資:"+pe.getHourlyPay());
}
}
/**
*?學(xué)生類
*?@author?LuHuan
*
*/
public?class?Student?{
private?String?id;?//學(xué)號(hào)
private?String?name;?//姓名
private?int?age;?//年齡
private?int?score;?//成績(jī)
//聲明一個(gè)空的構(gòu)造方法
public?Student(){}
//聲明一個(gè)全部屬性的構(gòu)造方法
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?"學(xué)號(hào)="?+?id?+?",?姓名="?+?name?+?",?年齡="?+?age?+?",?成績(jī)="?+?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;
}
//在這里設(shè)置年齡的范圍
public?void?setAge(int?age)?{
if(age?=?0??age?=?100)
this.age?=?age;
else?
System.out.println("年齡(取值介于0-100之間)");
}
public?int?getScore()?{
return?score;
}
//在這里設(shè)置成績(jī)的范圍
public?void?setScore(int?score)?{
if(score?=?0??score?=?100)
this.score?=?score;
else?
System.out.println("成績(jī)(取值介于0-100之間)");
}
測(cè)試類:
/**
*?測(cè)試類
*?@author?LuHuan
*
*/
public?class?Test?{
public?static?void?main(String[]?args)?{
Student?s?=?new?Student("10001",?"黃達(dá)",?20,?99);
s.show();?//輸出:學(xué)號(hào)=10001,?姓名=黃達(dá),?年齡=20,?成績(jī)=99
}
}
第1個(gè)文件: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個(gè)文件:Student.java
public class Student extends Person{
private String school;
private String number;
public void work(){
System.out.println("學(xué)生在認(rèn)真學(xué)習(xí)");
}
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個(gè)文件: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個(gè)文件:Cadre.java
public class Cadre extends Student{
private String position;
public void meeting(){
System.out.println("學(xué)生干部在開會(huì)");
}
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的時(shí)候繼續(xù)執(zhí)行do里面的代碼,當(dāng)為false的時(shí)候結(jié)束執(zhí)行。下面是您的問題的一種解決辦法,忘采納。
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為負(fù)值的時(shí)候會(huì)出現(xiàn)異常,您可以寫一個(gè)異常拋出來解決問題。
網(wǎng)站名稱:大一java代碼閱讀題,大一java題庫及答案
本文來源:http://chinadenli.net/article32/dsisisc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、響應(yīng)式網(wǎng)站、品牌網(wǎng)站制作、網(wǎng)站收錄、品牌網(wǎng)站設(shè)計(jì)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)