Results 1 to 10 of 10
  1. #1
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default [SOLVED] Need help with Slope (Loops)

    I have most of it...I just need the code for where it displays how many the user got correct/incorrect.

    Actual Question:
    Each time the user enters a slope, the
    computer lets him/her know if she is correct or incorrect. If the user is
    incorrect, then the program displays the correct answer, before asking
    another. Also, the program should keep track of the total number of questions
    and the number correct and report this information. Name the program Slope3.

    Here is what I have so far:

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    class Loop3 {
    public static void main(String args[])
    {
    
    
    	int slope = 0;
    	int count = 0;
    	int count2= 0 ;
    	int yInt = 0;
    	int response = 0;
    	Scanner scan = new Scanner(System.in);
    	Random generator = new Random();
    	String intro = "You will be asked to find the slopes of several lines\n" +
    					"When you want to quit, type -500 for the value of the slope\n";
    	System.out.println(intro);
    	do
    	{
    
    		count = count + 1;
    		slope = generator.nextInt(20)+1;
    		yInt = generator.nextInt(20)+1;
    		System.out.println("What is the slope of the line y = " + slope + "x + " + yInt + "?");
    		System.out.println("You have tried " + count + " times.");
    		response = scan.nextInt();
    
    
    			if(response == slope)
    			{
    				count2 = count2 + 1;
    				System.out.println("Yes! you have tried " + count2 + " times");
    			}
    			else
    			{
    				System.out.println("No, try again ");
    			}
    
    
    			} while (response != -500);
    		}
    	}
    I am a Java n00b.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You are looking something like this?

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    class Loop3 {
        public static void main(String args[])
        {
    
            int correct = 0;
            int incorrect = 0;
            
    	int slope = 0;
    	int count = 0;
    	int count2= 0 ;
    	int yInt = 0;
    	int response = 0;
    	Scanner scan = new Scanner(System.in);
    	Random generator = new Random();
    	String intro = "You will be asked to find the slopes of several lines\n" +
    					"When you want to quit, type -500 for the value of the slope\n";
    	System.out.println(intro);
    	do
    	{
    
    		count = count + 1;
    		slope = generator.nextInt(20)+1;
    		yInt = generator.nextInt(20)+1;
    		System.out.println("What is the slope of the line y = " + slope + "x + " + yInt + "?");
    		System.out.println("You have tried " + count + " times.");
    		response = scan.nextInt();
    
    
    			if(response == slope)
    			{
    				count2 = count2 + 1;
    				System.out.println("Yes! you have tried " + count2 + " times");
                                    correct++;
    			}
    			else
    			{
    				System.out.println("No, try again ");
                                    incorrect++;
    			}
    
                            System.err.println("You have " + correct + " success and " + 
                                    incorrect + " failed.\n");
    			
            } while (response != -500);
        }
    }

  4. #4
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Why don't you use additional variables for that. Depend on the result update the value of it.
    I don't really know what that means (sorry), but I am just trying to get some help or code on what to put to keep track of how many the user got correct and incorrect.
    I am a Java n00b.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  6. #6
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    OMG...lol...sorry. Solved!
    I am a Java n00b.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Zebra View Post
    OMG...lol...sorry. Solved!
    If you solve the question, please mark the thread solved. ;)

  9. #9
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Alright got it to work. Thanks!
    I am a Java n00b.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 05:40 PM
  2. [SOLVED] Need help with Loops...please!
    By Zebra in forum New To Java
    Replies: 5
    Last Post: 04-10-2008, 01:44 PM
  3. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 03:59 AM
  4. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 06:47 PM

Posting Permissions

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