I am learning Java and am making code that converts the number of pennies in to change when entered.
One thing I need to change is my output so that if there is only one coin it prints solely as 20p rather than 1*20p.
I would also appreciate if anyone notes any other improvements that could be made.
Thanks :)
class Main {
public static void main( String args[] ) {
System.out.print("#Please enter the amount of change : ");
int change = BIO.getInt();
while(change > 0)
{
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;
one = left / 1;
int nbCoins = twopounds + pounds + fifty + twenty + ten + five + two + one;
if (change == 1)
{
System.out.print("1 coin" + "\n");
}
if (change > 500)
{
System.out.print("Invalid amount " + change + "p" + "\n");
}
if (change <= 500 && change > 1)
System.out.print(change + "p " + nbCoins +" coins ");
{
if ( twopounds > 0 )
{
System.out.print( twopounds > 0 ? twopounds + "*200p " : "" );
}
if ( pounds > 0 )
{
System.out.print( pounds > 0 ? pounds + "*100p " : "" );
}
if ( fifty > 0 )
{
System.out.print( fifty > 0 ? fifty + "*50p " : "" );
}
if ( twenty > 0 )
{
System.out.print( twenty > 0 ? twenty + "*20p " : "" );
}
if ( ten > 0 )
{
System.out.print( ten > 0 ? ten + "*10p " : "" );
}
if ( five > 0 )
{
System.out.print( five > 0 ? five + "*5p " : "" );
}
if ( two > 0 )
{
System.out.print( two > 0 ? two + "*2p " : "" );
}
if ( one > 0 )
{
System.out.print( one > 0 ? one + "*1p " : "" );
}
System.out.print("\n");
}
System.out.print("#Please enter the amount of change : ");
change = BIO.getInt();
}
}
}
what you could do is define your coins as emums,
ie:
enum Coin {
twopounds(200), pounds(100), fifty(50), twenty(20), ten(10), five(5), two(
2), one(1);
int value;
Coin(int value) {
this.value = value;
}
int getCoins(int amount){
return amount/value;
}
int getReminder(int amount){
return amount%value;
}
}
then you could do
int change = 321;
for (Coin coin : Coin.values()){
int count = coin.getCoins(change );
if (count!=0){
System.out.println(coin.name()+" : " +count);
}
change = coin.getReminder(change );
}
and that will prints you your change in coins,
you could take this further, and define your wallet where you hold all your money
ie
class Wallet{
int money;
int getAllCoins(Coin coin){
int coins = coin.getCoins(money);
money=coin.getReminder(money);
return coins;
}
}
but this will be overkill for this simple scenario
You don't want entries with 1 coin to be displayed as 1*value so replace the comparison
System.out.print( twopounds > 0 ? twopounds + "*200p " : " " );
to
System.out.print( twopounds > 1 ? twopounds + "*200p " : "200p " );
in other words, you compare the numberOfCoins to one, if it's just one coin, then write just the value of the coin. Else write numberOfCoins * value
Replace the relevant part of your code with this:
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 " );
}
Related
This question already has answers here:
Align printf output in Java
(5 answers)
Closed 23 days ago.
I am currently working on a program which displays the multiplication table, but I'm not sure how to line them up properly, as the spacing varied, making the table not correct.
void Multiply_Table(){//open method
int i = 0, ii = 0, iii = 0, iv = 0, v = 0, vi = 0, vii = 0, viii = 0, ix = 0, x = 0; //VARIABLES
System.out.println("");
System.out.println("--------------- MULTIPLICATION TABLE ---------------");
System.out.println("");
System.out.println(" | 1 2 3 4 5 6 7 8 9 10 ");
System.out.println("---|------------------------------------------------");
System.out.print("1 | "); //NUMBER 1
while ( i < 10 ){
i+=1;
System.out.print( i + " " );
}
System.out.print("\n2 | "); //NUMBER 2
while ( ii < 20 ){
ii+=2;
System.out.print( ii + " " );
}
System.out.print("\n3 | "); //NUMBER 3
while ( iii < 30 ){
iii+=3;
System.out.print( iii + " " );
}
System.out.print("\n4 | "); //NUMBER 4
while ( iv < 40 ){
iv+=4;
System.out.print( iv + " " );
}
System.out.print("\n5 | "); //NUMBER 5
while ( v < 50 ){
v+=5;
System.out.print( v + " " );
}
System.out.print("\n6 | "); //NUMBER 6
while ( vi < 60 ){
vi+=6;
System.out.print( vi + " " );
}
System.out.print("\n7 | "); //NUMBER 7
while ( vii < 70 ){
vii+=7;
System.out.print( vii + " " );
}
System.out.print("\n8 | "); //NUMBER 8
while ( viii < 80 ){
viii+=8;
System.out.print( viii + " " );
}
System.out.print("\n9 | "); //NUMBER 9
while ( ix < 90 ){
ix+=9;
System.out.print( ix + " " );
}
System.out.print("\n10 | "); //NUMBER 10
while ( x < 100 ){
x+=10;
System.out.print( x + " " );
}
}//close method
I tried different spacing, but that didn't work.
I guess the format you need like this, having a try:
void Another_Multiply_Table() {
int rowCount = 10; int columnCount = 10;
System.out.println();
System.out.println("--------------- MULTIPLICATION TABLE ---------------");
System.out.println();
System.out.print(format("", 2) + " | ");
for (int i = 1; i <= columnCount; i++) {
System.out.print(format(i, 4));
}
System.out.println();
System.out.println("---|------------------------------------------------");
for (int row = 1; row <= rowCount; row++) {
System.out.print(format(row, 2) + " | ");
for (int column = row; column <= columnCount * row; column += row) {
System.out.print(format(column, 4));
}
System.out.println();
}
}
String format(Object num, int maxLength) {
String numStr = num + " ";
return numStr.substring(0, maxLength);
}
I have tried using casting with my variables as well as some of the other tips that I have read on here about getting percentages and its not working here is the code. There is a lot more to it, of course. I have tried all ways from casting, to what I have displayed.
(EDITED AGAIN 2nd time)
Here is the whole code. You were right is was working but in this, it is not! Even if the grade variables are float or int, still not working. Changed the grade variables so they don't display a decimal!. Output (BELOW) shows the grade but not the percentage. Thanks for the feedback.
OUTPUT:
Please enter a grade from 0 - 100! Enter -1 to end the program! 99
Please enter a grade from 0 - 100! Enter -1 to end the program! -1
The total number of grades is 2
Number of A's = 1 which is %0.0
Number of B's = 0 which is %0.0
Number of C's = 0 which is %0.0
Number of D's = 0 which is %0.0
Number of F's = 0 which is %0.0
Code:
public class Main {
public static void main(String[] main) {
Scanner input = new Scanner(System.in) ;
int totalGrades = 1 ;
int gradeA = 0 ;
float gradeAPercentage = (gradeA * 100f) / totalGrades ;
int gradeB = 0 ;
float gradeBPercentage = gradeB / totalGrades ;
int gradeC = 0 ;
float gradeCPercentage = gradeC / totalGrades ;
int gradeD = 0 ;
float gradeDPercentage = gradeD / totalGrades ;
int gradeF = 0 ;
float gradeFPercentage = gradeF / totalGrades ;
int a = 0 ;
do{
System.out.println("Please enter a grade from 0 - 100! Enter -1 to end the program!") ;
int grade = input.nextInt() ;
if( (grade == -1) && (totalGrades == 0) ) {
System.out.println("You entered no grades!") ;
totalGrades--;
break ;
}
else if(grade == -1){
a++ ;
}
else if(grade <= 59){
gradeF++ ;
totalGrades++ ;
}
else if( (grade >= 60) && (grade <= 69) ){
gradeD++ ;
totalGrades++ ;
}
else if( (grade >= 70) && (grade <= 79) ){
gradeC++ ;
totalGrades++ ;
}
else if( (grade >= 80) && (grade <= 89) ){
gradeB++ ;
totalGrades++ ;
}
else if( (grade <= 100) && (grade >= 90) ){
gradeA++ ;
totalGrades++ ;
}
else{
System.out.println("Your input is Invalid") ;
continue ;
}
}while(a < 1 ) ;
System.out.println("The total number of grades is " + totalGrades) ;
System.out.println("Number of A's = " + gradeA + " which is %" + gradeAPercentage) ;
System.out.println("Number of B's = " + gradeB + " which is %" + gradeBPercentage) ;
System.out.println("Number of C's = " + gradeC + " which is %" + gradeCPercentage) ;
System.out.println("Number of D's = " + gradeD + " which is %" + gradeDPercentage) ;
System.out.println("Number of F's = " + gradeF + " which is %" + gradeFPercentage) ;
}
}
There is an implicit casting done, no additional casting to float is needed. The code you provided works fine. If you print the result with
System.out.println(gradeAPercentage);
you will get, what you have wanted to calculate.
I'm attempting to fill a 2D array with the numbers 1 to 1000 and then show all the factors of those numbers. I then need to find all the prime numbers in the same array and output them. Here's what I have so far, keep in mind that I was hoping to do every step in its own method then return them but have not got that far yet
int i = 0;
//int x = 0;
String primeNumber = "";
int[] [] factorArray = new int [1000] [];
for (int x = 0 ; x < 1000 ; x++)
{
int remainder;
int y;
remainder = x % 2;
y = x / 2;
if (remainder != 0)
System.out.println (x + ": " + "1, " + x);
else if (remainder == 0)
System.out.println (x + ": " + (y) + " , " + (y / 2) + " , " + " 1, " + x);
}
for (i = 1 ; i <= 1000 ; i++)
{
int ctr = 0;
for (int x = i ; x >= 1 ; x--)
{
if (i % x == 0){
ctr = ctr + 1;
}
}
if (ctr == 2)
{
primeNumber = primeNumber + i + " ";
}
}
System.out.print ("Prime numbers from 1 - 1000 are : \n" + primeNumber);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This program will generate a random number and ask user to disperse
the correct amount of change. If user has entered the wrong amount
the program will say Incorrect by : +difference...
mine refuses to work...
*/
// ---------1---------2---------3---------4---------5---------6---------7
// 1234567890123456789012345678901234567890123456789012345678901234567890
import java.util.Scanner;
import java.util.Random;
public class ChangeGame9
{
public static void main(String[] args)
{
int low = 1;
int max = 10000;
int twentyDollars = 0;
int tenDollars = 0;
int fiveDollars = 0;
int oneDollar = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
Random random = new Random();
int randomNumber = random.nextInt(max - low + 1);
double randomNumber1 = (randomNumber /100.0);
double userAmount = twentyDollars * 20 + tenDollars * 10 +
fiveDollars * 5 + oneDollar * 1 + quarters * 0.25
+ dimes * 0.10 + nickels * 0.05 + pennies * 0.01;
double difference = Math.abs( randomNumber1 - userAmount);
double differenceOne = (int)(difference*100 + 0.5) / 100.0;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Twenties: ");
twentyDollars = keyboard.nextInt();
while( twentyDollars <= 0 );
{
System.out.println( "** Invalid Amount **\n" +
"Twenties: " );
twentyDollars = keyboard.nextInt();
}
System.out.println("Tens: ");
tenDollars = keyboard.nextInt();
while( tenDollars < 0 || tenDollars > 1 )
{
System.out.println( "** Invalid Amount **\n" +
"Tens: " );
tenDollars = keyboard.nextInt();
}
System.out.println("Fives: ");
fiveDollars = keyboard.nextInt();
while( fiveDollars < 0 || fiveDollars > 1 )
{
System.out.println( "** Invalid Amount **\n" +
"Fives: " );
fiveDollars = keyboard.nextInt();
}
System.out.println("Ones: ");
oneDollar = keyboard.nextInt();
while( oneDollar > 4 || oneDollar < 0 )
{
System.out.println( "** Invalid Amount **\n" +
"Ones: " );
oneDollar = keyboard.nextInt();
}
System.out.println("Quarters: ");
quarters = keyboard.nextInt();
while( quarters < 0 || quarters > 3 )
{
System.out.println( "** Invalid Amount **\n" +
"Quarters: " );
quarters = keyboard.nextInt();
}
System.out.println("Dimes: ");
dimes = keyboard.nextInt();
while( dimes < 0 || dimes > 2 )
{
System.out.println( "** Invalid Amount **\n" +
"Dimes: " );
dimes = keyboard.nextInt();
}
System.out.println("Nickels: ");
nickels = keyboard.nextInt();
while( nickels < 0 || nickels > 1 )
{
System.out.println( "** Invalid Amount **\n" +
"Nickels: " );
nickels = keyboard.nextInt();
}
System.out.println("Pennies: ");
pennies = keyboard.nextInt();
while( pennies < 0 || pennies > 4 )
{
System.out.println( "** Invalid Amount **\n" +
"Pennies: " );
pennies = keyboard.nextInt();
}
while ( differenceOne == 0)
{
System.out.println("Correct!");
}
}
while( differenceOne != 0);
{
System.out.println("Incorrect by: " + differenceOne);
}
}
}
----jGRASP exec: java ChangeGame9
Enter the change for 52.38
Twenties:
2
** Invalid Amount **
Twenties:
2
Tens:
1
Fives:
0
Ones:
2
Quarters:
1
Dimes:
1
Nickels:
0
Pennies:
3
Enter the change for 52.38
Twenties:
this is what is happening when I run my code...I don't understand what the problem is...
while( twentyDollars <= 0 );
{
System.out.println( "** Invalid Amount **\n" +
"Twenties: " );
twentyDollars = keyboard.nextInt();
}
You have a semicolon after your while loop. This is interpreted as the body of your while loop being an empty statement, followed by the code that says invalid input.
If you notice, you can enter actual invalid input the second time it asks and it will accept it and continue onto the tens. Likewise if you enter invalid input at the first opportunity your program will freeze as it enters an infinite loop.
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;
}
}