給你一個前幾天才幫人寫的
創(chuàng)新互聯(lián)建站,為您提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站營銷推廣、網(wǎng)站開發(fā)設(shè)計,對服務(wù)成都咖啡廳設(shè)計等多個行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗。創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報價服務(wù),我們深知市場的競爭激烈,認真對待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進步,是我們永遠的責任!
“計算整錢兌零”。程序要求用戶輸入一個雙精度數(shù)代表總元數(shù),就會列出總值與其等價的1元幣、二角五分幣、5分幣和1分幣的數(shù)目。程序報告的數(shù)目是1元幣的最大數(shù)、其次是二角五分幣的最大數(shù),等等,依此類推。只顯示非零的單位。對單個單位顯示單數(shù)單詞,對多于一個單位的顯示復(fù)數(shù)單詞
import java.util.Scanner;
public class MoneyCalculate {
public static void main(String[] args) {
int max100 = 0;
int max25 = 0;
int max5 = 0;
int max1 = 0;
double money = getMoneyFromInput();
String str = String.valueOf(money).trim();
String[] ary = str.split("\\.");
max100 = Integer.parseInt(ary[0]);
if(ary.length == 2){
int fen = Integer.parseInt(ary[1]);
if(ary[1].trim().length() == 1){
fen = Integer.parseInt(ary[1]) * 10;
}
max25 = fen / 25;
if(fen % 25 != 0){
fen = fen % 25;
}else{
fen = 0;
}
max5 = fen / 5;
max1 = fen % 5;
}
StringBuilder sb = new StringBuilder(money + " = ");
if(max100 != 0){
sb.append(max100);
sb.append("*1 ");
}
if(max25 != 0){
sb.append(max25);
sb.append("*0.25 ");
}
if(max5 != 0){
sb.append(max5);
sb.append("*0.05 ");
}
if(max1 != 0){
sb.append(max1);
sb.append("*0.01 ");
}
System.out.println(sb.toString());
}
private static double getMoneyFromInput() {
Scanner scanner = new Scanner(System.in);
return scanner.nextDouble();
}
}
-----------
2.49
2.49 = 2*1 1*0.25 4*0.05 4*0.01
-----------
2.5
2.5 = 2*1 2*0.25
-----------
37.23
37.23 = 37*1 4*0.05 3*0.01
-----------------
123.569
123.569 = 123*1 22*0.25 3*0.05 4*0.01
package?test;
import?java.util.*;
import?java.awt.*;
import?java.awt.event.*;
import?java.applet.*;
public?class?Test5?extends?Applet?{
private?final?Panel?pan?=?new?Panel();
private?final?Label?time?=?new?Label();
private?final?Button?btnGo?=?new?Button("開始");
private?final?Button?btnPouse?=?new?Button("暫停");
private?final?Button?btnReset?=?new?Button("復(fù)位");
private?final?StopwatchThread?swThread?=?new?StopwatchThread();
private?class?btnGoListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
???
swThread.go();
btnGo.setEnabled(false);
}
}
private?class?btnPouseListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
???if(btnGo.isEnabled()){
???return?;
???}
?if?(btnPouse.getLabel().equals("繼續(xù)"))?{
swThread.go();
btnPouse.setLabel("暫停");
}?else?if?(btnPouse.getLabel().equals("暫停"))?{
swThread.noGo();
btnPouse.setLabel("繼續(xù)");
}
}
}
private?class?btnResetListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("開始");
btnPouse.setLabel("暫停");
}
}
private?class?StopwatchThread?extends?Thread?{
private?boolean?going?=?false;
private?long?prevElapsed?=?0;
private?Date?startDate?=?new?Date();
private?long?elapsedTime()?{
return?prevElapsed?+
(going???new?Date().getTime()?-?startDate.getTime()?:?0);
}
private?String?msToString(long?time)?{
???System.out.println(time+"??"+((0*60+2)*1000+999));
if(((99*60+59)*1000+983)=time((99*60+59)*1000+999)=time){//((0*60+2)*1000+983)=time((0*60+2)*1000+999)=time
if?(time?%?1000??990)
time?+=?2;
swThread.noGo();
}
String?ms,?sec,?min;
if?(time?%?10?=?5)
time?+=?5;
ms?=?Long.toString(time?%?1000);
while?(ms.length()??3)
ms?=?"0"?+?ms;
ms?=?ms.substring(0,?ms.length()?-?1);
time?/=?1000;
sec?=?Long.toString(time?%?60);
if?(sec.length()?==?1)?sec?=?"0"?+?sec;
time?/=?60;
min?=?Long.toString(time);
return?min?+?":"?+?sec?+?"."?+?ms;
}
public?void?go()?{
startDate?=?new?Date();
going?=?true;
}
public?void?noGo()?{
prevElapsed?=?elapsedTime();
going?=?false;
}
public?void?reset()?{
going?=?false;
prevElapsed?=?0;
}
public?void?run()?{
while?(true)?{
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public?void?init()?{
setLayout(new?GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new?GridLayout(3,2));
pan.add(new?Label("計時:"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new?Label());
add(pan);
btnGo.addActionListener(new?btnGoListener());
btnReset.addActionListener(new?btnResetListener());
btnPouse.addActionListener(new?btnPouseListener());
swThread.setDaemon(true);
swThread.start();
}
public?static?void?main(String[]?args)?{
Test5?applet?=?new?Test5();
Frame?aFrame?=?new?Frame("計時器");
aFrame.addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);
}
});
aFrame.add(applet,?BorderLayout.CENTER);
aFrame.setSize(400,?200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
可以改變有注釋的那個if語句里面的值來判斷什么時候停止
Java代碼:
import java.io.File;
import java.io.IOException;
public class Test10 {
public static void main(String[] args) {
//創(chuàng)建“abc”文件夾
String pathName = "d:\\abc";
File path = new File(pathName);
path.mkdir();
//創(chuàng)建“abc”文件
String fileName = "d:\\abc\\abc";
File file = new File(fileName);
try {
file.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
下面是一個簡單的 Java 程序示例,用于實現(xiàn)山羊和狼的繼承關(guān)系,并在測試類中進行驗證:
Animal.java:
```java
public abstract class Animal {
public void walk() {
System.out.println("走路");
}
public abstract void eat();
}
```
Goat.java:
```java
public class Goat extends Animal {
@Override
public void eat() {
System.out.println("吃草");
}
}
```
Wolf.java:
```java
public class Wolf extends Animal {
@Override
public void eat() {
System.out.println("吃肉");
}
}
```
AnimalTest.java:
```java
public class AnimalTest {
public static void main(String[] args) {
Goat goat = new Goat();
wolf wolf = new wolf();
goat.eat(); // 輸出:吃草
wolf.eat(); // 輸出:吃肉
goat.walk(); // 輸出:走路
wolf.walk(); // 輸出:走路
}
}
```
在此程序中,我們定義了一個 `Animal` 類作為基類,包含了所有動物都會有的行為(例如走路),并使用抽象方法 `eat()` 表示不同動物的飲食習(xí)慣。然后通過繼承實現(xiàn) `Goat` 和 `Wolf` 兩個子類,并分別重寫 `eat()` 方法。
在測試類中,我們分別創(chuàng)建了一個 `Goat` 和一個 `Wolf` 對象,并分別調(diào)用其 `eat()` 和 `walk()` 方法進行驗證??梢钥吹剑煌膭游锞邆洳煌娘嬍沉?xí)慣,但它們的走路行為是一致的。