试着让这段代码在main中显示...请帮助 [英] Trying to get this code to display value in main...help please

查看:59
本文介绍了试着让这段代码在main中显示...请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试让这段代码工作,以便在主卡片中显示获得卡路里的价值





我尝试过:



Trying to get this code to work so that the value collected in get calories
is displayed in the main

What I have tried:

Sub Main()
    Dim lv_foodCalories As Double
    getCalories(lv_foodCalories)
    Console.WriteLine("You food contains {0} calories.", lv_foodCalories)
    Console.ReadLine()
End Sub

Sub getCalories(pv_calories As Double)
    Dim lv_userInput As String
    Do
        Console.Write("How many are in your food?: ")
        lv_userInput = Console.ReadLine
        If IsNumeric(lv_userInput) Then
            pv_calories = CDbl(lv_userInput)
        End If
    Loop
End Sub

推荐答案

您好 Dan



使用 ByRef 参数声明中的关键字。默认 VB.NET 使用 ByVal ,这意味着 sub 获取参数的新副本以及它对参数所做的任何更改都是针对该副本进行的,而不是针对预期的副本。

Hi Dan,

Use ByRef keyword at the parameter declaration. By default VB.NET uses ByVal which means the sub gets a new copy of the parameter and whatever change it makes to the parameter is made to that copy, and not to the intended one.
Sub getCalories(ByRef pv_calories As Double)

另外,我没有在while循环中看到任何终止逻辑,它将永远不断询问。你需要放一些终止逻辑。例如。 - 输入数字后退出。

Plus I don't see any termination logic in the while loop, it will keep asking forever. You need to put some termination logic. E.g. - exit after a number input.

If IsNumeric(lv_userInput) Then
    pv_calories = CDbl(lv_userInput)
    Exit Sub
End If


Quote:

我刚刚在Visual Studio的顶部点击调试它运行程序...它不会引导我完成代码。

I just hit debug at the top of Visual studio and it runs the program... it doesnt step me through the code.



调试器是一个工具,就像你需要学习如何使用它的任何工具一样。



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

没有魔法调试器,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。

调试器 - 维基百科,免费的百科全书 [ ^ ]

Visual Basic / Visual Studio视频教程 - 基本调试 - YouTube [ ^ ]

视觉基础教程 - 调试您的应用程序 - YouTube [ ^ ]


The debugger is a tool, and like for any tool you need to learn how to use it.

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic Tutorial - Debugging Your Applications - YouTube[^]


这篇关于试着让这段代码在main中显示...请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆