2016-04-28 3 views
2

これは可能ですか?それは何らかの関数を使って行うことができますか?私が見つけることができるのは、視覚的な基本的なウィンドウフォームアプリケーション内のテキストボックスの幅に出力を広げる方法の例です。ビジュアルな基本コンソールアプリケーションで出力を行う方法は、端末ウィンドウの幅全体に及んでいますか?

私のコードは以下の通りです。

Sub Main() 
    Dim usrnum(1), usramount, userchoice, count, result2 As Integer 
    Dim totalstars(1), question1, userchoice2, nchrt, result1 As String 

    Do Until question1 = "y" 
     userchoice = 0 
     Do Until userchoice = 1 Or userchoice = 2 
      'menu - text tries to be as specific as possible to prevent an incorrect input from the user 




      WriteLine("------------------- Bar chart creator! ---------------------") 
      WriteLine("                ") 
      WriteLine("                ") 

      WriteLine("===============================================================") 
      WriteLine("************************** MENU ***************************") 
      WriteLine("===============================================================") 
      WriteLine("                ") 
      ForegroundColor = ConsoleColor.Cyan 
      WriteLine(" 1 - To begin            ") 
      ForegroundColor = ConsoleColor.Yellow 
      WriteLine(" 2 - To exit             ") 
      WriteLine("                ") 
      WriteLine("                ") 
      WriteLine("                ") 
      WriteLine("                ") 


      'reads in data from the user from the menu 
      Try 
       userchoice = ReadLine() 
       Clear() 
      Catch ex As Exception 
       'if this produces an error, for example a string is entered by the user then the following message will be issued to the user 
       WriteLine("That value is unknown!") 
       WriteLine("Please enter an Integer only") 
      End Try 
      If userchoice = 1 Then 
       WriteLine("What would you like to name your bar chart as?") 
       nchrt = ReadLine() 
       Clear() 
       WriteLine("Enter the amount of values you want to enter.") 
       Try 
        usramount = ReadLine() 
       Catch ex As Exception 
        'if this produces an error, for example a string is entered by the user then the following message will be issued to the user     
        WriteLine("That value is unknown!") 
        WriteLine("Please enter an Integer only: ") 
       End Try 
       Clear() 
       WriteLine("Values for the chart " & nchrt) 
       WriteLine("") 
       WriteLine("Please note! Each * Is equal To 100. Once a value reaches over 1000 the maxium amount Of * shown Is 10") 
       WriteLine("") 
       ReDim usrnum(usramount) 're-assign the users input as the value for the arraysize 
       ReDim totalstars(usramount) 
       count = 0 
       'the count allows the user to see what how many values they have entered so far and what number they are currently on 
       While count < usramount 
        For i As Integer = 0 To usrnum.Length - 1 
         'allows the users numbers to be recorded into the array 
         WriteLine("Please enter value " & count & " And <press> enter") 
         Try 
          usrnum(i) = ReadLine() 
          count = count + 1 
         Catch ex As Exception 
          'if this produces an error, for example a string is entered by the user then the following message will be issued to the user 
          WriteLine("That value is unknown!") 
          WriteLine("Please enter an Integer only.") 
          ReadLine() 
         End Try 
         Clear() 
        Next i 
       End While 
       'Calulates how many * to display with an IF statement 
       For i As Integer = 0 To totalstars.Length - 1 
        If usrnum(i) > 1000 Then 
         totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ") 
         'This was where my function was supposed to work but I can't get it to work the way I wanted it to 
         'These commented out if statements would have checked to see if the usrnum(i) current value was the biggest in relation to the output of the function max() 
         'If the current value of usrnum(i) was equal to the biggest value output by max() then extra spacing would be added between the stars so that they would span the width of the console window 
         'Else 
         'If usrnum(i) > 1000 And usrnum(i) = Max(usrnum(usramount)) Then 
         'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ") 
        Else 
         If usrnum(i) >= 900 And usrnum(i) < 1000 Then 
          totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ") 
          'Else 
          'If usrnum(i) >= 900 And usrnum(i) < 1000 And usrnum(i) = Max(usrnum(usramount)) Then 
          'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ") 
         Else 
          If usrnum(i) >= 800 And usrnum(i) < 900 Then 
           totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ") 
           'Else 
           'If usrnum(i) >= 800 And usrnum(i) < 900 And usrnum(i) = Max(usrnum(usramount)) Then 
           'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ") 
          Else 
           If usrnum(i) >= 700 And usrnum(i) < 800 Then 
            totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * ") 
            'Else 
            'If usrnum(i) >= 700 And usrnum(i) < 800 And usrnum(i) = Max(usrnum(usramount)) Then 
            'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * ") 

           Else 
            If usrnum(i) >= 600 And usrnum(i) < 700 Then 
             totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * ") 
             'Else 
             'If usrnum(i) >= 600 And usrnum(i) < 700 And usrnum(i) = Max(usrnum(usramount)) Then 
             'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * ") 
            Else 
             If usrnum(i) >= 500 And usrnum(i) < 600 Then 
              totalstars(i) = (" * " & " * " & " * " & " * " & " * ") 
              'Else 
              'If usrnum(i) >= 500 And usrnum(i) < 600 And usrnum(i) = Max(usrnum(usramount)) Then 
              'totalstars(i) = (" * " & " * " & " * " & " * " & " * ") 
             Else 
              If usrnum(i) >= 400 And usrnum(i) < 500 Then 
               totalstars(i) = (" * " & " * " & " * " & " * ") 
               'Else 
               'If usrnum(i) >= 400 And usrnum(i) < 500 And usrnum(i) = Max(usrnum(usramount)) Then 
               'totalstars(i) = (" * " & " * " & " * " & " * ") 
              Else 
               If usrnum(i) >= 300 And usrnum(i) < 400 Then 
                totalstars(i) = (" * " & " * " & " * ") 
                'Else 
                'If usrnum(i) >= 300 And usrnum(i) < 400 And usrnum(i) = Max(usrnum(usramount)) Then 
                'totalstars(i) = (" * " & " * " & " * ") 
               Else 
                If usrnum(i) >= 200 And usrnum(i) < 300 Then 
                 totalstars(i) = (" * " & " * ") 
                 'Else 
                 'If usrnum(i) >= 200 And usrnum(i) < 300 And usrnum(i) = Max(usrnum(usramount)) Then 
                 'totalstars(i) = (" * " & " * ") 
                Else 
                 If usrnum(i) >= 100 And usrnum(i) < 200 Then 
                  totalstars(i) = " * " 
                  'Else 
                  'If usrnum(i) >= 300 And usrnum(i) < 400 And usrnum(i) = Max(usrnum(usramount)) Then 
                  'totalstars(i) = (" * ") 
                 Else 
                  If usrnum(i) >= 0 And usrnum(i) < 100 Then 
                   totalstars(i) = " " 
                  ElseIf usrnum(i) < 0 Then 
                   totalstars(i) = " " 
                  End If 
                 End If 
                End If 
               End If 
              End If 
             End If 
            End If 
           End If 
          End If 
         End If 
        End If 
        'End If 
        ' End If 
        'End If 
        'End If 
        'End If 
        'End If 
       Next i 
       Clear() 
       'displays results of the bar chart to the user. 
       Do Until userchoice2 = "y" Or userchoice2 = "n" 
        'gives user an extra choice for displaying the bar chart 
        WriteLine("would you like your bar chart sorted into desending order?") 
        WriteLine("Y = yes") 
        WriteLine("N = no ") 
        userchoice2 = ReadLine() 
        If userchoice2 = "y" Then 
         Clear() 
         'sorts the stars and numbers out so they display in desending order, looks neater also for the user to read 
         Array.Sort(usrnum) 
         Array.Reverse(usrnum) 
         Array.Sort(totalstars) 
         Array.Reverse(totalstars) 
         ForegroundColor = ConsoleColor.Magenta 
         WriteLine("") 
         WriteLine("Values for the chart " & nchrt & " in decending order") 
         WriteLine("") 
         For i As Integer = 0 To usrnum.Length - 1 
          BackgroundColor = ConsoleColor.DarkMagenta 
          ForegroundColor = ConsoleColor.White 
          result1 = (totalstars(i)) 
          result2 = (usrnum(i)) 
          result2.ToString() 
          WriteLine(result1 & result2) 
          WriteLine("") 
         Next i 
         BackgroundColor = ConsoleColor.DarkBlue 
         ForegroundColor = ConsoleColor.White 
         WriteLine("") 
         WriteLine("Please note! Each * Is equal To 100. 
    Once a value reaches over 1000 the maxium amount Of stars shown Is 10 ") 
        Else 
         'displays results as they were typed in and does not sort them in decending order 
         If userchoice2 = "n" Then 
          Clear() 
          ForegroundColor = ConsoleColor.Magenta 
          WriteLine("") 
          WriteLine("Values for the chart " & nchrt) 
          WriteLine("") 

          For i As Integer = 0 To usrnum.Length - 1 
           BackgroundColor = ConsoleColor.Cyan 
           ForegroundColor = ConsoleColor.Black 
           result1 = (totalstars(i)) 
           result2 = (usrnum(i)) 
           result2.ToString() 
           WriteLine(result1 & result2) 
           WriteLine("") 
          Next i 
          BackgroundColor = ConsoleColor.DarkBlue 
          ForegroundColor = ConsoleColor.White 
          WriteLine("") 
          WriteLine("Please note! Each * Is equal To 100. 
    Once a value reaches over 1000 the maxium amount Of stars shown Is 10 ") 
         Else 
          If userchoice2 <> "n" Or userchoice2 <> "y" Then 
           WriteLine("Sorry that choice is not reconised, please enter a choice of y or n.") 
          End If 
         End If 
        End If 
       Loop 
      Else 
       Clear() 
       If userchoice < 1 Or userchoice > 2 Then 
        WriteLine("That value is unknown!") 
        WriteLine("Please enter a choice of 1 or 2 only. ") 
        WriteLine("Press enter to return to the main menu.") 
        ReadLine() 
        Clear() 
       Else 
        If userchoice = 2 Then 
         WriteLine("you sure you want to exit?") 
        End If 
       End If 
      End If 
     Loop 
     Do Until question1 = "y" Or question1 = "n" 
      'exit or return to main menu 
      ForegroundColor = ConsoleColor.Magenta 
      WriteLine("         ") 
      WriteLine("         ") 
      WriteLine("         ") 
      WriteLine("Would you Like To Exit the program?") 
      WriteLine("") 
      WriteLine("y = Exit") 
      WriteLine("n = return to the main menu") 
      Try 
       question1 = ReadLine() 
       Clear() 
      Catch ex As Exception 
       'if this produces an error, for example a string is entered by the user then the following message will be issued to the user 
       WriteLine("That value is unknown!") 
       WriteLine("Please enter a choice of y or n") 
      End Try 
      If question1 <> "y" Or question1 <> "n" Then 
       WriteLine("That value is unknown!") 
       WriteLine("Please enter a choice of y or n") 
       ReadLine() 
       Clear() 
      End If 
     Loop 
    Loop 
End Sub 
'function that is meant to calculate the largest value in the given usrnum() array however I can't get it to work at the moment :(
Public Function Max(ByVal size As Integer) As Integer 
    Dim usrnum(0) As Integer 
    usrnum(0) = size 
    Dim lstsize As Integer = usrnum.Length - 1 'how many items are in the list 
    ReDim usrnum(lstsize) 
    Dim i As Integer = 0 'counter to enable all the values within the array to be compared 
    Dim maxium As Integer 
    maxium = 0 'gives a value for all the values within the array to be compared to 

    Do While (i <= lstsize - 1) 'do while still within the size of the array - prevents it from checking values which do not exsist 
     If usrnum(i) > maxium Then 
      maxium = usrnum(i) ' if the current value is bigger than the current maxium then the new bigger value replaces the maxium 
     End If 
     i += 1 'enables the counter to work 
    Loop 
    Return maxium 'returns the final value of maxium 
End Function 
End Module 
+0

私は、ウィンドウの幅に合わせて最大の出力を伸ばしたいと思っています。他の人たちは、そのまま彼らを残したかったのです。現在、私が持っている機能は正しく動作しませんが、私はそれをコメントアウトしていますので、どのように動作するのかまだ分かります。 – user1988

+0

あなたは本当にvbaを意味しますか、またはvbs、vb6またはvb.netを意味しましたか? –

+0

私はコンソールの幅(ajustable)を照会する方法はわかりませんが、(consolewidth - text)/ 2)左と右に置く星の数を計算することによって、あなたの* stars *出力を少なくとも単純化することができます。次に[PadLeft](https://msdn.microsoft.com/en-us/library/92h5dc07%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet- 1)と[PadRight](https://msdn.microsoft.com/en-us/library/34d75d7s%28v=vs.110%29.aspx)を参照してください。 – Filburt

答えて

2

Console.WindowWidthプロパティを照会し、出力を調整することができます。

関連する問題