Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Beginner help with a do loop

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Beginner help with a do loop

    import java.util.Scanner;
     
    public class aritmetik {
     
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
     
     
    		int resultat, tal1, tal2;
     
    	System.out.print("Enter a digit:  ");
    	tal1 = keyboard.nextInt();
    	System.out.print("Enter another digit: ");
    	tal2 = keyboard.nextInt();
     
     
    	System.out.println("How much is: " +tal1 + "+" +tal2 +"?");
    		resultat = keyboard.nextInt();
     
     
    		if(resultat == tal1 + tal2){
    			System.out.println("Your answer was right!");
    		}else{
    			System.out.println("Your answer was wrong.");
     
    			}
     
    		}
    }

    Hey,
    I'm a total newbie to Java programming. Itīs a course i'm taking in the university. Itīs actually a lot of fun (when it works) not so fun when it doesn't

    Anyway I've managed to program whats above, and it works fine. But I would like to tweak it a bit. I want to the program, say if you answer right: the program asks you if you would like to continue doing the same process again (but entering other digits) and if you answer wrong it will say: wrong answer, try again and let you enter the sum once more until you are right.

    I have watched I think 5 different youtube-videos about loops. I think a Do loop would be the case for me. But I have no idea how to implement it. Could someone be helpful and show me a bit for starters so that I have something to go on with

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner help with a do loop

    Do loop would be the case for me. But I have no idea how to implement it.
    Look at the tutorial for how to code a do{}while loop:
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    If you have problems coding it, post the code and and explanation of the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help with a do loop

    import java.util.Scanner;
     
    public class beraknaTal {
     
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
     
     
    		int resultat, tal1, tal2;
     
    	System.out.print("Ange ett heltal: ");
    	tal1 = keyboard.nextInt();
    	System.out.print("Ange ett till heltal: ");
    	tal2 = keyboard.nextInt();
     
    	do{	
    	System.out.print("Vad blir: " +tal1 + "+" +tal2 +"?");
    		resultat = keyboard.nextInt();
    	}
    		while (resultat != tal1 + tal2);
    			System.out.println("Du svarade rätt");
     
    	}
     
    			}

    Oh, it was not that hard. I have made it this far now, but I think it gets a bit tricky now. I would like the program to ask would you like to continue YES / NO. If YES go back to the beginning, if no say Ok (or something)?
    How is that made =/

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner help with a do loop

    Put the code to be repeated inside the do{} while loop. Just before the end of the loop, ask the question and use the response to determine if to exit the loop or repeat the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help with a do loop

    Basically you want to create a boolean variable and set it to false, then wrap your code in a do while loop...

    (in pseudo-code)

     
    do {
     
    do stuff here....
     
    Do you want to continue doing stuff? Yes(true) or No(false)
     
    } while(true)

Similar Threads

  1. Beginner: For loop excercise (stuck)
    By ObedMarsh in forum Loops & Control Statements
    Replies: 4
    Last Post: September 8th, 2012, 07:37 PM
  2. Beginner for loop
    By gerre in forum Loops & Control Statements
    Replies: 11
    Last Post: August 2nd, 2012, 03:43 AM
  3. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  4. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM
  5. [SOLVED] While Loop Help (beginner)
    By Perplexing in forum Loops & Control Statements
    Replies: 4
    Last Post: October 23rd, 2010, 02:00 PM

Tags for this Thread