10 Replies - 1193 Views - Last Post: 18 January 2013 - 07:28 AM Rate Topic: -----

#1 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Java Lotto program

Posted 16 January 2013 - 12:39 PM

Hi I am trying to teach myself Java as a hobby and I am basically learning from youtube and questions from java books

I came across this question below and I am stuck on a couple of things.

1) How do I get a yes/no response from the user until they enter n
2) How do I display the final results

I have tried to answer this question myself but I am stuck and could use a little assistance please

Write a program using a while loop which will ask the user to enter three Lotto numbers.
The program should then repeatedly ask the user if they wish to select three different Lotto
Numbers until they reply n, in which case the program will display the final 3 numbers.*/
import java.util.Scanner;
public class Question14
{
	public static void main (String [] args)
	{
		Scanner kb = new Scanner(System.in);
		
		int numChoice = 1; //store 3 number choices
		int i = 1; //initiaize index counter i
		
		System.out.println("Please enter lotto number between 1 and 40");
		while (i <=3)// loop 3 times while i is less than 3
		{
			numChoice = kb.nextInt(); // get user input and store the value in numChoice

			System.out.println(" Number " + i + " entered was " + numChoice);
			i++;
						 
			if(i > 3)
			System.out.println ("Would you like to enter 3 new numbers?");
		}
	}//end main
	
}//end of class



Is This A Good Question/Topic? 0
  • +

Replies To: Java Lotto program

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,096
  • Joined: 19-March 11

Re: Java Lotto program

Posted 16 January 2013 - 01:00 PM

Use your Scanner to get a response: something like

response = kb.nextLine();

will do the work.

To check this, use a while loop:


String response = "y";  // so it'll go through the first time
while (! response.equals('n')) {  // you can refine this in various ways later
  // do stuff here
  
  // when finished, prompt the user and get the response, as above
  System.out.println ("Would you like to enter 3 new numbers?");
  response = kb.nextLine();
}



One gotcha to watch out for: nextInt() is tricky, because it doesn't consume the whole line, just the next token on the line. This means that you have to use another call to

kb.nextLine();


after you call nextInt() and BEFORE you call nextLine for real - just after kb.nextInt() would be the right place to do that. There's another thread where I've explained this today, so look for that if you want the details.


EDIT: here

This post has been edited by jon.kiparsky: 16 January 2013 - 01:01 PM

Was This Post Helpful? 1
  • +
  • -

#3 FallenG  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 12-January 13

Re: Java Lotto program

Posted 16 January 2013 - 01:05 PM

Logically you are going to need two (nested) loops in your program. You need to keep looping round until the user is happy with their choice of lotto numbers, and you need to loop to get the three lotto numbers.

At the moment it looks as if you going to add the "Are you happy with your lotto numbers" (for want of a better description) inside the loop to enter their lotto numbers. I would suggest this is a bad idea, and what you want to structure your program as follows:

while (theUserWantsToEnterNewNumbers) {
    // Ask them for three numbers

    if (theUserIsHappyWithTheirChoice)
        theUserWantsToEnterNewNumbers = false;
}


Note that I don't propose those variable names (descriptive is good, but they are O.T.T). I am just demonstrating how I would recommend laying out the logic.
Was This Post Helpful? 1
  • +
  • -

#4 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Java Lotto program

Posted 16 January 2013 - 02:02 PM

Thanks guys for your help

Why is my code now going straight to the decision after I enter one number?

public class Question14
{
	public static void main (String [] args)
	{
		Scanner kb = new Scanner(System.in);
				
		int numChoice = 1; //store 3 number choices
		int i = 1; //initiaize index counter 
		char decision = 'c';
		
		System.out.println("Please enter lotto number between 1 and 40");
		while (i <=3)// loop 3 times while i is less than 3
		{
			numChoice = kb.nextInt(); // get user input and store the value in numChoice

			System.out.println(" Number " + i + " entered was " + numChoice);
			i++;
			
			if(i > 3)
			
			System.out.println ("Would you like to enter 3 new numbers?please enter y or n");	
			decision = kb.next().charAt(0);	
			
			if (decision == 'y')
			{
				System.out.println("You have chosen to enter 3 new numbers");
			}
			else
			{
				System.out.println("No! You are happy with your numbers");
			}	
		 }	
	
	}//end main
	
}//end of class


Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Java Lotto program

Posted 16 January 2013 - 04:47 PM

Trace the logic through the while() loop using pencil and paper. Set i = 1 (just as the program does), then work through the loop, noting variable values (including i) as each loop statement is executed.

Remember the structure of an if statement: either the if statement includes the statement immediately following, OR it includes multiple lines following the if statement iff they are enclosed in braces, '{ }'.
Was This Post Helpful? 2
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Java Lotto program

Posted 16 January 2013 - 11:23 PM

Well, I think your if condition on line 19 should be having braces as shown below or else the user will be asked to input even if the value of i is not greater than 3.

                if(i > 3)
20	          {   
21	            System.out.println ("Would you like to enter 3 new numbers?please enter y or n");  
22	            decision = kb.next().charAt(0);
23	             
24	            if (decision == 'y')
25	            {
26	                System.out.println("You have chosen to enter 3 new numbers");
27	            }
28	            else
29	            {
30	                System.out.println("No! You are happy with your numbers");
31	            }  
32	         }//closing braces of if(i>3)
            }//closing braces of while loop         
        }//closing braces of main
    }//closing braces of class



regards,
Raghav

This post has been edited by raghav.naganathan: 16 January 2013 - 11:25 PM

Was This Post Helpful? 1
  • +
  • -

#7 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Java Lotto program

Posted 17 January 2013 - 01:34 AM

Hey Guys! Thanks a lot for your help

I realize now that I had to nest the if statement to get the logic to work properly.

This is a really great site with kind and talented programmers

One more thing I am having difficulty with is

How do I print the final numbers after the 3 number choices have been made?

Thanks again
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Java Lotto program

Posted 17 January 2013 - 04:57 AM

I suspect you haven't learned arrays, so I won't suggest that. Let me know if I'm wrong.

Your program currently stores only the user's current choice; it does not store the user's choice history to capture the last 3 choices. You'll have to create variables in which to store the last 3 choices, and then you'll have to write the logic to keep those values fresh.

When ready to print the 3 choices, you'll do something like:

System.out.println( "The three choices are " + choice1 +
", " + choice2 + ", " + choice3 + "." );

Was This Post Helpful? 0
  • +
  • -

#9 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Java Lotto program

Posted 17 January 2013 - 05:20 AM

Hi Greg

Thanks again for your help

It's funny that you mentioned arrays . I have just started looking at arrays and I determined that they would be an ideal solution for this problem.

I made this test program this morning to see how I can store and print values easily
import java.util.Scanner;

public class ArrayInput
{
			
	public static void main (String[] args)
   {
	    Scanner input = new Scanner(System.in);
	    int[] numbers = new int[3]; //declare and initialise an int array called numbers
		 
		 int total = 0;
		 int i;
	
	    for (i = 0; i < numbers.length; i++)//for loop for user input
	    {
	        System.out.println("Please enter number");
	        numbers[i] = input.nextInt();
			  System.out.println(numbers[i]);//print out index value in this case 3 entries
		 }
		 
		 for (i = 0; i < numbers.length; i++)//for loop to display the numbers entered
		 {
		 	System.out.println( "Number " +i + " entered was " + numbers[i]);
		 }
		
	} 
		   	
}



Was This Post Helpful? 1
  • +
  • -

#10 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Java Lotto program

Posted 18 January 2013 - 01:59 AM

Very good effort Kevin :) That program will certainly work according to what you want.

Nevertheless, I have a few suggestions for you.

1. In line 11, you have initialized total to 0...but nowhere in the program have you used that variable, so it would better if you can remove that line from your program.

2. In your line 12, you have declared i. Well, you can avoid that declaration considering that you are only using i in the for loops. So, something like this would be better.

for(int i=0; i < numbers.length; i++)

3. You seem to be printing the values of the array elements immediately after scanning them.Although what you have done is not wrong, I would suggest you allow the first for loop to only scanning the array elements and the second for loop to print them.

Apart from that, a very well written code with appropriate comment lines where necessary, and also good indentation of your code. You have my +1 for that. :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#11 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Java Lotto program

Posted 18 January 2013 - 07:28 AM

Hey Raghav

Thanks a lot for your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1