zenon
zenon

Reputation: 29

Java programming, program will not go pass the scanner into the while loop

Okay I feel like this may be hard to explain because I tend to be the only one in my classes to ever have this problem, but basically I am learning Java programming right now. I Have an issue where basically I have to scan a value for a variable then take that variable into a while loop, problem is that the program, when launched, does not start the while loop. It lets me keep on inputting numbers, but that is the problem, it keeps scanning numbers but it only needs to scan once, and it will not enter the loop. This is the program

import java.util.Scanner;
public class Week05_NelsonPimentel_Assignment {
    public static void main(String args[]){
        int veraq;
        int times = 0;
        Scanner input = new Scanner(System.in);


        System.out.println("Please enter how many coins you have");

        veraq = input.nextInt();
        while(veraq>0){


            firstmachine(veraq);
            howmanytimesplayed(times);
            secondmachine(veraq);
            howmanytimesplayed(times);
            thirdmachine(veraq);
            howmanytimesplayed(times);

        }


            System.out.println("You were able to play this many times before running out of quarters: " +times);



        }





    static int howmanytimesplayed(int times)
    {
        times++;
        return times;
    }

    static int firstmachine(int veraq)
    {
        int times = 0;


        if(times == 33){
            veraq = veraq + 24;
            times = 0;
            System.out.println("Congradulations! On machine number one you have won $6.25!! You have this many coins left: " +veraq);
            return veraq;
        }
        else if( times != 33)
        {
            veraq = veraq - 1;
            times++;
            return veraq;
        }
        return 0;
    }

    static int secondmachine(int veraq)
    {
        int times = 0;

        if(times == 99){
            veraq = veraq + 74;

            times = 0;
            System.out.println("Congradulations! On machine number two you have won $18.75!! You have this many coins left: " +veraq);
            return veraq;
        }
        else if( times != 99)
        {
            veraq = veraq - 1;
            times++;
            return veraq;
        }

        return 0;
    }


    static int thirdmachine(int veraq)
    {
    int times = 0;

        if(times == 9){
            veraq = veraq + 6;
            times = 0;**enter code here**
            System.out.println("Congradulations! On machine number three you have won $1.75!! You have this many coins left: " +veraq);
            return veraq;
        }
        else if( times != 9)
        {
            veraq = veraq - 1;
            times++;
            return veraq;
        }
        return 0;
    }
}

Upvotes: 1

Views: 61

Answers (1)

opensam
opensam

Reputation: 368

Your program is not constantly scanning for numbers, the while loop in your program is never being terminated i.e. it's running in an infinite loop.

'int' is a primitive data type in Java. when you call the method firstmachine(veraq); and make this method do some operation on the variable veraq , those changes would not be reflected on the veraq variable in the scope of main method. Hence if you pass a positive value for veraq, it basically would never be less than 0 and your while loop would run forever. Moreover, your program is written in such a way that nothing would be printed giving you the illusion that your program is constantly scanning for numbers.

You should learn what is call by value and what is call by reference. Then you should learn how these concepts relate to passing arguments to a method in Java. Good luck :)

Upvotes: 1

Related Questions