While loop program

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 3171 Views - Last Post: 21 February 2016 - 04:49 PM Rate Topic: -----

#16 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: While loop program

Posted 21 February 2016 - 02:51 PM

Can you post a sample output of what you want printed? Use the same format as in the first part of post#12
Was This Post Helpful? 0
  • +
  • -

#17 redvelvet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 21-February 16

Re: While loop program

Posted 21 February 2016 - 03:00 PM

Desired Output:
Insert two tokens.
You have 18 tokens left.
Would you like to play another game? Insert two tokens.
.
.
.
.
.
.
.
.
.
You have 0 tokens left.
You can't play anymore games.
Was This Post Helpful? 0
  • +
  • -

#18 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: While loop program

Posted 21 February 2016 - 03:09 PM

What are the . . .s for? Is that the desired output instead of the number of tokens? No problem.
With a for loop, the start value of the loop variable would be 20, the end value 0 and the change would be -2.
Try writing a for loop with those values. Add a print statement inside the loop that prints a "."
Was This Post Helpful? 0
  • +
  • -

#19 redvelvet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 21-February 16

Re: While loop program

Posted 21 February 2016 - 03:12 PM

Oh I am sorry the "." just means like it would repeat itself.
Output:
Insert two tokens.
You have 18 tokens left.
Would you like to play another game? Insert two tokens.
You have 16 tokens left.
Would you like to play another game? Insert two tokens.
You have 14 tokens left.
(and this continues on until the user has no more tokens left)
You have 0 tokens left.
You can't play anymore games.
(this would be the end of the program)
Was This Post Helpful? 0
  • +
  • -

#20 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: While loop program

Posted 21 February 2016 - 03:21 PM

Ok, instead of printing a ".", print the desired message with the number in the middle of it.
Was This Post Helpful? 0
  • +
  • -

#21 redvelvet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 21-February 16

Re: While loop program

Posted 21 February 2016 - 03:40 PM

        public static void main(String[] args) {
        
        Scanner keyboard = new Scanner(System.in);
        
        for (int i = 20; (i <= 20 && i >= 0); i-=2)
        {
            System.out.println(i);
            System.out.println("Enter two tokens to play.");
            i = keyboard.nextInt();
        }  



This is the output I am getting:
20
Enter two tokens to play.
2
0

Which is not what I want
Was This Post Helpful? 0
  • +
  • -

#22 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: While loop program

Posted 21 February 2016 - 03:55 PM

It is usually a bad idea for beginners to change the value of the loop control variable inside of the loop.
Create a new variable to receive the user's input, don't use i.
Was This Post Helpful? 0
  • +
  • -

#23 redvelvet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 21-February 16

Re: While loop program

Posted 21 February 2016 - 03:59 PM

I am very sorry if I am not understand that well, this is the first programming class I have ever taken. Not really an excuse but just letting you know where I am coming from. So I do not use i at all? And where did I change the value of the loop control variable?
Was This Post Helpful? 0
  • +
  • -

#24 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: While loop program

Posted 21 February 2016 - 04:04 PM

Quote

where did I change the value of the loop control variable?

i is the loop control variable
line 9 changes its value.
Was This Post Helpful? 0
  • +
  • -

#25 redvelvet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 21-February 16

Re: While loop program

Posted 21 February 2016 - 04:12 PM

How would the program know when to stop?
Was This Post Helpful? 0
  • +
  • -

#26 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: While loop program

Posted 21 February 2016 - 04:29 PM

Quote

How would the program know when to stop?

Read the tutorial about how for loops work:
http://docs.oracle.c...dbolts/for.html
Was This Post Helpful? 0
  • +
  • -

#27 redvelvet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 21-February 16

Re: While loop program

Posted 21 February 2016 - 04:33 PM

        public static void main(String[] args) {
        
        Scanner keyboard = new Scanner(System.in);
        
        int remainingTokens;
        for (int i = 20; (i <= 20 && i >= 0); i-=2)
        {
            
            System.out.println("Enter two tokens to play.");
            int userInput = keyboard.nextInt();
            remainingTokens = i - userInput;
            System.out.println("You have these tokens left " + remainingTokens + ".");
        if (remainingTokens <= 0) {
            
            System.out.println("No more tokens.");
        }
            
            
        }  




It works except that the program stops when theres -2 tokens left and not 0. I need it to stop at 0.
Was This Post Helpful? 0
  • +
  • -

#28 redvelvet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 21-February 16

Re: While loop program

Posted 21 February 2016 - 04:45 PM

Well with the break it stops at 0.

        
        public static void main(String[] args) {
        
        Scanner keyboard = new Scanner(System.in);
        
        int remainingTokens;
        for (int i = 20; (i <= 20 && i >= 0); i-=2)
        {
            
            System.out.println("Enter two tokens to play.");
            int userInput = keyboard.nextInt();
            remainingTokens = i - userInput;
            System.out.println("You have these tokens left " + remainingTokens + ".");
        if (remainingTokens <= 0) {
            
            System.out.println("No more tokens.");
        break;
        }
            
            
        } 



Was This Post Helpful? 0
  • +
  • -

#29 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: While loop program

Posted 21 February 2016 - 04:47 PM

Does that program give you the desired output?
Was This Post Helpful? 0
  • +
  • -

#30 redvelvet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 21-February 16

Re: While loop program

Posted 21 February 2016 - 04:49 PM

Yes it does! The output is:
Enter two tokens to play.
2
You have these tokens left 18.
Enter two tokens to play.
2
You have these tokens left 16.
Enter two tokens to play.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2