提问人:Deasjia Shannon 提问时间:11/4/2023 更新时间:11/4/2023 访问量:39
C#:我使用 switch 语句创建了一个 SpanishTranslate 方法,但它在我的 Console.WriteLine 中不起作用
C#: I created a SpanishTranslate method using a switch statement but it won't work in my Console.WriteLine
问:
因此,要点如下,除了已经给出的方法(DisplayNumbers、FindSum、ProductSum、CreateRandomNumber)之外,我们还被赋予了创建两个方法的任务。我的任务是创建一个 CompareInteger 方法,用于比较数字并说明哪个更大。另一个是翻译西班牙语,我将数字更改为西班牙语单词(例如:7 = Siete)并输入“西班牙语中的数字是{西班牙数字}和{西班牙数字}。 - 问题是,每当我尝试打印“西班牙语中的数字是 {method} 和 {method} 时,它仍然打印数字而不是单词,但是如果我在 console.writeline 之外调用该方法,它就会起作用
现在,建议使用 and if 语句完成第一个方法 (CompareIntegers),使用 switch 语句完成第二个方法 (TranslateSpanish)。
我尝试过以不同的方式编写它,我不想通过从 console.writeline 调用不同行上的方法来作弊。
using System.Net;
class M9Methods
{
static Random randomNumber = new Random();
private static void Main(string[] args)
{
int num1, num2;
// Call method to create a random number and store in variable num1
num1 = CreateRandomNumber();
// Call method to create a random number and store in variable num2
num2 = CreateRandomNumber();
DisplayNumbers(num1, num2); // Call method to display the numbers
// Call method to find the sum and display the result.
Console.WriteLine($"The sum of {num1} and {num2} is {FindSum(num1, num2)}");
// Call method to find the product and display result.
Console.WriteLine($"The product of {num1} and {num2} is {FindProduct(num1, num2)}");
// Call method that compares the numbers and tells which is largest or if they are equal.
Console.WriteLine($"The largest number between {num1} and {num2} is {CompareInteger(num1, num2)}");
Console.WriteLine($"The {num1} and {num2} in spanish are {TranslateSpanish1(num1)}");
}
// Method to create a random integer from 1 to 10 and return it
public static int CreateRandomNumber()
{
int num = randomNumber.Next(10) + 1; // Create a random number from 1 to 10
return num;
}
// Method to display 2 integers
public static void DisplayNumbers(int n1, int n2)
{
Console.WriteLine($"The first number is {n1}");
Console.WriteLine($"The second number is {n2}");
}
// Method to return the sum of 2 integers
public static int FindSum(int n1, int n2)
{
return n1 + n2;
}
// Method to return the product of 2 integers
public static int FindProduct(int n1, int n2)
{
return n1 * n2;
}
// Method to compare 2 integers and display which is largest or if they are equal
public static int CompareInteger(int n1, int n2)
{
if (n1 > n2)
{
//Console.WriteLine($"The largest number is: {n1}");
return n1;
}
else if (n1 == n2)
{
Console.WriteLine($"These are the same numbers, Theyre equal!");
return n1;
}
else
{
// Console.WriteLine($"The largest number is: {n2} ");
return n2;
}
}
// Method to return/display the number in Spanish.
public static int TranslateSpanish1(int n1)
{
switch (n1)
{
case 1:
Console.WriteLine("Uno");
break;
case 2:
Console.WriteLine("Dos");
break;
case 3:
Console.WriteLine("Tres");
break;
case 4:
Console.WriteLine("Quatro");
break;
case 5:
Console.WriteLine("Cinco");
break;
case 6:
Console.WriteLine("Seis");
break;
case 7:
Console.WriteLine("Siete");
break;
case 8:
Console.WriteLine("Ocho");
break;
case 9:
Console.WriteLine("Nueve");
break;
case 10:
Console.WriteLine("Diez");
break;
default:
Console.WriteLine("couldnt comprehend");
break;
}
return n1;
}
}
答: 暂无答案
评论
Console.WriteLine($"The {num1} and {num2} in spanish are {TranslateSpanish1(num1)}");
int
int
int