2014-03-29 44 views
0

首先,我是全新的,我希望這符合您的指南的問題。我不相信這裏的其他問題/線索是適用的(至少他們似乎沒有)。拆分字符串數組[索引]>常量返回True時,應該是false

不管怎樣,我是一名編程新手,在大學裏學習C#課程。我正在處理的任務與Split()方法有關。我應該向用戶詢問文本框中的名稱和(保齡球)分數,將其分成一個臨時數組,然後從臨時數組中取適當的索引值,並將它們放在名稱和分數數組中。它應該處理多達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(); 
    } 


} 
} 
+1

哇,人......我發誓我試圖理解你的代碼(超過10分鐘!),但我不知道你在做什麼..對不起 – Buzinas

+0

那會是因爲我不知道我在做什麼!那麼,我想我知道我在做什麼,但我不知道在這種情況下有什麼問題。無論如何,感謝您的期待!這是我的第一個編程課,我仍然試圖去掌握一切。 – Heckmaster9

回答

0

所以從看你的代碼,一個缺陷我看到迄今是你沒有添加任何東西時檢查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. 
} 
+0

這使得更多的意義!我能夠使用你的建議達到良好效果,我忘了標記它是正確的。謝謝! – Heckmaster9

相關問題