Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何使其成为嵌套循环_Vb.net - Fatal编程技术网

Vb.net 如何使其成为嵌套循环

Vb.net 如何使其成为嵌套循环,vb.net,Vb.net,大家好,我的代码和号码终于正确了,唯一的问题是我不能让它循环正确。它应该询问用户是否愿意购买更多的酒吧,如果他们说愿意,它应该继续,如果它说不,那么它应该退出,但我尝试的一切都不起作用。我开始认为这是我如何将信息输入到程序中的问题,有人能指出我在感谢中搞砸了什么 Sub Main() ' declare variable Dim Answer As Char Do Console.Write("Would you like to buy some c

大家好,我的代码和号码终于正确了,唯一的问题是我不能让它循环正确。它应该询问用户是否愿意购买更多的酒吧,如果他们说愿意,它应该继续,如果它说不,那么它应该退出,但我尝试的一切都不起作用。我开始认为这是我如何将信息输入到程序中的问题,有人能指出我在感谢中搞砸了什么

Sub Main()

    ' declare variable

    Dim Answer As Char

    Do
        Console.Write("Would you like to buy some candy bars(1=Yes/0=No)? ")
        Answer = CChar(Console.ReadLine())
    Loop Until Answer = "1"

    Console.WriteLine("Please enter the amount of candy you want to buy")
    Console.WriteLine()


    Dim amountDollar As Integer
    Dim leftOverCoupons As Integer
    Dim numberofChocolate As Integer
    Dim freeChocolate As Integer
    amountDollar = CInt(Console.ReadLine())
    numberofChocolate = amountDollar
    leftOverCoupons = numberofChocolate
    While (leftOverCoupons >= 7)
        freeChocolate = CInt(Math.Truncate(leftOverCoupons / 7))
        numberofChocolate = numberofChocolate + freeChocolate
        leftOverCoupons = (leftOverCoupons Mod 7) + freeChocolate

    End While



    Console.WriteLine("Total number of chocolate: " & numberofChocolate)
    Console.WriteLine("Leftover Coupons: " & leftOverCoupons)
    Console.ReadLine()

End Sub

为什么你需要一个循环呢?一个简单的if语句,用于检查答案是否为1 else Application.Exit()

为什么需要循环?一个简单的if语句,用于检查答案是否为1 else Application.Exit()?您提到如果用户选择“否”,那么应用程序应该退出。但是,循环将继续,直到用户选择“是”:

你甚至根本不需要一个循环。只需提示用户一次并读取他们的响应。如果为“否”,则退出应用程序。(类似于
Application.Exit()
)如果为“是”,则继续执行其余逻辑


(此外,如果输入不是“否”或“是”,您可以重新提示,这可能涉及循环,或返回错误,或退出应用程序等。您希望如何处理意外输入取决于您自己,但应予以考虑。)

到底是什么不起作用?您提到如果用户选择“否”,那么应用程序应该退出。但是,循环将继续,直到用户选择“是”:

你甚至根本不需要一个循环。只需提示用户一次并读取他们的响应。如果为“否”,则退出应用程序。(类似于
Application.Exit()
)如果为“是”,则继续执行其余逻辑


(此外,如果输入不是“否”或“是”,您可以重新提示,这可能涉及循环,或返回错误,或退出应用程序等。您希望如何处理意外输入取决于您自己,但应予以考虑。)

我想您正在寻找类似以下内容:

Sub Main()

' declare variable

Dim Answer As Char

Console.Write("Would you like to buy some candy bars(1=Yes/0=No)? ")
Answer = CChar(Console.ReadLine())

While (Answer = 1)

    Console.WriteLine("Please enter the amount of candy you want to buy")
    Console.WriteLine()


    Dim amountDollar As Integer
    Dim leftOverCoupons As Integer
    Dim numberofChocolate As Integer
    Dim freeChocolate As Integer
    amountDollar = CInt(Console.ReadLine())
    numberofChocolate = amountDollar
    leftOverCoupons = numberofChocolate
    While (leftOverCoupons >= 7)
        freeChocolate = CInt(Math.Truncate(leftOverCoupons / 7))
        numberofChocolate = numberofChocolate + freeChocolate
        leftOverCoupons = (leftOverCoupons Mod 7) + freeChocolate

    End While



    Console.WriteLine("Total number of chocolate: " & numberofChocolate)
    Console.WriteLine("Leftover Coupons: " & leftOverCoupons)

    Console.Write("Would you like to buy more candy bars(1=Yes/0=No)? ")
    Answer = CChar(Console.ReadLine())

End While

End Sub

我想你在找这样的东西:

Sub Main()

' declare variable

Dim Answer As Char

Console.Write("Would you like to buy some candy bars(1=Yes/0=No)? ")
Answer = CChar(Console.ReadLine())

While (Answer = 1)

    Console.WriteLine("Please enter the amount of candy you want to buy")
    Console.WriteLine()


    Dim amountDollar As Integer
    Dim leftOverCoupons As Integer
    Dim numberofChocolate As Integer
    Dim freeChocolate As Integer
    amountDollar = CInt(Console.ReadLine())
    numberofChocolate = amountDollar
    leftOverCoupons = numberofChocolate
    While (leftOverCoupons >= 7)
        freeChocolate = CInt(Math.Truncate(leftOverCoupons / 7))
        numberofChocolate = numberofChocolate + freeChocolate
        leftOverCoupons = (leftOverCoupons Mod 7) + freeChocolate

    End While



    Console.WriteLine("Total number of chocolate: " & numberofChocolate)
    Console.WriteLine("Leftover Coupons: " & leftOverCoupons)

    Console.Write("Would you like to buy more candy bars(1=Yes/0=No)? ")
    Answer = CChar(Console.ReadLine())

End While

End Sub

首先,你们的糖果店在人们同意购买糖果之前一直把他们扣为人质……;)说真的,你希望在第一个循环中嵌套第二个循环吗?首先,你的糖果店在人们同意购买糖果之前会扣押他们作为人质……;)严肃地说,你希望在第一个循环中嵌套第二个循环吗?我假设使用嵌套循环询问用户是否愿意再次购买糖果。这让我很困惑,一点也不好笑。我假设使用嵌套循环询问用户是否愿意再次购买糖果。这让我很困惑,一点也不好笑。我明白你的意思。。他们的任务我已经工作了3天,他们说解决这个问题的最好方法是循环,然后使用嵌套循环询问他们是否愿意再次这样做。现在它把我弄糊涂了,一点也不好笑。@sha:好吧,退一步,问问你自己(我想为了理解作业的目的)循环在做什么,在这种情况下需要循环什么逻辑。例如,当您说“否”的答案应该退出所有内容时,这是否意味着它真的退出了应用程序,或者它返回到循环的开始(就像在代码中一样)?一组可能的嵌套循环逻辑可能是:
提示销售时(如果答案是肯定的(提示优惠券时(进行销售))
我明白你的意思。。他们的任务我已经工作了3天,他们说解决这个问题的最好方法是循环,然后使用嵌套循环询问他们是否愿意再次这样做。现在它把我弄糊涂了,一点也不好笑。@sha:好吧,退一步,问问你自己(我想为了理解作业的目的)循环在做什么,在这种情况下需要循环什么逻辑。例如,当您说“否”的答案应该退出所有内容时,这是否意味着它真的退出了应用程序,或者它返回到循环的开始(就像在代码中一样)?一组可能的嵌套循环逻辑可能是:
提示销售时(如果答案是肯定的(提示优惠券时(进行销售))