Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing While Loops, Using Scanner Input Variables
    text
    copied!<p>I need help understanding how i can link variables from a class to my main class, it seems to work during certain instances of code I've used before but I'm not clear on how it works. in this case, the Dollar variable needs to pull in an <code>integer</code> from my <code>main</code> method in the tester class that comes from a scanner input. Also how do I implement my <code>while</code> loop and have it call other methods so that it prints the desired variable. the point of this program is too find out the max bars you can get with a fixed amount of money, assuming each is 1 dollar, each bar has one coupon, and 6 coupons get you a free bar(with the 1 coupon inside). Im a beginner and I cant figure out where to go next.</p> <pre><code> public class VendingMachine { public static int dollars; public static int coupons = dollars; public static int bars = dollars; public VendingMachine (int x) { dollars = x; } public VendingMachine() { this (0); } public static void Bars() { int newbars; newbars = coupons/6; bars = bars + newbars; } public static void Coupons() { int newcoupons; newcoupons = (coupons % 6) +(coupons/6); coupons = newcoupons + coupons; } } import java.util.*; public class VendMachineTest{ public static void main(String[]args, int coupons) { Scanner user_input = new Scanner(System.in); int dollars; System.out.println("Amount of Cash?"); dollars = user_input.nextInt(); VendMachineTest totalbars = new VendMachineTest(); while ( coupons &gt;= 6) { VendMachineTest.Bars(); VendMachineTest.Coupons(); } } System.out.println( dollars +""+coupons+""+bars+""); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload