[英]Javascript calculator based on item price and total price
我需要制作某種計算器來交易游戲中的某些物品,說實話,javascript計算器對我來說是最好的,因為它可以隨時隨地進行計算。
但是我真的不知道從哪里開始。
我需要計算游戲中的物品,如果我得到它們的金牌價值多少。
例如,有人給我168金,我給他物品,還有頭盔,背心,靴子,手套等物品。
所以商品價格是這樣的
Vest is 60 gold
Helmet is 45 gold
作為零錢
Tokens 25 gold
charms 7 gold
因此,如果有人想給我168金幣,他可以選擇背心或頭盔以及令牌或護身符作為零錢
So for 168 gold
He gets 2 vest and 7 charms
And for example if he wants to exchange 325 gold in helmets he gets
7 helmets and 1 charm.
等等。
有誰知道這有多復雜以及從哪里開始。
想象一下,我是市場上的賣方,有人來告訴我他有100美元,為此他可以買多少巧克力。
首先學習什么是javascript ( 什么不是 )。 (通過這句話,我最初指的是該問題同時被標記為Java和JavaScript)
恐怕您期望有人可以將您可以復制和粘貼的整個代碼發布給您。 我會讓您失望的-這是一個知識網站,而不是編碼機器。
雖然我不明白您到底想做什么(我懷疑有人這樣做了),但我可以給您一些有關如何開始的提示。 事物的階段。
我假設您已閱讀此處的javascript:
要讓用戶輸入數字,請創建HTML輸入元素(在腳本外部):
<input type="text" id="UNIQUE_ID" value="" />
<button onclick="click()">Click when done!</button>
<script> ... put the code between script tags... </script>
我已經聲明,只要單擊<button>
( “ onclick” ),就會調用click()
函數。 讓我們創建click()
:
function click() {
//We can get an "object" that represents the input field
var field = document.getElementById("UNIQUE_ID");
//We can get the value of said field
var number = field.value;
//We can empty the field now:
field.value = "";
//The number is now string (=text) We need a number. Multiplication forces conversion to number
number = 1*number;
//Check if the number is really a number
if(isNaN(number))
throw new Error("Input is not a number!");
//Do some calculation - for example conversion to hex:
var hexagonal = "0x"+(number).toString(16).toUpperCase();
//Throw the number at the user - nasty but easy
alert(number);
}
根據您要計算什么 ,你可以修改我的功能。 您還可以具有更多的字段和更多的數字。
我注意到,無論花多少錢,您都可以買到東西和飾品 。 不幸的是夏天很熱,我的水晶球過熱了-所以我不能讓它告訴我什么是魅力 。
但要找出可以購買的數量:
var gold = 666;
//D'oh an object!
var prices = {
helmet: 150,
armor: 430,
ice_cream: 10,
charm: 9
}
//Define what we want to buy
var wanted = "ice_cream";
//Prevent accessing undefined price
if(prices[wanted]==null)
throw new Error("We don't have this thing in stock yet!");
//Divide the cash by the price to know the amount
var amount = Math.floor(gold/prices[wanted]);
//And calculate spare cash - modulo calculates rest after division
var spare = gold%prices[wanted]
//Finally find out how many charms can be bought with the rest gold
var charms = Math.floor(spare/prices.charm);
//and let the user know
alert("With "+gold+" gold, you can buy "+amount+" "+wanted+"(s) and "+charms+" charms.");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.