In a recent game I'm developing, I've made shops.
When you buy an item and try to sell it, the selling price drops to 75% of what the buying price is.
After buying an item for 154 gold pieces, It says the shop will buy for 115.5 gold pieces, but you get 115 gold pieces from selling.
I wish to remove the ".5" from "115.5"
Any help is appreciated.
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000 && ShopValue < 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
I would just use Math.floor.
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000 && ShopValue < 1000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1) + " coins)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000000000) + " billion)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
If you wanted to be a little more generous to your players you might use Math.ceil instead ;-)
You can get what you want by either:
Decalring ShopAdd as an integer. This might cause other problems if you're (going to be) using ShopAdd somewhere else that needs it to be a floating point data type though.
Casting ShopAdd into an int right before it's printed. This is a quick fix, and isn't that great if you plan to print ShopAdd in many places, because they'll all have to be casted.
Some example java code:
public class PrintingNumbers {
public static void main(String[] args) {
int i = 1;
double d = 1;
System.out.println("printing integer: " + i);
System.out.println("printing double: " + d + " (not what you want)");
System.out.println("printing casted double: " + (int) d);
}
}
Output of above java code:
printing integer: 1
printing double: 1.0 (not what you want)
printing casted double: 1
In your case, option 1 would look something like this:
int ShopAdd // declare ShopAdd as an integer
...
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
And, option 2 would look like this:
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
// casting ShopAdd into an int when printing
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+(int)ShopAdd+"</col> coins");
}
}
I hope this helps! :)
As Kayaman already wrote in his comment, I would just cast it to an int or to an long - This will cut of the decimals without rounding.
If you want an integer as a result, the simple solution is to use integer maths.
long ShopValue = (long) (getItemShopValue(removeId, 1, removeSlot))*3/4;
String ShopAdd = " (";
if (ShopValue >= 750000000) {
ShopAdd = ShopValue / 750000000 + " billion";
} else if (ShopValue >= 1000000) {
ShopAdd = ShopValue / 750000 + " million";
} else if (ShopValue >= 1000) {
ShopAdd = ShopValue / 750 + "k";
} else {
ShopAdd = ShopValue + " coins";
}
ShopAdd += ")";
Note: your current implementation will print 800 as 0k
You can use Math.floor() as others said.
Apart from that: you may have a bug in your code logic. The 3rd and the 4th branches of the if statement are unreachable as the 2nd if clause covers them
if (ShopValue >= 100)
The correct way is to arrange them such that comparisons with greater numbers comes before comparisons with smaller numbers:
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
Related
I want to split any number to any identical pieces and the last remaining but not dividable piece will be the last piece. I wrote this code but I know that it should be more simple way to do this :) For example; 7500 divided by 2000 and the last modulus part will be the last part. Any suggestions?
public class MyClass {
public static void main(String args[]) {
int x =7500;
int y = 2000;
int lastPartCount = 0;
String result = new String();
if(x%y != 0){
lastPartCount = x%y;
}
int newx = x-lastPartCount;
for(int i=1; i<=(newx/y); i++){
if(i == 1){//first member
result = "part " + i + ": 0-" + y*i;
}else
{
result = "part " + i + ": " + (y*(i-1)) + "-" + y*i;
}
System.out.println(result);
if(i == (newx/y)){//last member
result = "part " + (i+1) + ": " + (y*(i)) + "-" + x;
System.out.println(result);
}
}
}
}
the result is like this:
part 1: 0-2000
part 2: 2000-4000
part 3: 4000-6000
part 4: 6000-7500
You can simplify your code like the following:
public static void main(String args[]) {
int x = 7500;
int y = 2000;
for (int i = 0; i < x/y; i++) {
System.out.println("Part " + (i+1) + ": " + y*i + " - " + y*(i+1));
}
if (x%y != 0) {
System.out.println("Part " + ((x/y)+1) + ": " + (x/y)*y + " - " + x);
}
}
(x/y)*y) is not equal to x since you divide integers, so (x/y)*y is actually the same as the "next" i of the for-loop.
You can also try the below code:
private void test() {
int x = 7500;
int y = 2000;
int j = 0;
int newX = x;
while (newX > y) {
System.out.println("Part " + (j + 1) + " = " + y * j++ + " - " + y * j);
newX -= y;
}
System.out.println("Part " + (j + 1) + " = " + j * y + " - " + x);
}
Alternative approach using two variables in your for loop and Math.min():
int x = 7500;
int y = 2000;
for (int i = 0, p = 1; i < x; i += y, p++) {
System.out.printf("Part %d: %d - %d%n", p, i, Math.min(i+y,x));
}
import java.util.Scanner;
public class MoneyChange {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Input an amount of money: ");
double value = reader.nextDouble();
int money = (int) value;
int thousand = money / 1000;
int fivehundred = money / 500;
int hundred = money / 100;
int fifty = (money % 100) / 50;
int twenty = ((money % 100) % 50) / 20;
int ten = (((money % 100) % 50) % 20) / 10;
int five = ((((money % 100) % 50) % 20) % 10) / 5;
int one = (((((money % 100) % 50) % 20) % 10) % 5) / 1;
System.out.println("Number of 1000-Bhat Banknote(s) is " + thousand);
System.out.println("Number of 500-Bhat Banknote(s) is " + fivehundred);
System.out.println("Number of 100-Bhat Banknote(s) is " + hundred);
System.out.println("Number of 50-Bhat Banknote(s) is " + fifty);
System.out.println("Number of 20-Bhat Banknote(s) is " + twenty);
System.out.println("Number of 10-Bhat Coin(s) is " + ten);
System.out.println("Number of 5-Bhat Coin(s) is " + five);
System.out.println("Number of 1-Bhat Coin(s) is " + one);
reader.close();
}
}
I would like to know how to continue the values from the first variable onto the other and not start again. Help me out!
In this code
int fifty = (money % 100) / 50;
you can calculate money % 100 and store it in a variable you can re-use it
int hun = money % 100;
int fifty = (hun) / 50;
Also be aware of Integer division
please do small modifications in your program.please take the look below code once.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Input an amount of money: ");
double value = reader.nextDouble();
int money = (int) value;
int thousand = money / 1000;
int remain = money % 1000 ;
int fivehundred = remain / 500;
remain = remain % 500;
int hundred = remain / 100;
remain = remain % 100;
int fifty = remain / 50;
remain = remain % 50;
int twenty = remain / 20;
remain = remain % 20;
int ten = remain / 10;
remain = remain % 10;
int five = remain / 5;
remain = remain % 5;
int one = remain / 1;
System.out.println("Number of 1000-Bhat Banknote(s) is " + thousand);
System.out.println("Number of 500-Bhat Banknote(s) is " + fivehundred);
System.out.println("Number of 100-Bhat Banknote(s) is " + hundred);
System.out.println("Number of 50-Bhat Banknote(s) is " + fifty);
System.out.println("Number of 20-Bhat Banknote(s) is " + twenty);
System.out.println("Number of 10-Bhat Coin(s) is " + ten);
System.out.println("Number of 5-Bhat Coin(s) is " + five);
System.out.println("Number of 1-Bhat Coin(s) is " + one);
reader.close();
}
May I know what is the argument need to sset in ()? 0 or 1? My datasets have three classes.
System.out.println("False Negative % = " + eval.falseNegativeRate(1));
System.out.println("True Negative % = " + eval.trueNegativeRate(1));
System.out.println("True Positive % = " + eval.truePositiveRate(1));
System.out.println("Accuracy % = " + accuracy);
System.out.println("Precision % = " + eval.precision(1));
System.out.println("Recall % = " + eval.recall(1));
System.out.println("F-Measure % = " + eval.fMeasure(1));
System.out.println("Error rate % = " + eval.errorRate());
Doing code for my java class, program runs as expected the only issue I'm having is if the interstate is, for example, 405 when it prints it prints 05 marking the question wrong because the question is just looking for 5 . Any help?
import java.util.Scanner;
highwayNumber = scnr.nextInt();
if ((highwayNumber > 999) || (highwayNumber < 1)) {
System.out.println(highwayNumber + " is not a valid interstate highway number.");
}
else if ((highwayNumber < 100) && (highwayNumber > 0) && (highwayNumber % 2 == 0)) {
System.out.println("The " + highwayNumber + " is primary, going east/west.");
}
else if ((highwayNumber < 100) && (highwayNumber > 0) && (highwayNumber % 2 != 0)) {
System.out.println("The " + highwayNumber + " is primary, going north/south.");
}
else if ((highwayNumber > 99) && (highwayNumber < 1000) && (highwayNumber % 2 == 0)) {
System.out.println("The " + highwayNumber + " is auxiliary, serving the " + String.valueOf(highwayNumber).substring(1) + ", going east/west.");
}
else if ((highwayNumber > 99) && (highwayNumber < 1000) && (highwayNumber % 2 != 0)) {
System.out.println("The " + highwayNumber + " is auxiliary, serving the " + String.valueOf(highwayNumber).substring(1) + ", going north/south.");
}
}
}
Just replace substring with % modulus operator:
System.out.println("The " + highwayNumber + " is auxiliary, serving the " + String.valueOf(highwayNumber % 100) + ", going east/west.");
System.out.println("The " + highwayNumber + " is auxiliary, serving the " + String.valueOf(highwayNumber % 100) + ", going north/south.");
I am learning Java and am making code that converts pennies in to change. It is completed however, I am unsure what to enter for the while loop. I would have used while(!change.equals("END")) but this can't be done because change is an integer, so what can I do?
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();
}
}
}
Thanks :)
It depends on what BIO is. Generally, there are options like .hasNextToken() if it is Enumerable. But, without knowing what that variable is declared as, I can't tell you what your trigger would be.
maybe this will work:
boolean flag = true;
while(flag){
//do things
if(condition_when_you_want_your_loop_to_stop){
flag = false;
}
}