necesito su ayuda para un proyecto donde debo usar un inputbox para colocar un numero que va a ir decreciendo y usar un Msgbox para que muestre ese decrecimiento en forma de triangulo, ademas de estar bien centrado así:
15
14 15
13 14 15
12 13 14 15
11 12 13 14 15
10 11 12 13 14 15
9 10 11 12 13 14 15
8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15
6 7 8 9 10 11 12 13 14 15
5 6 7 8 9 10 11 12 13 14 15
4 5 6 7 8 9 10 11 12 13 14 15
3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
El codigo que use es este, me hace la forma pero, los números no me salen bien centrados ¿Qué puedo hacer?, ¿Como seria la condicion?
Dim nume As Integer
Dim tri, spe, nose As String
tri = " "
nume = InputBox("ingrese el numero", "entrada de datos")
While (nume < 0)
MsgBox("ingrese solo valores positivos")
nume = InputBox("Ingrese solo números mayores a 0")
End While
Next
For n As Integer = 0 To nume
For i As Integer = n To 1 Step -1
If nume < 10 Then
tri = tri & n - i + 1 & " "
Else
tri = tri & nume - i + 1 & " "
End If
Next
tri += vbNewLine
Next
MsgBox(tri)
End Sub
Cita de: zethli en 3 Junio 2020, 06:31 AM
necesito su ayuda para un proyecto donde debo usar un inputbox para colocar un numero que va a ir decreciendo y usar un Msgbox para que muestre ese decrecimiento en forma de triangulo, ademas de estar bien centrado así:
15
14 15
13 14 15
12 13 14 15
11 12 13 14 15
10 11 12 13 14 15
9 10 11 12 13 14 15
8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15
6 7 8 9 10 11 12 13 14 15
5 6 7 8 9 10 11 12 13 14 15
4 5 6 7 8 9 10 11 12 13 14 15
3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
El codigo que use es este, me hace la forma pero, los números no me salen bien centrados ¿Qué puedo hacer?, ¿Como seria la condicion?
Dim nume As Integer
Dim tri, spe, nose As String
tri = " "
nume = InputBox("ingrese el numero", "entrada de datos")
While (nume < 0)
MsgBox("ingrese solo valores positivos")
nume = InputBox("Ingrese solo números mayores a 0")
End While
Next
For n As Integer = 0 To nume
For i As Integer = n To 1 Step -1
If nume < 10 Then
tri = tri & n - i + 1 & " "
Else
tri = tri & nume - i + 1 & " "
End If
Next
tri += vbNewLine
Next
MsgBox(tri)
End Sub
Siempre se te va a ver mal si usas eso ya que el Espacio (" ") no tiene el mismo tamaño que cualquier numero.
Tendrias que rellenar con 0 y al final igual te dara error/ bug por las siguientes razones:1= Que pasaria si meto Letras en el InputBox?
2= Se veria super mal si colocas numeros de mas del 100 en adelante.
Bueno te reestructure el codigo . Solucionando todos esos problemas Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Max As String = String.Empty
Dim Min As String = 1
Dim Input As String = InputBox("Text:")
If Input <> "" Then
Max = Input
If Regex.IsMatch(Max, "^[0-9 ]+$") Then
Dim Filling As Integer = Max.Length
Dim Result As New StringBuilder
Dim Progress As String = Max
For i As Integer = Min To Max
Progress -= 1
Result.Append(GetProgresive(Progress, Max, Filling) & vbNewLine)
Next
MsgBox(Result.ToString) 'Resultado Final
Else
MsgBox("Porfavor Use Solo Numeros")
End If
End If
End Sub
Private Shared Function GetProgresive(ByVal Min As Integer, ByVal Max As Integer, ByVal FillingCount As Integer, _
Optional ByVal FillingString As String = "0") As String
Dim s As String = String.Empty
Dim sb As New StringBuilder
For i As Integer = Min To Max
Dim CalculateSpace As Integer = FillingCount - i.ToString.Length
sb.Append(GenerateSpaces(CalculateSpace, FillingString) & i & " ")
Next
Return sb.ToString()
End Function
Private Shared Function GenerateSpaces(ByVal len As Integer, ByVal FillingStr As String) As String
Dim sb As New StringBuilder
For i As Integer = 1 To len
sb.Append(FillingStr)
Next
Return sb.ToString()
End Function
Se vería así : (https://i.ibb.co/yS24mXn/a1.png)
(https://i.ibb.co/S73M7Bm/A2.png)
Comenta Cualquier cosa si necesitas ayuda
Saludos,
- Si se trata de ejercicios de introducción a la programación con sentencias básicas podrías corregir tu código en la parte del If ya que no estás agregando los espacios iniciales si el nume es menor a 10, es más no estás comparando ni mostrando nume correctamente XD:
Dim nume As Integer
Dim tri As String
tri = " "
nume = InputBox("ingrese el numero", "entrada de datos")
While (nume < 0)
MsgBox("ingrese solo valores positivos")
nume = InputBox("Ingrese solo números mayores a 0")
End While
For n As Integer = 0 To nume
For i As Integer = n To 1 Step -1
If nume - i + 1 < 10 Then
tri = tri & " " & nume - i + 1 & " "
Else
tri = tri & nume - i + 1 & " "
End If
Next
tri += vbNewLine
Next
MsgBox(tri)
- Lo digo porque algunas veces el profesor pide un algoritmo sencillo de muestra de acuerdo a lo aprendido en clase, es como si se llevara Paint y el profesor pidiera dibujar una casa, y de pronto aparece un alumno que le trae un plano 3D en AutoCAD XD.
Muchas gracias, me han salvado la vida.