英文:
How to switch variable into another variable once the condition is get in java if else if statement
问题
I just want to know how to switch the variable into another variable.. For example, I have an individual eggs... its 12 will consider as dozen eggs, in program, if I type 12 in individual eggs, how to put a code to switch it in dozen egg..
like this:
if (individualEggs==12)
convert it to dozenEggs:
the output on the screen will be like this:
price of dozen egg = 1 USD
price of individual egg = 0.1 USD
dozenEggs = 1
individualEggs = 12
THE ANSWER MUST BE 2 USD
and not 2.2 USD... when it is computed.. please help me...
英文:
I just want to know how to switch the variable into another variable.. For example, I have a an individual eggs... its 12 will consider as dozen eggs, in program, if I type 12 in individual eggs, how to put a code to switch it in dozen egg..
like this:
if (individualEggs==12)
convert it to dozenEggs:
the output in screen will be like this:
price of dozen egg = 1 usd
price of individual egg = .1 usd
dozenEggs = 1
individualEggs = 12
THE ANSWER MUST BE 2 USD
and not 2.2 usd... when it is computed.. please help me..
答案1
得分: 1
除以 12
以获得可以从个别鸡蛋中获得的整打数。取余数以获得剩余的个别鸡蛋数量,不足一打。
int dozenEggs = 1;
int individualEggs = 15;
dozenEggs += individualEggs / 12;
individualEggs %= 12;
// 然后计算价格,例如 dozenEggs + individualEggs * .1
英文:
Divide by 12
to get the number of full dozens you can get from the individual eggs. Take the remainder to get how many individual eggs are left, not forming a dozen.
int dozenEggs = 1;
int individualEggs = 15;
dozenEggs += individualEggs / 12;
individualEggs %= 12;
// then calculate price, e.g. dozenEggs + individualEggs * .1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论