This site requires JavaScript, please enable it in your browser!
Greenfoot back
h123n
h123n wrote ...

2015/5/28

Wait for specific keys to be pressed.

h123n h123n

2015/5/28

#
Hi! I am currently trying to code a scenario where there is an AI who asks basic questions that have a positive response or a negative response (Yes/no) the problem is, the code runs straight through the method that is used to see if it negative or positive. I want it to wait for when you answer the question and then do different methods depending on which one was picked. Here is my code:
import greenfoot.*;

/**
 * Write a description of class AI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AI extends Actor
{
    String Opensentance;
    String greeting;
    String opsgreeting;
    String condquestion;
    int opening = 1;
    int interest = Greenfoot.getRandomNumber(100);
    int Userhappiness = 50;
    /**
     * Act - do whatever the AI wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (opening == 1)
        {
        System.out.println("A = positive response. s = negative response");
        }
        if (interest < 60)
        {
            greeting = "Hi";
        }
        else
        {
            greeting = "Wussup";
        }
        opsgreeting = "Goodbye";
        condquestion = "are you okay?";
        if (opening == 1)
        {
        opensentance();
        }
    }
    public void opensentance()
    {
       System.out.println(greeting + " " + condquestion);
       opening = 2;
       condresponse();
    }
    public void condresponse()
    {
        if (Greenfoot.isKeyDown("a"))
        {
            Userhappiness +=50;
            System.out.println("positive response.");
        }
        else if (Greenfoot.isKeyDown("d"))
        {
            Userhappiness -=50;
            act();
        }
    }
}
Any and all help is appreciated, thanks for your time.
h123n h123n

2015/5/28

#
Also it prints out this into the console: A = positive response. s = negative response Wussup are you okay?
Super_Hippo Super_Hippo

2015/5/28

#
Instead of 'System.out.println(...)', you could use 'Greenfoot.ask(...)' to ask the user for input. It could be like that:
String answer = Greenfoot.ask("How are you?");
if ("a".equals(answer))
{
    Userhappiness +=50;
}
//...
I never used this 'ask' method, but I think it works like this.
h123n h123n

2015/5/28

#
THANK YOU SO MUCH THIS IS EVEN BETTER THAN WHAT I WANTED.
You need to login to post a reply.