Results 1 to 4 of 4
  1. #1
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default Problem with taking input and printing it.

    Hello, I have a problem. I'm trying to make the application ask me questions and if i answer them right, they print out a "Correct..." message, and then asks another question. But the "Correct"-message is only printed every other time, why is that?

    Code
    PHP Code:
    import java.util.Scanner;
    public class pinki1{
    
    
    	public static void main(String[] args) {
    	
    		Scanner scan = new Scanner(System.in);
    		//asks for name
    		System.out.println("What is your name?");
    		String nick = scan.nextLine();
    		//check if name is correct and prints its is not printed
    		String nick2 = "equal";
    	if (nick.equals(nick2))
    			System.out.println("Correct " + nick + " is your name!");
    	
    	//asks for age
    	System.out.println("How old are you?");
    	String alder2 = "19";
    	String alder = scan.nextLine();
    	
    	//checks if age is correct and prints this is printed
    	if(alder.equals(alder2))
    		System.out.println("Correct! You are " + alder + " years old!");
    
    	
    //asks for city
    	System.out.println("Which city do you live in?");
    	String stad2 = "MyCity";
    	String stad = scan.nextLine();
    	
    	//checks if city is correct and prints this doesnt print
    	if (stad.equals(stad2))
    		System.out.println("Correct! You live in " + stad);
    	
    	
    	//asks for street name
    	System.out.println("What is your streets name?");
    	String gata2 = "MyStreetsName"; 
    	String gata = scan.nextLine();
    	
    	//checks if street name i correct and prints this prints
    	if (gata.equals(gata2))
    		System.out.println("Correct! You live on " + gata);
    				
    	}
    
    }


    Problem 2:

    I wonder if there is any method so that if i answer wrong, it will ask the same question again.

    Thanks for the help!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    But the "Correct"-message is only printed every other time, why is that?

    I don't see that when I run the program.

    I wonder if there is any method so that if i answer wrong, it will ask the same question again.

    If the intention is to repeat a question every time a question is answered incorrectly, you could use a (do-?)while loop. (In that case the response is unnecessary.) If you only want to repeat the question once maybe an if statement would suffice.

    Either way you would be much better off if you wrote and called a small method to do the question asking/checking:

    Java Code:
    // in main()
    String ans= "nick";
    if(poseQuestion("What is your name?", ans)) {
        System.out.println("Correct " + ans + " is your name!");
    }
    
        /**
        * Poses a question and returns true iff the correct answer is given.
        * todo: add loop to allow for multiple attempts at answering
        */
    static boolean poseQuestion(String ques, String ans) {
        //...
    }
    Last edited by pbrockway2; 11-11-2010 at 08:17 PM.

  3. #3
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    I've found the problem with the print...

    If the answer is all in lowercase, and I answer in uppcase or in mixed upper&lowercase it will not print. How do i fix this problem with the upper and lower case?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Taking args consisting of a scanner input
    By Implode in forum New To Java
    Replies: 1
    Last Post: 09-27-2009, 10:16 PM
  2. printing number of pos/neg numbers input?
    By shroomiin in forum New To Java
    Replies: 12
    Last Post: 09-25-2009, 03:15 AM
  3. Taking input from Properties File
    By bugger in forum New To Java
    Replies: 4
    Last Post: 05-07-2008, 10:30 AM
  4. Problem after Printing GUI.
    By coldblood22 in forum AWT / Swing
    Replies: 1
    Last Post: 04-05-2008, 03:43 PM
  5. Taking input from console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:47 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
  •