Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Application Switches demonstrates the use of the Switch

  1. #1
    BLJava is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default Application Switches demonstrates the use of the Switch

    There is no prompt for input in this application...where do I put the prompt?

    // Application Switches demonstrates the use of the Switch
    // statement.

    import java.util.Scanner;
    public class Switches
    {
    public static void main(String[] args)
    {
    char code;
    int answer;
    int one;
    int two;
    String inputString;
    boolean quit = false;

    Scanner inLine = new Scanner(System.in);
    do
    {
    inputString = inLine.nextLine();
    code = inputString.charAt(0);
    if (code != 'Q')
    {
    inputString = inputString.substring(1, inputString.length());
    Scanner string = new Scanner(inputString);
    one = string.nextInt();
    two = string.nextInt();
    switch (code)
    {
    case 'A' : answer = (one + two);
    System.out.println(one + " + " + two
    + " is " + answer);
    break;
    case 'S' : answer = (one - two);
    System.out.println(one + " - " + two
    + " is " + answer);
    break;
    case 'M' : answer = (one * two);
    System.out.println(one + " * " + two
    + " is " + answer);
    break;
    case 'D' : answer = (one / two);
    System.out.println(one + " / " + two
    + " is " + answer);
    break;
    }
    }
    else
    quit = true;
    } while (!quit);
    }
    }

  2. #2
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Java Code:
    do 
    {
    System.out.println("enter the string");
    inputString = inLine.nextLine();
    code = inputString.charAt(0);
    if (code != 'Q')
    {
    Java Code:
    Scanner string = new Scanner(inputString);
    System.out.println("enter ");
    one = string.nextInt();
    System.out.println("enter ");
    two = string.nextInt();
    switch (code)
    Last edited by fakepics500; 07-24-2011 at 08:12 PM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by fakepics500 View Post
    Java Code:
    do 
    {
    System.out.println("enter the string");
    inputString = inLine.nextLine();
    code = inputString.charAt(0);
    if (code != 'Q')
    {
    Cute if the user just presses the enter key; please stop posting sloppy code like that; it doesn't help anyone.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Using switches...
    By besweeet in forum New To Java
    Replies: 11
    Last Post: 03-04-2010, 04:48 PM
  2. help with switches
    By spots of fire in forum New To Java
    Replies: 1
    Last Post: 01-23-2010, 09:02 PM
  3. Demonstrates printing images
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:42 PM
  4. Demonstrates how to draw vertical text in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:26 PM
  5. Help with switches
    By Daniel in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:37 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
  •