Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
有没有办法让C#读取同一行中的整数和字符串?_C#_String_Integer - Fatal编程技术网

有没有办法让C#读取同一行中的整数和字符串?

有没有办法让C#读取同一行中的整数和字符串?,c#,string,integer,C#,String,Integer,我正在尝试制作这个程序: 我不确定是否应该使用if-else或某种循环或两者兼用。这是我的第一个编程课程,所以任何帮助都会很好 这是我到目前为止所拥有的,它是有效的,但它不是我想要的 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assignment_3 { class Program

我正在尝试制作这个程序:

我不确定是否应该使用if-else或某种循环或两者兼用。这是我的第一个编程课程,所以任何帮助都会很好

这是我到目前为止所拥有的,它是有效的,但它不是我想要的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 using System.Threading.Tasks;

namespace Assignment_3
{
class Program
{
    static void Exercise1(){


    }

    static void Exercise2()
    {

        double number;
        string character;
        Console.Write("Enter an integer or 'q' to quit ");
        number = double.Parse(Console.ReadLine());
        character = Console.ReadLine();
        if (number >= 1 && number <= 10 || character =="q")
        {
            Console.WriteLine("Coool");

        }
        else
        {
            Console.WriteLine("Your number must be between 1 and 10");
        }
    }
    static void Main(string[] args)
    {
        Exercise1();

        Exercise2();

        Console.WriteLine("Press enter to end");
        Console.ReadLine();
    } 
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间分配\u 3
{
班级计划
{
静态空洞练习1(){
}
静态空洞练习2()
{
双数;
字符串;
Console.Write(“输入一个整数或'q'退出”);
number=double.Parse(Console.ReadLine());
character=Console.ReadLine();

如果(number>=1&&number将ReadLine()的结果存储为变量并对其求值,检查“q”,如果不起作用,则将变量解析为int。

静态void Exercise2()
static void Exercise2()
    {


        string entry;
        Console.Write("Enter an integer or 'q' to quit: ");
        entry = Console.ReadLine();
        while (entry.ToLower() != "q")
        {
            int number;
            if (int.TryParse(entry, out number))
            {
                if (number >= 1 && number <= 10)
                {
                    Console.WriteLine("Coool");

                }
                else
                {
                    Console.WriteLine("Your number must be between 1 and 10");
                }
            }

            Console.Write("Enter an integer or 'q' to quit: ");
            entry = Console.ReadLine();
        }

    }
{ 字符串输入; 写入(“输入一个整数或'q'退出:”); entry=Console.ReadLine(); while(entry.ToLower()!=“q”) { 整数; if(输入、输出编号) {
如果(number>=1&&number SO是寻找“任何帮助”的错误位置-如果您有具体问题-请确保通过更新您的帖子来询问,否则在帖子仍然为0分时删除该帖子可能是个好主意。
Console.ReadLine()的结果
是一个字符串。您可以将字符串值存储在变量中,并检查它是否等于
q
,而不必将字符串解析为一个数字。我这样做了。它在问题的标题中,然后我问我是否应该使用循环来实现我想要的内容。“任何帮助”部分是为了帮助澄清我是新手,我不确定我需要做什么来实现这个计划,所以我只是简单地说我会接受任何帮助建议。@RyanHunt如果我提供的答案令人满意,那么请接受它。否则,如果你有任何问题,请告诉我们。