Results 1 to 2 of 2
  1. #1
    crowledg is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default If statment or not an if statement

    So was just wondering am doing the bug gift option and wondering how should i go about it, do i use if statment

    Java Code:
    import java.util.Scanner;
    public class hardwork {
    
        public static void main(String args[]) {
    
        	Scanner input = new Scanner (System.in);
    
        	int giftpoints[]={2000,1200,700,200,1000};
        	String gifts[]={"i-pad","PS3","bike","i-pod","Laptop"};
        	int customerpoints[]={500,1200,300};
        	String names[]={"James","Joe","Danny"};
        	int pin[]={111,222,333};
        	int choices=0;
        	boolean flag=false;
        	int sub=0, userpin=0;
        	String strlogin, strpin, stroption;
    
        	System.out.print("---- Welcome ----");
        	System.out.println("\n\nPlease choose an option :\n\n1: Login \n2: Exit\n>");
    		strlogin=input.next();
    
    		while(!strlogin.matches("[1-2]")) {
    			System.out.println("\nError! 1 or 2 only");
    			System.out.println("\nPlease choose an option :\n\n1: Login \n2: Exit\n>");
    			strlogin=input.next();
    				} //End while
    choices=Integer.parseInt(strlogin);
    //choices is not valid.
    
        	do {
    
    if (choices == 1) {
    System.out.print("\nPlease enter Pin: ");
    strpin = input.next();
    while(!strpin.matches("\\d+")){
    	System.out.println("Incorrect Pin! ");
    	System.out.print("\nPlease enter Pin: ");
    	strpin = input.next();
    }
    userpin=Integer.parseInt(strpin);
    
    }//end if choice =1
    else{
    	System.out.print("\n\n");
    System.exit(0);
    
    }//end else to exit
    
    for(int i = 0; i < pin.length; i++) {
    if (userpin == pin[i]) {
    flag = true;
    sub = i;
    break;
    }
    }//end for password
    
    if (flag == false) {
    System.out.println("Incorrect pin!");
    }//end if flag is false
    
    } while (flag == false);
    
    stroption="";
    
    while(!stroption.equals("4")){
    
    
    		System.out.println("\nWelcome : " + names[sub]);
        	System.out.println("Points : " + customerpoints[sub]);
        	System.out.println("\n\nPlease choose an option :\n\n1: View Gifts \n2: Buy Gifts\n3: Add Points\n4: Exit\n>");
    		stroption=input.next();
    
    		while (!stroption.matches("[1-4]") ) {
    			System.out.println("\nError! 1 to 4 only");
    			System.out.println("\n\nPlease choose an option :\n\n1: View Gifts \n2: Buy Gifts\n3: Add Points\n4: Exit\n>");
    			stroption=input.next();
    		}//end while error check
    		choices=Integer.parseInt(stroption);
    
    		if(choices ==1){
    
    				System.out.println("\nId \t Gift \t\t Gift Points:");
    
    				for (int i = 0; i < gifts.length; i++) {
    					System.out.println(i+1 + "\t" + gifts[i] + "\t\t\t" + giftpoints[i]);
    
    
    					}//end for
    
    		}//end if
    
    
    		else if(choices ==2){
    			System.out.println("Please enter gift Id");
    
    		}//end else if
    		else if(choices ==3){
    			System.out.println("How many Points do you want to buy? ");
    			int additional_point=input.nextInt();
    			customerpoints[sub] = customerpoints[sub]+additional_point;
    
    		}//end else if
    		else{
    			System.out.print("\n\n");
    			System.exit(0);
    
    		}//end else
    }//end while stroption =4
    
        }//end main method
    
    
    }//end class

  2. #2
    crowledg is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: If statment or not an if statement

    Also do you think i would have been better off declaring the gift ID in an array rather than using the subscript+1 for the ID

Similar Threads

  1. java gui if statment help
    By jumpman8947 in forum New To Java
    Replies: 4
    Last Post: 02-28-2013, 07:12 AM
  2. Need some help on my if statment.
    By ThretZ in forum New To Java
    Replies: 1
    Last Post: 11-06-2012, 05:19 PM
  3. if statment not working good
    By nour in forum New To Java
    Replies: 3
    Last Post: 03-25-2012, 10:29 PM
  4. If statment not working:(
    By Harris68 in forum Networking
    Replies: 6
    Last Post: 02-24-2010, 11:41 PM
  5. statment replay!!!
    By moamen in forum New To Java
    Replies: 4
    Last Post: 09-25-2009, 01:49 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
  •