分割字符串数组[index] >常量,当它应该为false时返回True
本文关键字:false True 返回 数组 字符串 index 常量 分割 | 更新日期: 2023-09-27 17:54:55
首先,我是新来的,我希望这符合你们的提问准则。我不相信这里的其他问题/线索是适用的(至少他们看起来不是)。
不管怎样,我是一个编程新手,在大学里上c#课。我正在处理的任务与Split()方法有关。我应该在文本框中向用户询问姓名和(保龄球)分数,将其拆分为一个临时数组,然后从临时数组中获取适当的索引值,并将它们放入Name和Score数组中。它应该处理最多10个玩家,但它应该工作少于这个(部分填充数组)。
它将计算并输出高分+谁拥有它,低分+谁拥有它,和平均分数,并输出所有的名字和分数。我想在进入数组之前拒绝任何不在0到300之间的输入分数,但是当我输入超出该范围的值并进行调试时,它会说大于/小于/等于为真。显然,如果我输入9001或-3等作为分数,我希望它被拒绝。下面是这部分代码。
public void GetSplit(string _stg)
{
string[] tempArray;
//Split the temp array
tempArray = _stg.Split();
//Professor said a for loop would be unnecessary since it's a GUI
//LOW = 300
if(score[index] <= LOW && score[index] >= 0)
{//this part here^^ is returning true when it should be false
//1st score is in the 2nd slot in tempArray
//if input is not between 0-300 and is NOT an integer, yell at them
if (!int.TryParse(tempArray[1], out score[index]))
{
MessageBox.Show(YELL_INT);
return;
}
bool status = (int.TryParse(tempArray[1], out score[index]) ? true :false);
//1st name is in 1st slot in tempArray
name[index++] = tempArray[0];
}
}
如果有人有一个解决方案,如何或为什么这是不工作,以及一个例子,如何让它工作(甚至重定向到另一个回答的问题,类似于一个我问,我错过了),那太棒了。谢谢!
我不知道为什么我有必要添加这个,但这里是程序中的所有代码:
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 Proj_08
{
public partial class FrmMain : Form
{
private BowlingScores bs;//reference to bowlingscores class
/// <summary>
/// Purpose: Use the BowlingScores class to display names of players and scores
/// as well as high score + name, low score + name, and average score
/// </summary>
public FrmMain()
{
InitializeComponent();
}
/// <summary>
/// Purpose: Initialize BowlingScores and _tempArray
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
bs = new BowlingScores();
}
/// <summary>
/// Purpose: Close out of program
/// </summary>
/// <param name="sender">Button Clear Click Event</param>
/// <param name="e">EventArgs Object</param>
public void MSpExit_Click(object sender, EventArgs e)
{
Close();
}
/// <summary>
/// Purpose: Send the contents of the textbox to the GetScore method in BowlingScores class
/// </summary>
/// <param name="sender">Entry Enter Key Press Event</param>
/// <param name="e">KeyPressEventArgs Object</param>
private void TxtEntry_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == (char)Keys.Enter)
{
bs.GetSplit(TxtEntry.Text);
TxtEntry.Text = string.Empty;
}
}
/// <summary>
/// Purpose: show everything in RichTextBox
/// </summary>
/// <param name="sender">Button Calc Click Event</param>
/// <param name="e">EventArgs Object</param>
public void BtnCalc_Click(object sender, EventArgs e)
{
//Output returned string from GetAll method
RTbOutput.Text = bs.GetAll();
}
/// <summary>
/// Purpose: Clear the textboxes and reset all arrays and references and the index
/// </summary>
/// <param name="sender">Button Clear Click Event</param>
/// <param name="e">EventArgs Object</param>
private void BtnClear_Click(object sender, EventArgs e)
{
bs = new BowlingScores();
TxtEntry.Text = string.Empty;
RTbOutput.Text = string.Empty;
}
}
//class BowlScores
public class BowlingScores
{
private const int LOW = 300;
private const int ASIZE = 10;
private const string YELL_INT = "Invalid Score";
private const string HEADER = "ERROR";//still have to add this
private int[] score;
private string[] name;
private int index;
/// <summary>
/// Purpose: Constructor for BowlingScores Class
/// </summary>
public BowlingScores()
{
index = 0;
score = new int[ASIZE];
name = new string[ASIZE];
}
/// <summary>
/// Purpose: Getter/Setter for name array
/// </summary>
public string[] Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// Purpose: Getter/Setter for score array
/// </summary>
public int[] Score
{
get { return score; }
set { score = value; }
}
/// <summary>
/// Purpose: Capture text from textbox and split into name and score arrays
/// </summary>
/// <param name="_stg"></param>
public void GetSplit(string _stg)
{
//int index = 0;
string[] tempArray;
//Split the temp array
tempArray = _stg.Split();
if(score[index] <= LOW && score[index] >= 0)
{
//1st score is in the 2nd slot in tempArray
//if input is not between 0-300 and is NOT an integer, yell at them
if (!int.TryParse(tempArray[1], out score[index]))
{
MessageBox.Show(YELL_INT, HEADER, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
bool status = (int.TryParse(tempArray[1], out score[index]) ? true : false);
//1st name is in 1st slot in tempArray
name[index++] = tempArray[0];
//increment index to continue filling respective arrays
}
}
/// <summary>
/// Purpose: Calculate High, Low, Average
/// </summary>
/// <returns></returns>
public string CalcData()
{
int high = 0;//to figure high score
string holdHigh = "";
int low = LOW;//to figure low score
string holdLow = "";
double sum = 0.0;
double avg = 0.0;
double count = 0.0;//to calculate average,
//
for (int index = 0; index < score.Length && name[index] != null; index++)
{
//calculate high score
//if an entered score is greater than 0, replace high with that entered score
if (score[index] > high && score[index] <= LOW && score[index] >= 0)
{
high = score[index];
holdHigh = name[index];
}
//calculate the low score
//if an entered score is less than 300, replace low with that entered score
if (score[index] < low && score[index] <= LOW && score[index] >= 0)
{
low = score[index];
holdLow = name[index];
}
//calculate sum and average
if (score[index] <= LOW && score[index] >= 0)
sum += score[index];
count = index + 1;
avg = (sum / count);
}
return string.Format("Congratulations {0}, you got the high score of {1}!'nBetter luck next time, {2}, you got the lowest score of {3}'nThe average score is {4:F2}", holdHigh, high, holdLow, low, avg);
}
/// <summary>
/// Purpose: Get all the names and scores and output them as a string
/// </summary>
/// <returns></returns>
public string GetAll()
{
string outputStg = "";
//as long as entry isn't null and index is less than Length
for(int index = 0; index < score.Length && name[index] != null ; index++)
{
//if the score is above 300 or below 0, don't return those values
if (score[index] <= LOW && score[index] >= 0 )
outputStg += name[index] + "'t't" + score[index] + "'n";
}
return outputStg += "'n" + CalcData();
}
}
}
因此,从查看您的代码,到目前为止我看到的一个缺陷是,您正在检查score
时还没有添加任何内容!如果您的tempArray
包含用户输入的数据,您应该首先检查它。
就像
// I'm assuming your incoming string is formatted like "Name Score Name Score"
// E.G. "Ryan 140 Tim 400"
int result;
string[] tempArray = _stg.Split();
if (int.TryParse(tempArray[1], out result))
{
if (result <= LOW && result >= 0)
// within bounds, add it to score array, etc.
}