這是我第一次嘗試使用 Java,我盡了最大努力,但我需要縮短它,所以它不會那么長。
重要的是它保留了歐元的所有硬幣。這是一個德國代碼,所以 geld 意味著錢。
這部分代碼System.out.println(rgeldt " mal 2 Euro");僅表示 2 歐元必須被彈出的頻率。
public static void Rueckgaberechner(Double geld) {
System.out.println("Rueckgeld: ");
int rgeldt = 0;
while (geld >= 200) {
geld = geld - 200;
rgeldt = rgeldt 1;
}
if (rgeldt >= 1) {
System.out.println(rgeldt " mal 2 Euro");
}
int rgeldO = 0;
while (geld >= 100) {
geld = geld - 100;
rgeldO = rgeldO 1;
}
if (rgeldO >= 1) {
System.out.println(rgeldO " mal 1 Euro");
}
int rgeldf = 0;
while (geld >= 50) {
geld = geld - 50;
rgeldf = rgeldf 1;
}
if (rgeldf >= 1) {
System.out.println(rgeldf " mal 50 Cent");
}
int rgeldtw = 0;
while (geld >= 20) {
geld = geld - 20;
rgeldtw = rgeldtw 1;
}
if (rgeldtw >= 1) {
System.out.println(rgeldtw " mal 20 Cent");
}
int rgeldten = 0;
while (geld >= 10) {
geld = geld - 10;
rgeldten = rgeldten 1;
}
if (rgeldten >= 1) {
System.out.println(rgeldten " mal 10 Cent");
}
int rgeldfive = 0;
while (geld >= 5) {
geld = geld - 5;
rgeldfive = rgeldfive 1;
}
if (rgeldfive >= 1) {
System.out.println(rgeldfive " mal 5 Cent");
}
int rgeldtwo = 0;
while (geld >= 2) {
geld = geld - 2;
rgeldtwo = rgeldtwo 1;
}
if (rgeldtwo >= 1) {
System.out.println(rgeldtwo " mal 2 Cent");
}
int rgeldone = 0;
while (geld >= 1) {
geld = geld - 1;
rgeldone = rgeldone 1;
}
if (rgeldone >= 1) {
System.out.println(rgeldone " mal 1 Cent");
}
}
uj5u.com熱心網友回復:
您可以將其放入自己的函式中:
public static void Rueckgaberechner(Double geld) {
System.out.println("Rueckgeld: ");
geld = anzahlRueckgeld(200,geld, " mal 2 Euro");
geld = anzahlRueckgeld(100,geld, " mal 1 Euro");
geld = anzahlRueckgeld(50,geld, " mal 50 cent");
geld = anzahlRueckgeld(20,geld, " mal 20 cent");
geld = anzahlRueckgeld(10,geld, " mal 10 cent");
geld = anzahlRueckgeld(5,geld, " mal 5 cent");
geld = anzahlRueckgeld(2,geld, " mal 2 cent");
anzahlRueckgeld(1,geld, " mal 1 cent");
}
public static double anzahlRueckgeld(int amount, double geld ,String text) {
int rgeldt =0;
while (geld >= amount) {
geld = geld - amount;
rgeldt = rgeldt 1;
}
if (rgeldt >=1) {
System.out.println(rgeldt text);
}
return geld;
}
順便說一句:按照約定,java中的方法名應該以小寫字符開頭
uj5u.com熱心網友回復:
您的函式的輸入應該以美分為單位。這應該是自然數 ( int) 與實數 ( float)。
import java.util.*;
public class CoinDisplay {
private static List<CoinDenomination> euroCoinDenominations = List.of(
new CoinDenomination(200, "2 Euro"),
new CoinDenomination(100, "1 Euro"),
new CoinDenomination(50, "50 Cent"),
new CoinDenomination(20, "20 Cent"),
new CoinDenomination(10, "10 Cent"),
new CoinDenomination(5, "5 Cent"),
new CoinDenomination(2, "2 Cent"),
new CoinDenomination(1, "1 Cent")
);
public static void main(String args[]) {
System.out.println(formatCents(497, euroCoinDenominations));
}
public static String formatCents(int cents, List<CoinDenomination> coinDenoms) {
List<String> result = new ArrayList<>();
for (CoinDenomination denom : coinDenoms) {
int count = (int) Math.floor(cents / denom.getAmount());
if (count > 0) {
result.add(String.format("%d × %s", count, denom.getDisplay()));
}
cents %= denom.getAmount();
}
return String.join(System.lineSeparator(), result);
}
private static class CoinDenomination {
private int amount;
private String display;
public CoinDenomination(int amount, String display) {
this.amount = amount;
this.display = display;
}
public int getAmount() { return this.amount; }
public String getDisplay() { return this.display; }
}
}
輸出
2 × 2 Euro
1 × 50 Cent
2 × 20 Cent
1 × 5 Cent
1 × 2 Cent
這是一個作為片段作業的 JavaScript 版本:
顯示代碼片段
const euroCoinDenominations = [
{ amount: 200 , display: '2 Euro' },
{ amount: 100 , display: '1 Euro' },
{ amount: 50 , display: '50 Cent' },
{ amount: 20 , display: '20 Cent' },
{ amount: 10 , display: '10 Cent' },
{ amount: 5 , display: '5 Cent' },
{ amount: 2 , display: '2 Cent' },
{ amount: 1 , display: '1 Cent' }
];
const formatCents = (cents, coinDenominations) => {
const result = [];
for (let { amount, display } of coinDenominations) {
const count = Math.floor(cents / amount);
if (count > 0) {
result.push(`${count} × ${display}`);
}
cents %= amount;
}
return result.join('\n');
};
console.log(formatCents(497, euroCoinDenominations));
.as-console-wrapper { top: 0; max-height: 100% !important; }
uj5u.com熱心網友回復:
你不需要回圈。
static int Rueckgaberechner(int geld, int unit, String message) {
int regld = geld / unit;
if (regld > 0)
System.out.println(regld " mal " message);
return geld % unit;
}
public static void Rueckgaberechner(int geld) {
System.out.println("Rueckgeld: ");
geld = Rueckgaberechner(geld, 200, "2 Euro");
geld = Rueckgaberechner(geld, 100, "1 Euro");
geld = Rueckgaberechner(geld, 50, "50 Cent");
geld = Rueckgaberechner(geld, 20, "20 Cent");
geld = Rueckgaberechner(geld, 10, "10 Cent");
geld = Rueckgaberechner(geld, 5, "5 Cent");
geld = Rueckgaberechner(geld, 2, "2 Cent");
geld = Rueckgaberechner(geld, 1, "1 Cent");
}
public static void main(String[] args) {
Rueckgaberechner(1498);
}
輸出:
Rueckgeld:
7 mal 2 Euro
1 mal 50 Cent
2 mal 20 Cent
1 mal 5 Cent
1 mal 2 Cent
1 mal 1 Cent
uj5u.com熱心網友回復:
假設我已經正確理解并且 geld 是美分的數量,并且目標是輸出盡可能少的硬幣的變化,您可以通過使用除法和模數來做到這一點而無需任何回圈,例如獲得 2 歐元硬幣的數量凝膠你可以做
int numberOf2Euros = (int) (geld / 200);
geld = geld % 200;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/527178.html
標籤:爪哇循环while循环
上一篇:如何使用資料框中的設定坐標為搜索查詢創建回圈,同時為每個搜索結果創建一個新的資料框?
下一篇:while回圈不會在c 中回圈
