CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Need Help with Array.

    import java.io.*;
    import java.lang.*;
    import java.util.Random;
    import java.math.*;



    public class first{
    public static void main(String args[])throws IOException{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Mathematical Operations Drill");
    System.out.println("1.Practice");
    System.out.println("2.Challenge");
    System.out.println();
    System.out.println("Enter choice:");
    int choice=Integer.parseInt(br.readLine());
    System.out.println("Number of items:");
    int items=Integer.parseInt(br.readLine());

    generator.rangen(items);
    }


    public static class generator extends first{
    public static void rangen(int items){
    Random rand = new Random();
    for(int counter=1; counter<items;counter++)
    {
    int x=1+rand.nextInt(100);
    int y=1+rand.nextInt(100);
    int z=1+rand.nextInt(4);
    switch(z){
    case 1:System.out.println(+x+"+"+y+"=");break;
    case 2:System.out.println(+x+"-"+y+"=");break;
    case 3:System.out.println(+x+"*"+y+"=");break;
    case 4:System.out.println(+x+"/"+y+"=");break;
    }
    }
    }
    }
    }
    How can I store the user's answers into an array?

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: Need Help with Array.

    Your method asks two questions from the user, never more, if that is all you need, need for an array does not make sense. Refactor your question - answer logic such that you are capable to run it untill user wants to quit. Then it makes sense to collect the answers. For that use a Collection, such as ArrayList.

    Arrays are based on fixed index, a size that needs to declared upon creating a new Array. To read / write, you use simple syntax myvalue=myArray[index]; or respectively myArray[index]=myvalue. Arrays are usefull when you know the amouth of items beforehand, in more abstract cases Collections are much less fuss.

    Always better to choose the working tool for the task. Scanner class already contains what you need, so you simply run into troubles you dont know to expect using BufferedReader. What happens when you type alphabetical values as an answer? While reinventing the wheel is sometimes a rewarding experience, dont do it unless you are prepared to develop your tool far enough to meet the requirements.

    If you want to develop your program further, break the logic to simpler steps. Devise methods that ask the user a question and return an answer. Ponder how do you validate the answer is correct, or correct type, how do you keep on asking untill the validation is a success. How do you use the Collection class implementations to store results and finally when user quits the drill, how you do produce the results of the drill. And remember to break it into methods, for love of god, do not cram all into the main A program such as this is a very good entry step. While doing it, think what could you do so that very little needs to be changed to use your code in eg. asking for personel data, cooking book recipes etc. Try to think bigger than the box of the day... its the OO way.
    Last edited by Londbrok; October 23rd, 2009 at 02:16 AM.

  3. #3
    Join Date
    Oct 2009
    Posts
    3

    Re: Need Help with Array.

    I'm sorry I guess I wasn't too specific of what I was asking.
    This program will generate:
    Random Mathematical Operations
    ex.
    6/37=
    97-25=
    70/67=
    14*38=
    39+66=
    56/31=
    72/42=
    35*2=
    26-85=
    88+39=
    31+42=
    41*60=
    64/70=
    47*20=
    15+41=
    98-15=

    I was asking how do I put the user's answer to each question into an array...

  4. #4
    Join Date
    Apr 2007
    Posts
    442

    Re: Need Help with Array.

    You printing out the mathematical equations does not equal you asking the user for any input. Again, as it is, your application asks only two inputs. You need to refactor the way you handle the question - answer routine.

  5. #5
    Join Date
    Oct 2009
    Posts
    3

    Re: Need Help with Array.

    Code:
    import java.io.*;
    import java.lang.*;
    import java.util.Random;
    import java.math.*;
    
    
    
    public class first{
        public static void main(String args[])throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
            System.out.println("Mathematical Operations Drill");
            System.out.println("1.Practice");
            System.out.println("2.Challenge");
            System.out.println();
            System.out.println("Enter choice:");
            int choice=Integer.parseInt(br.readLine());
            System.out.println("Number of items:");
            int items=Integer.parseInt(br.readLine());
    
            switch(choice)
                    {
                    case 1:practice();break;
                    case 2:challenge();break
                    }
            int i=0;
            int[] ans;
            ans = new int[100];
    
    
            while ( i< items) {
                    generator.rangen();
                    ans[i]=Integer.parseInt(br.readLine());
                    i++;
       }
    
        }
    
    
    public static class generator {
    public static void rangen(){
        Random rand = new Random();
    	int x=1+rand.nextInt(100);
    	int y=1+rand.nextInt(100);
            int z=1+rand.nextInt(4);
            switch(z){
                case 1:System.out.println(+x+"+"+y+"=");break;
                case 2:System.out.println(+x+"-"+y+"=");break;
                case 3:System.out.println(+x+"*"+y+"=");break;
                case 4:System.out.println(+x+"/"+y+"=");break;
            }
        }
         
    }
    okay. i get it. it's like that right?
    now, I have another problem. how do I store the right answers into another array?

  6. #6
    Join Date
    Apr 2007
    Posts
    442

    Re: Need Help with Array.

    Refactor your rangedgen method so that it not only displays the question but also returns the right answer. Have another array and store the right answer to the same index. Thus you can then iterate both the lists, having the user input in one and correct result in another. Might be wisdom (if you use arrays and not Collections), to initialize the arrays with... say, Integer.MAX_VALUE. So that while iterating you know when the actual user submitted answers end.

    Might also be wisdom not to force the user to go through all 100 questions. Consider how you can refactor this allow user to eg. type "quit" and leave the questioning routine.
    Last edited by Londbrok; October 25th, 2009 at 02:52 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured