could someone tell me what\'s wrong with this code and how do I fix it? the outp
ID: 440228 • Letter: C
Question
could someone tell me what's wrong with this code and how do I fix it? the output is messed up... import java.util.*; public class Coins { public static final Scanner CONSOLE = new Scanner(System.in); public static void main(String[] args) { System.out.println("Lab 2 Written by Barzan Abdulaziz"); DollarAmount( ); } public static void DollarAmount( ) { System.out.print("Enter the number of Quaters: "); int QuaterDollar = CONSOLE.nextInt( ); double amount = QuaterDollar * 0.25; System.out.println(QuaterDollar + " Quater is $" + amount); DimeAmount(); } public static void DimeAmount( ) { System.out.print("Enter the number of dimes: "); int DimeDollar = CONSOLE.nextInt( ); double amount = DimeDollar * 0.10; System.out.println(DimeDollar + " Dime is $" + amount); NickleAmount(); } public static void NickleAmount( ) { System.out.print("Enter the number of Nickles: "); int NickleDollar = CONSOLE.nextInt( ); double amount = NickleDollar * .05; System.out.println(NickleDollar + " Nickle is $" + amount); PennyAmount(); } public static void PennyAmount( ) { System.out.print("Enter the number of Pennies: "); int PennyDollar = CONSOLE.nextInt( ); double amount = PennyDollar * 0.1; System.out.println(PennyDollar + " Penny is $" + amount); System.out.println("The total is" + PennyDollar + NickleDollar+ DimeDollar + QuaterDollar); } }Explanation / Answer
this is the corrected code..
the problem with previous code is that ..u hav declared pennyDollar ,nikkleDollar dimeDollar and quaterDollar variable in local scope of the respective method..u sholud declare them at class level to access them through out the class..
1-- visibility of any local variable is inside the block it is declared.
2-- visibility of a class variable is throough out the class..inside any block..
so i just declar those variable at class level..
import java.util.*;
public class test1
{
public static final Scanner CONSOLE = new Scanner(System.in);
public static int QuaterDollar;
public static int NickleDollar;
public static int PennyDollar;
public static int DimeDollar;
public static void main(String[] args)
{
System.out.println("Lab 2 Written by Barzan Abdulaziz");
DollarAmount( );
}
public static void DollarAmount( )
{
System.out.print("Enter the number of Quaters: ");
QuaterDollar = CONSOLE.nextInt( );
double amount = QuaterDollar * 0.25;
System.out.println(QuaterDollar + " Quater is $" + amount);
DimeAmount(); }
public static void DimeAmount( )
{
System.out.print("Enter the number of dimes: ");
DimeDollar = CONSOLE.nextInt( );
double amount = DimeDollar * 0.10;
System.out.println(DimeDollar + " Dime is $" + amount);
NickleAmount();
}
public static void NickleAmount( )
{
System.out.print("Enter the number of Nickles: ");
int NickleDollar = CONSOLE.nextInt( );
double amount = NickleDollar * .05;
System.out.println(NickleDollar + " Nickle is $" + amount);
PennyAmount();
}
public static void PennyAmount( )
{
System.out.print("Enter the number of Pennies: ");
int PennyDollar = CONSOLE.nextInt( );
double amount = PennyDollar * 0.1;
System.out.println(PennyDollar + " Penny is $" + amount);
System.out.println("The total is" + PennyDollar + NickleDollar+ DimeDollar + QuaterDollar);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.