英文:
Go Program Not Displaying Desired Result of Sliced Int assigned to variable
问题
我的程序在printRecipeOfTheDay函数上出现了问题。例如,假设程序随机选择了0,并将其赋值给我创建的Monday变量,当我将"recipe1"传递给函数"printRecipeOfTheDay"时,我得不到任何输出或者得到一个空值。你有什么想法,我可能搞错了什么吗?
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
完整的程序如下:
package main
import (
"fmt"
"math/rand"
"time"
)
// Recipe的结构体定义如下
type Recipe struct {
name string
prepTime int
cookTime int
Ingredients []string // 这是一个可以接受多个元素的切片
ID int
Yield int
}
// 打印Recipe的函数
func printRecipe(recipe Recipe) {
fmt.Printf("Recipe Name : %s\n", recipe.name)
fmt.Printf("Prep Time : %d\n", recipe.prepTime)
fmt.Printf("Cook Time : %d\n", recipe.cookTime)
fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
fmt.Printf("Recipe ID : %d\n", recipe.ID)
}
// 返回总时间,即烹饪时间和准备时间之和
func totalTime(recipe Recipe) {
fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
}
func main() {
var recipe1 Recipe // 声明类型为Recipe的recipe1变量
var recipe2 Recipe
var recipe3 Recipe
// 为recipe选择一个随机数
r := rand.New(rand.NewSource(time.Now().UnixNano()))
i := r.Perm(5)
fmt.Printf("%v\n", i)
fmt.Printf("%d\n", i[0])
// 将Perm返回的int切片分配给一周中的每一天
var monday = i[0]
var tuesday = i[1]
var wednesday = i[2]
var thursday = i[3]
var friday = i[4]
// 测试打印分配给每一天的变量
fmt.Printf("This is for the day Monday %d\n", monday)
fmt.Printf("%d\n", tuesday)
fmt.Printf("%d\n", wednesday)
fmt.Printf("%d\n", thursday)
fmt.Printf("%d\n", friday)
// 处理星期一的食谱
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe2)
} else if monday == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe3)
} else if monday == 3 {
fmt.Println(3)
}
/* recipe1的规格 */
recipe1.name = "BBQ Pulled Chicken"
recipe1.prepTime = 25
recipe1.cookTime = 5
recipe1.Ingredients = append(
recipe1.Ingredients,
"1 8-ounce can reduced-sodium tomato sauce",
)
recipe1.Ingredients = append(
recipe1.Ingredients,
"1/2 medium onion (grated),",
)
recipe1.ID = 1
recipe1.Yield = 8
/* Recipe 2的规格 */
recipe2.name = "Steak Tacos with Pineapple"
recipe2.prepTime = 45
recipe2.cookTime = 45
recipe2.Ingredients = append(
recipe2.Ingredients,
"3 tablespoons soy sauce,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated garlic,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated peeled fresh ginger,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 1/2 pounds skirt steak, cut into 5-inch lengths,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Salt",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Pepper",
)
recipe2.ID = 2
recipe2.Yield = 4
recipe3.name = "Simple Lemon Herb Chicken"
recipe3.prepTime = 10
recipe3.cookTime = 15
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 skinless boneless chicken breast halves,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 Lemon,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"Salt and Pepper to taste,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 tablespoon olive oil,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 sprigs fresh parsley (for garnish),",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 pinch dried oregano,",
)
recipe3.ID = 3
recipe3.Yield = 2
// 调用printRecipe函数打印recipe
printRecipe(recipe1)
totalTime(recipe1)
printRecipe(recipe2)
totalTime(recipe2)
printRecipe(recipe3)
totalTime(recipe3)
}
// 打印当天食谱的函数,用于午餐或晚餐
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
英文:
My program is having issues with the printRecipeOfTheDay function. Say for example 0 is randomly selected by the program and gets assigned to the Monday variable I created, when I pass "recipe1" to the function "printRecipeOfTheDay" I get no output or a null value. Any idea on what I may have messed up?
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
The entire program is below:
package main
import (
"fmt"
"math/rand"
"time"
)
//Struct for Recipe below
type Recipe struct { //Struct for recipe information
name string
prepTime int
cookTime int
Ingredients []string //this is now a slice that will accept multiple elements
ID int
Yield int
}
//function to print Recipe
func printRecipe(recipe Recipe) {
fmt.Printf("Recipe Name : %s\n", recipe.name)
fmt.Printf("Prep Time : %d\n", recipe.prepTime)
fmt.Printf("Cook Time : %d\n", recipe.cookTime)
fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
fmt.Printf("Recipe ID : %d\n", recipe.ID)
}
//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
}
func main() {
var recipe1 Recipe //Declare recipe1 of Type Recipe
var recipe2 Recipe
var recipe3 Recipe
//choose random number for recipe
r := rand.New(rand.NewSource(time.Now().UnixNano()))
i := r.Perm(5)
fmt.Printf("%v\n", i)
fmt.Printf("%d\n", i[0])
//assign slices of int from Perm to variables assigned days of the week
var monday = i[0]
var tuesday = i[1]
var wednesday = i[2]
var thursday = i[3]
var friday = i[4]
//testing printing of variables assigned to days
fmt.Printf("This is for the day Monday %d\n", monday)
fmt.Printf("%d\n", tuesday)
fmt.Printf("%d\n", wednesday)
fmt.Printf("%d\n", thursday)
fmt.Printf("%d\n", friday)
//logic for Mondays Recipe
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe2)
} else if monday == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe3)
} else if monday == 3 {
fmt.Println(3)
}
/* recipe1 specifications */
recipe1.name = "BBQ Pulled Chicken"
recipe1.prepTime = 25
recipe1.cookTime = 5
recipe1.Ingredients = append(
recipe1.Ingredients,
"1 8-ounce can reduced-sodium tomato sauce",
)
recipe1.Ingredients = append(
recipe1.Ingredients,
"1/2 medium onion (grated),",
)
recipe1.ID = 1
recipe1.Yield = 8
/* Recipe 2 specifications */
recipe2.name = "Steak Tacos with Pineapple"
recipe2.prepTime = 45
recipe2.cookTime = 45
recipe2.Ingredients = append(
recipe2.Ingredients,
"3 tablespoons soy sauce,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated garlic,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated peeled fresh ginger,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 1/2 pounds skirt steak, cut into 5-inch lengths,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Salt",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Pepper",
)
recipe2.ID = 2
recipe2.Yield = 4
recipe3.name = "Simple Lemon Herb Chicken"
recipe3.prepTime = 10
recipe3.cookTime = 15
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 skinless boneless chicken breast halves,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 Lemon,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"Salt and Pepper to taste,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 tablespoon olive oil,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 sprigs fresh parsley (for garnish),",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 pinch dried oregano,",
)
recipe3.ID = 3
recipe3.Yield = 2
//call to printRecipe function below
printRecipe(recipe1)
totalTime(recipe1)
printRecipe(recipe2)
totalTime(recipe2)
printRecipe(recipe3)
totalTime(recipe3)
}
//function to print the winner for recipe of the day to use
//for either lunch or dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
答案1
得分: 3
你的语句顺序有误。在尝试打印食谱时,你已经声明了它,但没有进行初始化,所以它应该是空的。
我建议你创建一个initRecipes
方法,返回一个[]Recipe
,并使用复合字面量语法进行初始化,而不是反复调用append
方法。
以下是翻译好的代码:
package main
import (
"fmt"
"math/rand"
"time"
)
//Struct for Recipe below
type Recipe struct { //Struct for recipe information
name string
prepTime int
cookTime int
Ingredients []string //this is now a slice that will accept multiple elements
ID int
Yield int
}
//function to print Recipe
func printRecipe(recipe Recipe) {
fmt.Printf("食谱名称:%s\n", recipe.name)
fmt.Printf("准备时间:%d\n", recipe.prepTime)
fmt.Printf("烹饪时间:%d\n", recipe.cookTime)
fmt.Printf("食材:%s\n", recipe.Ingredients)
fmt.Printf("食谱ID:%d\n", recipe.ID)
}
//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
fmt.Printf("该食谱的总时间为:%d\n", recipe.cookTime+recipe.prepTime)
}
func main() {
var recipe1 Recipe //Declare recipe1 of Type Recipe
var recipe2 Recipe
var recipe3 Recipe
/* recipe1 specifications */
recipe1.name = "BBQ Pulled Chicken"
recipe1.prepTime = 25
recipe1.cookTime = 5
recipe1.Ingredients = append(
recipe1.Ingredients,
"1 8-ounce can reduced-sodium tomato sauce",
)
recipe1.Ingredients = append(
recipe1.Ingredients,
"1/2 medium onion (grated),",
)
recipe1.ID = 1
recipe1.Yield = 8
/* Recipe 2 specifications */
recipe2.name = "Steak Tacos with Pineapple"
recipe2.prepTime = 45
recipe2.cookTime = 45
recipe2.Ingredients = append(
recipe2.Ingredients,
"3 tablespoons soy sauce,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated garlic,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated peeled fresh ginger,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 1/2 pounds skirt steak, cut into 5-inch lengths,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Salt",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Pepper",
)
recipe2.ID = 2
recipe2.Yield = 4
recipe3.name = "Simple Lemon Herb Chicken"
recipe3.prepTime = 10
recipe3.cookTime = 15
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 skinless boneless chicken breast halves,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 Lemon,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"Salt and Pepper to taste,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 tablespoon olive oil,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 sprigs fresh parsley (for garnish),",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 pinch dried oregano,",
)
recipe3.ID = 3
recipe3.Yield = 2
//choose random number for recipe
r := rand.New(rand.NewSource(time.Now().UnixNano()))
i := r.Perm(5)
fmt.Printf("%v\n", i)
fmt.Printf("%d\n", i[0])
//assign slices of int from Perm to variables assigned days of the week
var monday = i[0]
var tuesday = i[1]
var wednesday = i[2]
var thursday = i[3]
var friday = i[4]
//testing printing of variables assigned to days
fmt.Printf("这是星期一的食谱:%d\n", monday)
fmt.Printf("%d\n", tuesday)
fmt.Printf("%d\n", wednesday)
fmt.Printf("%d\n", thursday)
fmt.Printf("%d\n", friday)
//logic for Mondays Recipe
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe2)
} else if monday == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe3)
} else if monday == 3 {
fmt.Println(3)
}
//call to printRecipe function below
printRecipe(recipe1)
totalTime(recipe1)
printRecipe(recipe2)
totalTime(recipe2)
printRecipe(recipe3)
totalTime(recipe3)
}
//function to print the winner for recipe of the day to use
//for either lunch or dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("今天的食谱是:%s\n", recipe.name)
}
希望对你有帮助!如果有任何问题,请随时问我。
英文:
You have your statements in the wrong order. At the point when you try to print the recipe you've declared it but you've done no initialization so it should be null.
package main
import (
"fmt"
"math/rand"
"time"
)
//Struct for Recipe below
type Recipe struct { //Struct for recipe information
name string
prepTime int
cookTime int
Ingredients []string //this is now a slice that will accept multiple elements
ID int
Yield int
}
//function to print Recipe
func printRecipe(recipe Recipe) {
fmt.Printf("Recipe Name : %s\n", recipe.name)
fmt.Printf("Prep Time : %d\n", recipe.prepTime)
fmt.Printf("Cook Time : %d\n", recipe.cookTime)
fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
fmt.Printf("Recipe ID : %d\n", recipe.ID)
}
//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
}
func main() {
var recipe1 Recipe //Declare recipe1 of Type Recipe
var recipe2 Recipe
var recipe3 Recipe
/* recipe1 specifications */
recipe1.name = "BBQ Pulled Chicken"
recipe1.prepTime = 25
recipe1.cookTime = 5
recipe1.Ingredients = append(
recipe1.Ingredients,
"1 8-ounce can reduced-sodium tomato sauce",
)
recipe1.Ingredients = append(
recipe1.Ingredients,
"1/2 medium onion (grated),",
)
recipe1.ID = 1
recipe1.Yield = 8
/* Recipe 2 specifications */
recipe2.name = "Steak Tacos with Pineapple"
recipe2.prepTime = 45
recipe2.cookTime = 45
recipe2.Ingredients = append(
recipe2.Ingredients,
"3 tablespoons soy sauce,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated garlic,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated peeled fresh ginger,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 1/2 pounds skirt steak, cut into 5-inch lengths,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Salt",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Pepper",
)
recipe2.ID = 2
recipe2.Yield = 4
recipe3.name = "Simple Lemon Herb Chicken"
recipe3.prepTime = 10
recipe3.cookTime = 15
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 skinless boneless chicken breast halves,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 Lemon,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"Salt and Pepper to taste,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 tablespoon olive oil,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 sprigs fresh parsley (for garnish),",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 pinch dried oregano,",
)
recipe3.ID = 3
recipe3.Yield = 2
//choose random number for recipe
r := rand.New(rand.NewSource(time.Now().UnixNano()))
i := r.Perm(5)
fmt.Printf("%v\n", i)
fmt.Printf("%d\n", i[0])
//assign slices of int from Perm to variables assigned days of the week
var monday = i[0]
var tuesday = i[1]
var wednesday = i[2]
var thursday = i[3]
var friday = i[4]
//testing printing of variables assigned to days
fmt.Printf("This is for the day Monday %d\n", monday)
fmt.Printf("%d\n", tuesday)
fmt.Printf("%d\n", wednesday)
fmt.Printf("%d\n", thursday)
fmt.Printf("%d\n", friday)
//logic for Mondays Recipe
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe2)
} else if monday == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe3)
} else if monday == 3 {
fmt.Println(3)
}
//call to printRecipe function below
printRecipe(recipe1)
totalTime(recipe1)
printRecipe(recipe2)
totalTime(recipe2)
printRecipe(recipe3)
totalTime(recipe3)
}
//function to print the winner for recipe of the day to use
//for either lunch or dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
I just did a quick cut and paste there cause I'm rather busy at the moment. I may edit this later, it would just be for style and readability. I recommend making an initRecipes
method and having it return a []Recipe
and using composite literal syntax to do your initialization instead of calling append over and over again.
答案2
得分: 0
感谢您的建议,我已经修复了我的代码,如下所示。它按预期工作。现在我只需要进一步整理它。
package main
import (
"fmt"
"math/rand"
"time"
)
//Struct for Recipe below
type Recipe struct { //Struct for recipe information
name string
prepTime int
cookTime int
Ingredients []string //this is now a slice that will accept multiple elements
ID int
Yield int
}
//main method
func main() {
//5 variables below for 5 recipes for Monday-Friday
var recipe1 Recipe //Declare recipe1 of Type Recipe
var recipe2 Recipe
var recipe3 Recipe
var recipe4 Recipe
var recipe5 Recipe
//choose random number for recipe
r := rand.New(rand.NewSource(time.Now().UnixNano()))
i := r.Perm(5)
fmt.Printf("%v\n", i)
fmt.Printf("%d\n", i[0])
fmt.Printf("%d\n", i[1])
//assign slices of int from Perm to variables assigned days of the week
var monday = i[0]
var tuesday = i[1]
var wednesday = i[2]
var thursday = i[3]
var friday = i[4]
//testing printing of variables assigned to days
fmt.Printf("This is for the day Monday %d\n", monday)
fmt.Printf("This is for the day Tuesday %d\n", tuesday)
fmt.Printf("This is for the day Wednesday %d\n", wednesday)
fmt.Printf("This is for the day Thursday %d\n", thursday)
fmt.Printf("This is for the day Friday %d\n", friday)
/* recipe1 specifications */
recipe1.name = "BBQ Pulled Chicken"
recipe1.prepTime = 25
recipe1.cookTime = 5
recipe1.Ingredients = append(
recipe1.Ingredients,
"1 8-ounce can reduced-sodium tomato sauce",
)
recipe1.Ingredients = append(
recipe1.Ingredients,
"1/2 medium onion (grated),",
)
recipe1.ID = 1
recipe1.Yield = 8
/* Recipe 2 specifications */
recipe2.name = "Steak Tacos with Pineapple"
recipe2.prepTime = 45
recipe2.cookTime = 45
recipe2.Ingredients = append(
recipe2.Ingredients,
"3 tablespoons soy sauce,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated garlic,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated peeled fresh ginger,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 1/2 pounds skirt steak, cut into 5-inch lengths,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Salt",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Pepper",
)
recipe2.ID = 2
recipe2.Yield = 4
recipe3.name = "Simple Lemon Herb Chicken"
recipe3.prepTime = 10
recipe3.cookTime = 15
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 skinless boneless chicken breast halves,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 Lemon,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"Salt and Pepper to taste,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 tablespoon olive oil,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 sprigs fresh parsley (for garnish),",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 pinch dried oregano,",
)
recipe3.ID = 3
recipe3.Yield = 2
//recipe4 specifications
recipe4.name = "Easy Meatloaf"
recipe4.prepTime = 10
recipe4.cookTime = 60
recipe4.Ingredients = append(
recipe4.Ingredients,
"1 onion (chopped),",
)
recipe4.Ingredients = append(
recipe4.Ingredients,
"1 cup milk,",
)
recipe4.Ingredients = append(
recipe4.Ingredients,
"1 cup dried bread crumbs,",
)
recipe4.ID = 4
recipe4.Yield = 8
//recipe 5 specifications
recipe5.name = "Fast Salmon with a Ginger Glaze"
recipe5.prepTime = 5
recipe5.cookTime = 20
recipe5.Ingredients = append(
recipe5.Ingredients,
"salt to taste,",
)
recipe5.Ingredients = append(
recipe5.Ingredients,
"1/3 cup cold water,",
)
recipe5.Ingredients = append(
recipe5.Ingredients,
"1/4 cup seasoned rice vinegar,",
)
recipe5.ID = 5
recipe5.Yield = 4
//call to printRecipe function below
printRecipe(recipe1)
totalTime(recipe1)
printRecipe(recipe2)
totalTime(recipe2)
printRecipe(recipe3)
totalTime(recipe3)
printRecipe(recipe4)
totalTime(recipe4)
printRecipe(recipe5)
totalTime(recipe5)
//logic for Mondays Recipe
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe2)
} else if monday == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe3)
} else if monday == 3 {
fmt.Println(3)
printRecipeOfTheDay(recipe4)
} else if monday == 4 {
fmt.Println(4)
printRecipeOfTheDay(recipe5)
}
//logic for Tuesdays Recipe
if tuesday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if tuesday == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe2)
} else if tuesday == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe3)
} else if tuesday == 3 {
fmt.Println(3)
printRecipeOfTheDay(recipe4)
} else if tuesday == 4 {
fmt.Println(4)
printRecipeOfTheDay(recipe5)
}
}
//function to print Recipe
func printRecipe(recipe Recipe) {
fmt.Printf("Recipe Name : %s\n", recipe.name)
fmt.Printf("Prep Time : %d\n", recipe.prepTime)
fmt.Printf("Cook Time : %d\n", recipe.cookTime)
fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
fmt.Printf("Recipe ID : %d\n", recipe.ID)
}
//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
}
//function to print the winner for recipe of the day to use
//for either lunch or dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
以上是您要翻译的内容。
英文:
Thanks to your advice I fixed up my code and it is below. It is working as expected. Now I just have to clean it up more
package main
import (
"fmt"
"math/rand"
"time"
)
//Struct for Recipe below
type Recipe struct { //Struct for recipe information
name string
prepTime int
cookTime int
Ingredients []string //this is now a slice that will accept multiple elements
ID int
Yield int
}
//main method
func main() {
//5 variables below for 5 recipes for Monday-Friday
var recipe1 Recipe //Declare recipe1 of Type Recipe
var recipe2 Recipe
var recipe3 Recipe
var recipe4 Recipe
var recipe5 Recipe
//choose random number for recipe
r := rand.New(rand.NewSource(time.Now().UnixNano()))
i := r.Perm(5)
fmt.Printf("%v\n", i)
fmt.Printf("%d\n", i[0])
fmt.Printf("%d\n", i[1])
//assign slices of int from Perm to variables assigned days of the week
var monday = i[0]
var tuesday = i[1]
var wednesday = i[2]
var thursday = i[3]
var friday = i[4]
//testing printing of variables assigned to days
fmt.Printf("This is for the day Monday %d\n", monday)
fmt.Printf("This is for the day Tuesday %d\n", tuesday)
fmt.Printf("This is for the day Wednesday %d\n", wednesday)
fmt.Printf("This is for the day Thursday %d\n", thursday)
fmt.Printf("This is for the day Friday %d\n", friday)
/* recipe1 specifications */
recipe1.name = "BBQ Pulled Chicken"
recipe1.prepTime = 25
recipe1.cookTime = 5
recipe1.Ingredients = append(
recipe1.Ingredients,
"1 8-ounce can reduced-sodium tomato sauce",
)
recipe1.Ingredients = append(
recipe1.Ingredients,
"1/2 medium onion (grated),",
)
recipe1.ID = 1
recipe1.Yield = 8
/* Recipe 2 specifications */
recipe2.name = "Steak Tacos with Pineapple"
recipe2.prepTime = 45
recipe2.cookTime = 45
recipe2.Ingredients = append(
recipe2.Ingredients,
"3 tablespoons soy sauce,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated garlic,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 tablespoon finely grated peeled fresh ginger,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"1 1/2 pounds skirt steak, cut into 5-inch lengths,",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Salt",
)
recipe2.Ingredients = append(
recipe2.Ingredients,
"Pepper",
)
recipe2.ID = 2
recipe2.Yield = 4
recipe3.name = "Simple Lemon Herb Chicken"
recipe3.prepTime = 10
recipe3.cookTime = 15
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 skinless boneless chicken breast halves,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 Lemon,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"Salt and Pepper to taste,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 tablespoon olive oil,",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"2 sprigs fresh parsley (for garnish),",
)
recipe3.Ingredients = append(
recipe3.Ingredients,
"1 pinch dried oregano,",
)
recipe3.ID = 3
recipe3.Yield = 2
//recipe4 specifications
recipe4.name = "Easy Meatloaf"
recipe4.prepTime = 10
recipe4.cookTime = 60
recipe4.Ingredients = append(
recipe4.Ingredients,
"1 onion (chopped),",
)
recipe4.Ingredients = append(
recipe4.Ingredients,
"1 cup milk,",
)
recipe4.Ingredients = append(
recipe4.Ingredients,
"1 cup dried bread crumbs,",
)
recipe4.ID = 4
recipe4.Yield = 8
//recipe 5 specifications
recipe5.name = "Fast Salmon with a Ginger Glaze"
recipe5.prepTime = 5
recipe5.cookTime = 20
recipe5.Ingredients = append(
recipe5.Ingredients,
"salt to taste,",
)
recipe5.Ingredients = append(
recipe5.Ingredients,
"1/3 cup cold water,",
)
recipe5.Ingredients = append(
recipe5.Ingredients,
"1/4 cup seasoned rice vinegar,",
)
recipe5.ID = 5
recipe5.Yield = 4
//call to printRecipe function below
printRecipe(recipe1)
totalTime(recipe1)
printRecipe(recipe2)
totalTime(recipe2)
printRecipe(recipe3)
totalTime(recipe3)
printRecipe(recipe4)
totalTime(recipe4)
printRecipe(recipe5)
totalTime(recipe5)
//logic for Mondays Recipe
if monday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if monday == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe2)
} else if monday == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe3)
} else if monday == 3 {
fmt.Println(3)
printRecipeOfTheDay(recipe4)
} else if monday == 4 {
fmt.Println(4)
printRecipeOfTheDay(recipe5)
}
//logic for Tuesdays Recipe
if tuesday == 0 {
fmt.Println(0)
printRecipeOfTheDay(recipe1)
} else if tuesday == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe2)
} else if tuesday == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe3)
} else if tuesday == 3 {
fmt.Println(3)
printRecipeOfTheDay(recipe4)
} else if tuesday == 4 {
fmt.Println(4)
printRecipeOfTheDay(recipe5)
}
}
//function to print Recipe
func printRecipe(recipe Recipe) {
fmt.Printf("Recipe Name : %s\n", recipe.name)
fmt.Printf("Prep Time : %d\n", recipe.prepTime)
fmt.Printf("Cook Time : %d\n", recipe.cookTime)
fmt.Printf("Ingredients : %s\n", recipe.Ingredients)
fmt.Printf("Recipe ID : %d\n", recipe.ID)
}
//Returns total time by addings cookTime and prepTime
func totalTime(recipe Recipe) {
fmt.Printf("The total time for this recipe is %d\n", recipe.cookTime+recipe.prepTime)
}
//function to print the winner for recipe of the day to use
//for either lunch or dinner
func printRecipeOfTheDay(recipe Recipe) {
fmt.Printf("The recipe of the day is : %s\n", recipe.name)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论