Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Char input problems (https://www.askmehelpdesk.com/showthread.php?t=707558)

  • Oct 8, 2012, 01:51 AM
    chialin
    Char input problems
    Why when I try to compile , it says "variable ch is already defined in method main(java.lang.String[])"? I have probkems getting char inputs.

    import java.util.Scanner;



    public class Tolak{
    public static void main(String [] args) {
    int correct=0;
    char ch;

    Scanner input= new Scanner(System.in);

    System.out.print("You have RM10. After buying a drink that costs RM4.50, how much you left?\n");
    System.out.print("A:RM6.50\n");
    System.out.print("B:RM5.50\n");
    char ch=(sc.next).charAt(0);
    System.out.println(ch);
    if (ch=='a');
    correct += 1;

    System.out.print("Marks you get = "+ correct);
    }


    }
  • Jul 7, 2013, 09:52 PM
    rameshspry
    the ch is already declared

    and the reading input syntax is also wrongly arranged

    it should be like this...


    import java.util.Scanner;



    public class Tolak{
    public static void main(String [] args) {
    int correct=0;
    char ch;

    Scanner input= new Scanner(System.in);

    System.out.print("You have RM10. After buying a drink that costs RM4.50, how much you left?\n");
    System.out.print("A:RM6.50\n");
    System.out.print("B:RM5.50\n");
    ch=input.next().charAt(0);;
    System.out.println(ch);
    if (ch=='a');
    correct += 1;

    System.out.print("Marks you get = "+ correct);
    }


    }

  • All times are GMT -7. The time now is 04:10 PM.