How to switch variable into another variable once the condition is get in java if else if statement

huangapple go评论70阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月23日 11:58:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定