gpt4 book ai didi

java - 用于更改的 while 循环

转载 作者:行者123 更新时间:2023-12-01 12:11:47 25 4
gpt4 key购买 nike

我正在学习 Java,并正在编写将便士兑换成零钱的代码。然而它已经完成,我不确定要在 while 循环中输入什么。我本来会使用 while(!change.equals("END")) 但这无法完成,因为 Change 是一个整数,所以我该怎么办?

class Main {

public static void main(String args[]) {

System.out.print("#Please enter the amount of change : ");
int change = BIO.getInt();

if (change <= 500 && change >= 1) {
System.out.print("Amount" + "\t" + "Coins" + "\n");
}

while (change =) {
int twopounds, pounds, fifty, twenty, ten, five, two, one;

twopounds = change / 200;
int left = change % 200;

pounds = left / 100;
left = left % 100;

fifty = left / 50;
left = left % 50;

twenty = left / 20;
left = left % 20;

ten = left / 10;
left = left % 10;

five = left / 5;
left = left % 5;

two = left / 2;
left = left % 2;

one = left / 1;

int nbCoins = twopounds + pounds + fifty + twenty + ten + five + two + one;

if (change > 500 || change < 1) {
System.out.print("Invalid amount " + change + "p" + "\n");
}

if (change <= 500 && change >= 1) {

if (nbCoins == 1) {
System.out.print(change + "p " + "\t" + nbCoins + " coin ");
} else {
System.out.print(change + "p " + "\t" + nbCoins + " coins ");
}

if (twopounds > 0) {
System.out.print(twopounds > 1 ? twopounds + "*200p " : "200p ");
}

if (pounds > 0) {
System.out.print(pounds > 1 ? pounds + "*100p " : "100p ");
}

if (fifty > 0) {
System.out.print(fifty > 1 ? fifty + "*50p " : "50p ");
}

if (twenty > 0) {
System.out.print(twenty > 1 ? twenty + "*20p " : "20p ");
}

if (ten > 0) {
System.out.print(ten > 1 ? ten + "*10p " : "10p ");
}

if (five > 0) {
System.out.print(five > 1 ? five + "*5p " : "5p ");
}

if (two > 0) {
System.out.print(two > 1 ? two + "*2p " : "2p ");
}

if (one > 0) {
System.out.print(one > 1 ? one + "*1p " : "1p ");
}

System.out.print("\n");
}

System.out.print("#Please enter the amount of change : ");
change = BIO.getInt();
}
}
}

谢谢:)

最佳答案

这取决于 BIO 是什么。一般来说,如果是Enumerable的话,有.hasNextToken()之类的选项。但是,在不知道该变量声明的内容的情况下,我无法告诉您触发器是什么。

关于java - 用于更改的 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27230611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com