如何根据输入做出不同结果的是或否问题? [英] How do I make a yes or no question with different outcomes depending on input?

查看:94
本文介绍了如何根据输入做出不同结果的是或否问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在高中完成基础编程,并负责一个项目,在这个项目中,我要制作一个向老师打招呼的程序,并提供一个段落,说明为什么编程在今天的时间是有价值的。



我最初的想法是制作一个迎接问题的程序,并询问老师是否想知道编程为何有价值的问题。当控制台询问问题时,将有两个选项:是或否。每个都有自己的文本输出。



I have Programming in Basic in High School and was tasked with a project in which I was to make a program that says hello to the teacher as well as give a paragraph describing why programming is valuable in today's time.

My initial idea was to make a program that greets and asks the teacher a question on whether they want to know why programming is valuable. When the console asks the question there will be two options: Yes or No. Each having their own text output.

Module Module1

    Sub Main()
        REM Test
        Dim Yes As String
        Dim No As String
        Console.WriteLine("Hello, Would you like to know why programming is valuable? Yes or No")
        Yes = Console.ReadLine
        No = Console.ReadLine
        If Console.ReadLine = Yes Then
            Console.WriteLine("Well...1")
        End If
        If Console.ReadLine = No Then
            Console.WriteLine("Well...2")
        End If

    End Sub

End Module





这有效但我必须在控制台上放三次是弹出嗯...... 1,当我用否测试时,弹出嗯...... 1而不是嗯...... 2。有时候嗯...... 2会弹出,但这种情况很少见。



我尝试了什么:



我已尝试过在其他论坛上向我推荐的所有内容,但没有成功。到目前为止,这是我最成功的代码,但仍然有一些缺陷。



This works but I have to put "Yes" on the console three times before the "Well...1" pops up and when I test it out with "No", "Well...1" pops up instead of "Well...2". Sometimes "Well...2" pops up but it's rare.

What I have tried:

I've tried everything suggested to me in other forums but no success. So far, this is the code I had the most success with but still get a few flaws.

推荐答案

你正在做很多console.readline(s)那......你确定你需要那么多吗?怎么样的东西(使用一个变量来保持答案,而不是一个用于是和否)



you're doing a lot of console.readline(s) there ... are you sure you need that many ? how about something like (using one variable to hold the answer, instead of one for yes and no)

Dim answer As String
Console.WriteLine("Hello, Would you like to know why programming is valuable? Yes or No")
StringComparison comparison = StringComparison.OrdinalIgnoreCase 
answer = Console.ReadLine
If answer.Contains("yes", comparison) Then
    Console.WriteLine("Well...1")
End If
If answer.Contains("no"), comparison Then
    Console.WriteLine("Well...2")
End If





请注意(如果我已正确完成此操作),如果他们输入'YES'或'yES则无关紧要'或'是的确如此' - StringComparison和contains将处理它 - 然后你可以在你的If / End-If支票中对'answer'做更多的工作



这会让生活变得更轻松吗?



note that (if Ive done this correctly), it doesnt matter if they enter 'YES' or 'yES' or 'Yes it does' - the StringComparison and the contains will handle it - you can then do more work on 'answer' in your If/End-If checks

does that make life easier ?


引用:

这有效但我必须把是在Well ... 1弹出之前三次在控制台上,当我用No测试时,Well ... 1弹出而不是Well ... 2。有时候嗯...... 2会弹出,但很少见。

This works but I have to put "Yes" on the console three times before the "Well...1" pops up and when I test it out with "No", "Well...1" pops up instead of "Well...2". Sometimes "Well...2" pops up but it's rare.

这正是你要求你的程序做的。



你应该学习使用调试器尽快。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

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

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。

This is exactly what you asked your program to do.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于如何根据输入做出不同结果的是或否问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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