从一种方法到另一种方法使用var

时间:2019-03-08 22:26:43

标签: c# list methods void

对于学校来说,我需要创建一个程序来洗牌并划分一副纸牌。

我想从一个方法(无效)中获取一个变量以返回。但是因为它在Void内部,所以不能。但是,如果我在Void之外创建它,则会出现错误。因为它是一个变种。我从那个变量中列出了一个清单。 我是C#的新手。帮助将不胜感激。

public static void main(String[] args) {
    System.out.println("Hello traveler.. Please enter your.. your... name");
    Scanner in = new Scanner(System.in);
    String userName = in.nextLine();
    System.out.println("Hello there " + userName);
    System.out.println("Welcome to the world of never ending lies. You can only leave if you solve my simple question.");
    System.out.println("What number between 1 and 10 do I like the most?");
    int numbs;
    numbs = in.nextInt(); // get numbers
    switch (numbs) {
    case 1:
        System.out.println("This is not my favorite number.");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 2:
        System.out.println("This traveler is indeed my favorite number."); // this is the right number
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 3:
        System.out.println("Did I tell you about that time in France? WRONG AGAIN!");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 4:
        System.out.println("This is definitely not it.");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 5:
        System.out.println("Wrong.");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 6:
        System.out.println("Wrong again.");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 7:
        System.out.println("Haha, you would think of this wouldn't you? W.r.O.n.G");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 8:
        System.out.println("Not right at all");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 9:
        System.out.println("Who do you think that I are, some girl that you'd meet at a bar? WRONG.");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    case 10:
        System.out.println("You are as naive as you are stupid. WRONG.");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;
    default:
        System.out.println("That's not even a choice you fool.");
        System.out.println("Please try again traveler, though I shouldn't have to say it.");
        break;

使用此代码,Shuffle将给出一个错误,他没有回报。我真的不知道该怎么办

2 个答案:

答案 0 :(得分:1)

您已经定义了Shuffle方法,如下所示:

public static Shuffle(List<string> deck)

在C#中,方法必须具有在方法名称之前的返回类型。将声明更改为此:

public static List<string> Shuffle(List<string> deck)

答案 1 :(得分:0)

此外,您可能想创建一个表示Card的简单类:

public class Card
{
    public Card(int color, int val)
    {
        Color = color;
        Value = val;
    }
    public int Color { get; }
    public int Value { get; }

    // That might look nicer in the debugger (with less code).
    public override string ToString()
    {
        return "♥♣♦♠"[Color] + "A23456789TNQK"[Value];
    }
}

我不确定,如果您当然也是(有点)关于对象定向的,但是您可能想要实现静态方法Card.GetDeck(),以返回卡的列表(或数组)。