Esempio n. 1
0
func main() {
	// This is the variable declaration for the temperature that you type in.
	var temperature int

	// this is the varaible decleration for the temperature in London today
	var temperatureInLondon int
	// You need to add 5 more variabels here. One for each city that you picked

	temperatureInLondon = 11 // this needs to be updated on the day!

	// tell the user what the program does
	fmt.Println("The worldtemperature program tells you which cities are ")
	fmt.Println("hotter or colder than where you live.")

	fmt.Println("Enter the temperature in degrees Celsius today: ")
	// This is the using part of the input pattern.
	// Here you are assigning the number that the user types in to the
	// temperature variable.
	temperature = simpleio.ReadNumberFromKeyboard()

	// This is the selection pattern
	// If the temperature you typed in is less than the temperature in London
	// then the program will print out
	//
	// Colder than London, which is 11 degrees Celsius today.
	//
	// otherwise this if test is skipped over and nothing is printed out.
	if temperature < temperatureInLondon {
		fmt.Print("Colder than London, which is ")
		fmt.Print(temperatureInLondon)
		fmt.Println(" degrees Celsius today.")
	}
	// if the temperature you typed in is the same as the temperature in London
	// then the program will print out
	//
	// Exactly the same as London.
	//
	if temperature == temperatureInLondon {
		fmt.Println("Exactly the same as London.")
	}
	// Just like the first if test, except this one only works if the
	// temperature you typed in is greater than the temperature in London.
	if temperature > temperatureInLondon {
		fmt.Print("Hotter than London, which is ")
		fmt.Print(temperatureInLondon)
		fmt.Println(" degrees Celsius today.")
	}
	// Add the if statements for the 5 cities that you picked here
}
func main() {
	// Declare a variable called myName.
	var myName string
	// Declare a variable called myAge
	var myAge int
	// Now add a variable for your friends name here.
	var friendsName string
	// Now add a variable for your friends age here.
	var friendsAge int

	fmt.Print("Please tell me your name: ")
	// now you have to read a value for your name from the keyboard
	// and assign it to the myName variable
	// You need to use the import pattern for this
	myName = simpleio.ReadStringFromKeyboard()

	fmt.Print("Please tell me how old you are: ")
	// now you have to read a value for your age from the keyboard
	// and assign it to the myAge variable
	myAge = simpleio.ReadNumberFromKeyboard()

	// now you have to read your friends name and age from the keyboard
	// you will need extra print statements to tell the user when to enter
	// your friends name and age
	fmt.Print("Please tell me your friends name: ")
	friendsName = simpleio.ReadStringFromKeyboard()
	fmt.Print("Please tell me how old they are: ")
	friendsAge = simpleio.ReadNumberFromKeyboard()

	fmt.Print("Hello ")
	// Print out the value of myName.
	// You do not need inverted commas because you want to use
	// the value of the variable myName
	fmt.Println(myName)
	fmt.Print("You are ")
	// now print out your age
	fmt.Print(myAge)
	fmt.Println(" years old.")
	// Now print out your friends name and age here
	fmt.Print("Hello ")
	fmt.Println(friendsName)
	fmt.Print("You are ")
	fmt.Print(friendsAge)
	fmt.Println(" years old.")

	fmt.Print("Our ages multiplied together is ")
	// now multiply your ages together and print out the answer
	// Use a new veriable for this. It is ok to decalre a new variable here.
	// It doesn't have to be at the top, so long as it is decalred before
	// we first use the variable.
	var ages int
	ages = myAge * friendsAge
	// now print the answer
	fmt.Println(ages)

	fmt.Print("The difference between our ages is ")
	// now print out the difference between your ages
	// We don't have to use a new variable do do this. We can work the anser
	// as we go. Like this, which prints the answer of the sum.
	fmt.Println(myAge - friendsAge)

	fmt.Print("The sum of our ages is ")
	// now print out the sum of your ages
	// We'll use the quick way again
	fmt.Println(myAge + friendsAge)
}
func main() {
	// declare some varaibels.
	// a and b are the variabels we are going to use to hold the question in
	var a int
	var b int
	// answer is the variable we are going to use for your answer
	var answer int

	// Tell the user what the program does.
	fmt.Println("The timesquestion shows you how to use if and else.")
	fmt.Println("Can you remember your times tables?")
	fmt.Println("")

	// This new!
	// a and b are both set to a random number.
	// The program will pick a number between 1 and 12, including 1 and 12
	// and assign it to the variable a
	// If you run the program again, the value of a will be different because
	// the computer will have picked a different number.
	a = random.GetRandomNumberInRange(1, 12)
	// b is also picked randomly!
	b = random.GetRandomNumberInRange(1, 12)

	// Now we want to print the question
	// What is a * b?
	// but with the values of a and b
	fmt.Print("What is ")
	fmt.Print(a)
	fmt.Print(" * ")
	fmt.Print(b)
	fmt.Println("?")

	// now you have to read the users answer from the keyboard
	// using the input using pattern
	answer = simpleio.ReadNumberFromKeyboard()

	// Now you have to fill in the rest!
	// Use the selection if else if pattern...

	// if the users gues is exacly a*b then you have to print
	// congratulations
	// else if the users guess is to large print out
	// Sorry you guess was to big and then print out the correct answer
	// else the users guess must have been too small so print out
	// Sorry your guess was too small and then print out the correct
	// answer
	if answer == a*b {
		fmt.Println("Congratulations! You are correct! ")
	} else if answer > a*b {
		fmt.Println("Sorry, your guess was to big.")
		fmt.Print("The correct answer is ")
		fmt.Print(a)
		fmt.Print(" * ")
		fmt.Print(b)
		fmt.Print(" = ")
		fmt.Println(a * b)
	} else {
		fmt.Println("Sorry your guess was to small.")
		fmt.Print("The correct answer is ")
		fmt.Print(a)
		fmt.Print(" * ")
		fmt.Print(b)
		fmt.Print(" = ")
		fmt.Println(a * b)
	}
	fmt.Println("Run the program again to try another question.")
}
func main() {
	// declare some varaibels.
	// a and b are the variabels we are going to use to hold the question in
	var a int
	var b int
	// answer is the variable we are going to use for your answer
	var answer int

	// Tell the user what the program does.
	fmt.Println("The timesquiz shows you how to use loops.")
	fmt.Println("Can you remember your times tables?")
	fmt.Println("")

	// use random numbers again!
	// The program will pick a number between 1 and 12, including 1 and 12
	// and assign it to the variables a and b
	// If you run the program again, the value of a will be different because
	// the computer will have picked a different number.
	a = random.GetRandomNumberInRange(1, 12)
	b = random.GetRandomNumberInRange(1, 12)

	// Now we want to print the question
	// What is a * b?
	// but with the values of a and b
	fmt.Print("What is ")
	fmt.Print(a)
	fmt.Print(" * ")
	fmt.Print(b)
	fmt.Println("?")

	// now you have to read the users answer from the keyboard
	// using the input using pattern
	answer = simpleio.ReadNumberFromKeyboard()

	// now you need a loop!
	// You have to keep looping around while the users answer is
	// not equal to a * b
	// You need to use the loop pattern for this
	for answer != a*b {
		// your loop statement block starts here!
		// Now you need to use an if statement
		// if the users answer was to large print out
		// Sorry, your gueds was to big
		// otherwise the users guess is too small so print out
		// Sorry your guess was to small
		if answer > a*b {
			fmt.Println("Sorry, your guess was to big.")
		} else {
			fmt.Println("Sorry your guess was to small.")
		}
		// now tell the user to try again
		fmt.Println("Try again ")
		// now you have to use the input pattern again to assign
		// the users next guess to the answer variable
		answer = simpleio.ReadNumberFromKeyboard()
	}
	// Now we are outside the loop! So the user must have guessed
	// correctly. Now you need to print Congratulations.

	fmt.Println("Congratulations! You are correct.")
}