-1

I'm trying to make my game give the percentage(%) of a user's grade but I keep getting 0%.

Here's my code :

int win = 2
int Percentage = win/5;
System.out.println();
System.out.println("Your score is : " + win + "/" + "5" + "(" + Percentage *100 + "%" + ")");

Console : Your score is : 2/5(0%)

Crypto
  • 60
  • 9

2 Answers2

6

You need to use double (floating point) arithmetic for this. FYI / 5.0 converts the int into a double

double Pct = win / 5.0;
ControlAltDel
  • 28,815
  • 6
  • 42
  • 68
1

The Integer type can only be whole numbers. If you want to create a percentage, you need to use the Double type. You can also use \n to create a new return line in your printout.

double win = 2.0;
double percentage = (win/5) * 100;
System.out.println("\n\n Your score is: " + win + "/5 (" + percentage + "%)"); 
iamwhoiam
  • 277
  • 1
  • 5
  • 15
JTMKoenig
  • 21
  • 6