我試圖調試此代碼,但在第三種方法中,它不允許我使用不同的數據類型。在我的教科書中,該示例顯示他們使用不同的數據類型作爲參數。爲什麼它不允許我這樣做?我確信這段代碼有其他問題,但是我直到清除錯誤時才調試它們。C#參數不能將int轉換爲字符串或字符串int
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WP_Week3_Problem2
{
public partial class Problem2 : Form
{
public Problem2()
{
InitializeComponent();
}
int[] productID = { 1234, 5678, 5677, 8765, 2345, 8734 };
double[] productPrice = { 5.55, 20.0, 40.0, 56.34, 78.10, 98.0 };
String[] productName = { "Pencil", "Backpack", "MessengerBag", "RoomHeater", "SewingMachine", "Decorative Lamp" };
private void Problem2_Load(object sender, EventArgs e)
{
}
private void buttonLoadTable_Click(object sender, EventArgs e)
{
labelOutput.Text = "ProdID Price ProdName";
for (int i = 0; i < productID.Length; i = i + 1)
{
labelOutput.Text += String.Format("\n {0} {1} {2} ", productID[i], productPrice[i].ToString("C"), productName[i]);
}
}
private void buttonSearch_Click(object sender, EventArgs e)
{
int prodID = Convert.ToInt32(textBoxID.Text);
String prodName = textBoxName.Text;
double prodPrice = Convert.ToDouble(textBoxPrice.Text);
SearchProducts(prodID);
SearchProducts(prodName);
SearchProducts(prodID, prodName, prodPrice);
}
private void SearchProducts(int prodID)
{
int prodMinus = prodID - 1;
int idExists = Array.IndexOf(productID, prodMinus);
if (idExists == -1)
{
labelOutput.Text = "That product ID is invalid.\nPlease enter a valid product ID";
textBoxID.Text = "";
}
}
private void SearchProducts(String prodName)
{
String nameExists = Array.IndexOf(productName, prodName).ToString();
bool isName = true;
if (nameExists == "Pencil")
isName = true;
else if (nameExists == "Backpack")
isName = true;
else if (nameExists == "MessengerBag")
isName = true;
else if (nameExists == "RoomHeater")
isName = true;
else if (nameExists == "SewingMachine")
isName = true;
else if (nameExists == "Decorative Lamp")
isName = true;
else
isName = false;
labelOutput.Text = "This Product Name is invalid.\nPlease enter a valid Product Name";
}
private void SearchProducts(String prodName, int prodID, double prodPrice)
{
labelOutput.Text = String.Format("Your Product ID of {0} is a {2} and it costs: {1}", prodID, prodName, prodPrice.ToString("C"));
}
}
}
C#是一種靜態類型的語言。無論你想要什麼,你都不能簡單地「使用不同的類型」。哪一行是拋出這個錯誤?你使用的是什麼類型與期望的類型?你想在這條線上完成什麼? – David
第45行是我聲明方法的地方,第79行是我嘗試在字符串中使用三種數據類型的地方。格式 – Bob
@Bob尼斯,讓我算你的帖子中的行:) –