First I apologize if this is a fairly simple question, but I am a newbie to programming. Basically what I want to do is have two UITextField's where you enter a number into each, then press a button and in a third UITextField the sum of the first two UITextFields is displayed. Is this possible? Basically all I would need to know is how to define the IBAction method of the addition button. Here's what "I'm thinking" but it doesn't work because you can't do math with string values.
-(IBAction)addthefields {
textfield3.text = textfield1.text + textfield2.text;
}
Again sorry if its a simple question but any help is much appreciated
This will probably work. It will add the values u've entered to the two textfields to your third textfield.
-(IBAction)addFields
{
textfield3.text = [NSString stringWithFormat:#"%i",( [textfield1.text intValue] + [textfield2.text intValue] ) ];
{
One thing that u'll have to keep in mind is that you cant calculate with string values, you'll need integers, doubles and so on.
Related
For a small project of mine, I try to build a small Vocabulary trainer. I am using arrays, to store the vocabulary and the translation.
I have a listbox, where I can choose a specific topic which looks like this:
[void] $ListBox.Items.Add('Weekdays')
[void] $ListBox.Items.Add('Months')
[void] $ListBox.Items.Add('Numbers 1-10')
[void] $ListBox.Items.Add('Numbers 10-20')
So as soon as I have my listbox, I can choose from the above 4 topics.
The issue I am fighting with is, how do I tell the code, that for example
the before chosen Weekdays, shall go through the array called $weekdays?
I played around with Hashtables to convert weekdays into $weekdays
$Vocabulary = #{Weekdays = $Weekdays; Months = $Months; Numbers = $Numbers}
But then I face different problems, not being able to dissolve the code on such way, that I get the first word out of the array as below:
$Weekdays = 'Montag','Dienstag','Mittwoch'
$weekdays_Answer = 'Monday','Tuesday','Wednesday'
$Months = 'Januar','Februar','März','April','Mai','Juni'
$Months_Answer = 'January','February','March','April','May','June'
So my question is, what do I have to do, that when I pick 'Weekdays'from the listbox, that with this output, I receive the arrayname '$Weekdays', so I can continue to work from there?
I tried several approaches like:
$Vocabulary[$listbox.SelectedItems]
$test=$listbox.SelectedItems
$Vocabulary['weekdays'][0]
$Vocabularytopic = '$'+$listbox.selecteditem
$($TopicSelection.Values)[$Script:CounterVocabulary]
But so far no success, because I am not sure, how to convert the result from the listbox into an array. The closest I got was with:
$Vocabulary['weekdays'][0]
but here I failed by exchanging 'weekdays' with $listbox.selecteditems. Maybe you are able to figure this one out?
The reason for having two separate arrays is, that I first question the foreign word and then through a click onto a button, show the answer.
Open for any suggestions and other approaches
Many thanx,
Mike
I was able to find a solution, should anyone have similar difficulties.
$vocabulary.($listbox.text)[0]
Gives me the result 'Montag' I was searching for.
I do realize similar questions have been asked before, I looked at them before seeking help. But they were either not in Swift or too complex for me to decipher. My question is different from How do I shuffle an array in Swift? in that I have already done some of the work, like shuffling. The title was actually different from the question asked, which was "How do I randomize or shuffle the elements within an array in Swift?" I am only interested in iterating the resulting array and I couldn't separate that part from the rest of the code in the answers given for a question that encompassed so much more. That said, there are some great suggestions on that page so I think people like me will benefit from having both pages available. Maybe someone can set up a reciprocal link on that page as I have done here.
Admittedly, I am new to Swift and not a seasoned programmer in any language, but please don't assume I am coming here seeking help without trying to figure it out on my own. I am spending many hours learning the fundamentals of all C based languages and reading the Swift literature at developer.apple.com.
So the question will be more obvious, I and attempting to build the card game War. Thus far I have accomplished constructing the (an array) deck of cards and randomized it (shuffled). I am stuck at looping through the resulting array of 52 objects and assigning (moving) them to the two players hands (two new arrays). I'm not sure how much of my code I should display in order to help me but if you need more, I'll gladly provide it. Please note that this is only an exercise, practice for me to learn how to write complex programs, and some code, like the function to randomize, is not mine, I found it right here at stackoverflow. I'd almost prefer if you didn't just hand me the code that will work, I'm not likely going to learn as much that way, but if providing steps in plain English so I can figure out the syntax is too much trouble, so be it, provide an example, I'm sure I'll get plenty of chances to write/use the syntax later.
One more note, I'm only working in a playground at the moment, when and if I can get all the code working, I'll move to the UI stuff.
Thanks in advance, Rick
/* Skipping past everything I did to get here,
the array (shuffledDeck) has 52 shuffled cards (elements) in it.
The array is NSMutableArray and contains strings like
2Hearts, 5Spades, 14Clubs, etc. Each suit has 14 cards.*/
shuffledDeck
// create vars to hold shuffled hands
var playerOneHand = []
var playerTwoHand = []
/* Started a for loop to assign cards to each hand
but don't know which method(s) is/are best to use
to remove the first or last card and alternately
append (move) it to the (hopefully) initialized
variables playerOneHand and PlayerTwoHand.
Optionally, since the cards are already shuffled,
I could just split the deck using the range method,
whichever is easier. I tried and failed at both ways.*/
var i = 0
for dealtCard in shuffledDeck {
}
var shuffledDeck:[String] = ["2Hearts", "5Spades", "14Clubs", "etc"]
//shuffledDeck will of course be your shuffled deck
var playerOneHand:[String] = []
var playerTwoHand:[String] = []
for (index, cardString) in enumerate(shuffledDeck) {
if index % 2 == 0 {
playerOneHand.append(cardString)
}else{
playerTwoHand.append(cardString)
}
}
I’m looping through every item in the shuffledDeck, but with that I use the index of the array to get a number. I use this number to see if that number devided by 2 is equal to 0 (the number is even) or not (uneven) if a number is even, I get the item that is in the array at the given index and add that item to the hand of player one. If the index is uneven I add the item to the second player’s hand. This means the first item goed to player one’s hand, the second item goes to the hand of the second player. the third Item goes back to the first player and so on.
As mentioned by Martin R you can use the range method to assign the first half of the deck to the first player and the second to the second player as follow:
let cards:[String] = ["2♦️","3♦️","4♦️","5♦️","6♦️","7♦️","8♦️","9♦️","T♦️","J♦️","Q♦️","K♦️","A♦️","2♠️","3♠️","4♠️","5♠️","6♠️","7♠️","8♠️","9♠️","T♠️","J♠️","Q♠️","K♠️","A♠️","2♥️","3♥️","4♥️","5♥️","6♥️","7♥️","8♥️","9♥️","T♥️","J♥️","Q♥️","K♥️","A♥️","2♣️","3♣️","4♣️","5♣️","6♣️","7♣️","8♣️","9♣️","T♣️","J♣️","Q♣️","K♣️","A♣️"]
extension Array {
var shuffled:[T] {
var elements = self
for index in 0..<elements.count - 1 {
swap(&elements[index], &elements[Int(arc4random_uniform(UInt32(elements.count - 1 - index))) + index])
}
return elements
}
}
let cardsShuffled = cards.shuffled
let playerOneHand = cardsShuffled[0...25]
let playerTwoHand = cardsShuffled[26...51]
Note: The shuffle extension was created using this answer as reference
I've ran into a weird problem with flash, I have an array of 92 buttons, at first it was all contained in a single array, the buttons up to the first 20th buttons work, the rest don't.
The buttons will take the user to the next scene basically.
So I tried to breakup the array into multiple arrays, so the first array contains the first 20, the second array the 21-40th and so on, the fifth array contains the 81-92 buttons.
The problem now is I will get this error message:
TypeError #1010: A term is undefined and has no properties
and it'll break all the buttons, rendering all the buttons unusable.
Therefore, I commented out the
for (var a=0; a<buttons.length; a++)
{
firstarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
secondarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
thirdarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
fourtharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
//fiftharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
}
in my button spawn function and the buttons from the first to fourth array works flawlessly well except the fifth, which when clicked, nothing happens.
So I tired to create a new function whereby it was only the fiftharray in it and called the new function in the spawner, same error, breaks everything.
Then I thought was there a button naming issue whereby i mistyped something, I took the button names in the fifth array and pasted them into the start of the fourtharray, replacing what was in it plus commenting out the fiftharray from my script.
The once unworkable buttons (81 to 92) worked, but now (61 to 80) didn't.
I tried combining all the arrays using the comarray, but only the first 20 buttons worked.
So I am wondering if is there a fix or something to solve this problem, much help is appreciated!
There is no need to have multiple arrays, remove them. The last array is obviously shorter than the rest and that's messing up your code -> you are pointing to an index that doesn't exist in your last array.
There is actually no need to have an array. You have 92 buttons, that's a nice bunch. Why not to put it to a movieclip instead? What's the need of the array?
Let's assume you select all your buttons and put them inside of movieclip called buttonsClip. Now you can just use this code, without typing all the instance names out to put them to the array (like the tutorial did it... that may work for 8 buttons, but 92... come on :) ):
import flash.display.MovieClip;
import flash.events.MouseEvent;
for(var i:uint=0; i<buttonsClip.numChildren; i++) {
var b:MovieClip = buttonsClip.getChildAt(i) as MovieClip; //Assuming the buttons are movieclips
b.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(e:MouseEvent):void {
trace(e.target.name);
}
I know this Question is currently 8 years old, but maybe this will help another person that is searching for the same thing.
I had pretty much the same problem and in my case it was just that I accidently skipped a number while naming all the Symbols in the libary that I wanted to be in this array. I had 42 Symbols but because of this mistake the function to load the array had 43 and obviously the program was confused about that.
So, I am a basic programmer in flash and this weekend I have to make a small mini game. This is where I get confused...I have 1 movieclip which has 5 labels ( each showing a different shape). I also have a dynamic text field which I have text or (a string) that will need to match the movieclip. Meaning, if the text displays circle, and the shape is circle, if you click the screen you win. if they dont match, you lose. So I am asking this in order to find out, how to create 2 arrays, randomize them then compare the value. I know how to set everything on timers and give scores, I just cant get figure this part out. AS3 and I are having a bad day. Any ideas, even pseudo code helps...or just a flow , something please ! lol thanks in advance
Regarding randomizing an array, have a look at this elaborate article at Activetuts, which specifically aims at Actionscript. It provides documented code with clear illustrations and tips. You could also check out the Fisher-Yates shuffle for some pseudo-code.
I don't quite get your question with regards to comparing the strings.. In AS3, you can use == to see if the strings are equal.
This is/isn't homework...the printing of the list IS homework and that works great, the iscntrl() and Array stuff is 6 weeks from now stuff and giving me grief.
I want to create an array filled with the first 32 TLAs of the Ascii table so that when I print out a column / row chart of Decimal to Ascii code I can use iscntrl() to flag that it's an un-printable character. In its place I want to grab the next TLA in the array and print that instead of the non-graphical character.
I have the iscntrl() working fine. Just can't figure out the array thing. All the examples in the books I have and online want to demo grabbing input from the user and tossing it into the array. I want to give the array a list at the beginning in the code and pull from that.
Can someone either give me a good link for what I need or just tell me how to do the whole process?
I've got 32 three letter items and I need to populate the array and pull them out via a for loop.
Thanks.
You can declare an array like this, and pre-fill its values:
const char *ControlCharacterNames[] = {
"NUL",
"SOH",
"STX",
"ETX",
// etc
};
Then, you can access ControlCharacterNames as an array in your code.
http://publications.gbdirect.co.uk/c_book/chapter6/initialization.html, chapter "6.7.2. More initialization".
Long story short, you probably need something like
char *TLAs[] = { "TL1", "TL2", "TL3", "FYI", "WTH", /* ...and so on...*/ };
and then pull the one you need using it's index
printf(TLAs[3]); // print "FYI", the 4th TLA
Hope I understood your question right.