CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Mar 2005

    I don't know what is wrong with my program

    I got this program using nested ifs and it compiles fine but gives me the wrong outcome. The way it is now, when I run the code and the first question shows "Enter the number of years you have been playing, and I enter 10, and then I hit the Enter key and then the next question comes up, but the answer comes up too.

    So on my screen I am looking at this:

    Enter the number of years you have been playing: 10
    Enter in the kind of guitar you play: have to have been playing for 10 years.

    So you see something is wrong somewhere and I suspect it is in the nested ifs.
    import java.util.Scanner;
    public class NestedIfStatement
    	public static void main(String[] args)
    		int numberOfYearsPlaying = 10;
    		String guitarPrerequisite = "Fender";
    		int userNumberOfYearsPlayed;
    		String userGuitarPlayed;
    		Scanner keyboard = new Scanner(;
    		System.out.print("Enter the number of years you've been playing: ");
    		userNumberOfYearsPlayed = keyboard.nextInt();
    		System.out.print("Enter in the kind of guitar you play: ");
    		userGuitarPlayed = keyboard.nextLine();
    		if(userNumberOfYearsPlayed >= numberOfYearsPlaying)
    			if(userGuitarPlayed == guitarPrerequisite)
    				System.out.println("Great! You are hired!");
    				System.out.println(" have to have been playing for 10 years");
    Last edited by 2kaud; May 25th, 2023 at 10:24 AM. Reason: Correct code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width