Results 1 to 4 of 4
  1. #1
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Using Scanner in a selfcheck exercise

    Hi I got a selfcheck exercise, where I'm supposed to make a program that prints the following(example) "Hello there. 1+2 is 3", where I have to be able to type in values as if it was a+b = c.

    Java Code:
    import java.util.*;
    public class SelfCheck_3_16 {
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
            
             int a = console.nextInt();
            int b = console.nextInt();
            int c = a+b;
            
            System.out.println("Hello there. " +a +" + " +b +" is " + c);
            
           
        }
    }
    This is working, but how do I add text before the output that comes when I have entered a and b's values? I want it to show the line "Hello there. a + b is c, choose a value for a". and then same line just with b instead of a, for when I have to give b a value.

    Hope I make any sense :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using Scanner in a selfcheck exercise

    Consider placing System.out.print(...) or System.out.println(...) statements before the statements where you obtain your nextInt() from the Scanner object.

  3. #3
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Re: Using Scanner in a selfcheck exercise

    I see, thank you for helping me!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using Scanner in a selfcheck exercise

    You're welcome and best of luck!

Similar Threads

  1. Need Serious Help on Odometer exercise
    By gnng in forum New To Java
    Replies: 16
    Last Post: 03-20-2011, 06:58 PM
  2. Have I done this exercise right?
    By ccie007 in forum New To Java
    Replies: 7
    Last Post: 09-28-2010, 05:54 PM
  3. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-13-2010, 11:13 PM
  4. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 PM
  5. help with exercise
    By e_as're in forum New To Java
    Replies: 3
    Last Post: 09-25-2007, 10:14 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
  •