Can you post a sample output of what you want printed? Use the same format as in the first part of post#12
29 Replies - 3171 Views - Last Post: 21 February 2016 - 04:49 PM
#17
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.
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.
#18
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 "."
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 "."
#19
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)
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)
#20
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.
#21
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
#22
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.
Create a new variable to receive the user's input, don't use i.
#23
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?
#24
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.
#25
Re: While loop program
Posted 21 February 2016 - 04:12 PM
How would the program know when to stop?
#26
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
#27
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.
#28
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;
}
}
#29
Re: While loop program
Posted 21 February 2016 - 04:47 PM
Does that program give you the desired output?
#30
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.
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.

New Topic/Question
Reply




MultiQuote


|