• 0

Visual Basic - Making 1000 = "*"


Question

Need help making every 1000 = "*"

attach is what the final result should look like
This is my code:

Public Class Form1
 
    Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
 
        'Number of planets before Pluto was "let go"'
        Const intNUM_PLANETS As Integer = 9
 
        'Planet names'
        Dim strArrayPlanetNames() As String =
        {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
 
        'diameter of each planet in miles'
        Dim dblArrayPlanetSizes() As Double =
        {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
 
 
        For intX = 0 To intNUM_PLANETS - 1
 
            'output name and size'
            lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
 
            'output bar chart on next line'
        Next
        
    End Sub

post-503111-0-56641300-1376880908.png

Link to comment
https://www.neowin.net/forum/topic/1171589-visual-basic-making-1000/
Share on other sites

15 answers to this question

Recommended Posts

  • 0

ermm is it something like this? this does not work but like what code should i add inside the lstPlanets.items.add(.....)

Option Strict On
Option Explicit On
'Anthony L
'CS 115
'Summer 2013
 Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
 
        'Number of planets before Pluto was "let go"'
        Const intNUM_PLANETS As Integer = 9
 
        'Planet names'
        Dim strArrayPlanetNames() As String =
        {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
 
        'diameter of each planet in miles'
        Dim dblArrayPlanetSizes() As Double =
        {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
        Dim stars = New String("*"c, 1) 'stars = "*****"
 
        For intX = 0 To intNUM_PLANETS - 1
 
            'output name and size'
            lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
            lstPlanets.Items.Add(stars)
        Next
 
 
    End Sub
  • 0

Note: psuedo code below:

Const intNUM_PLANETS As Integer = 9

Dim strArrayPlanetNames() As String =
   {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
Dim dblArrayPlanetSizes() As Double =
   {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
 
For intX = 0 To intNUM_PLANETS - 1
   lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
  
   Dim numberOfStars As ??? = dblArrayPlanetSizes(intX))/1000   //??? - select variable type without decimal places
   for intY = 0 To numberOfStars
      lstPlanets.Items.Add("*")
   Next

Next
  • 0
Take the time to think about your problem, the data you have and in which form it is available. There's no point trying to get code examples to paste into your assignment if you don't understand how to reason about solving your problem. The code I gave you was not meant to be pasted verbatim into your assignment, but simply to illustrate how to create a string containing a repeating character.

 

Here's your problem in words:

For every planet you need to print its name and a certain amount of stars that corresponds to its size divided by 1000 and rounded up.

 

The planet names are in the array strArrayPlanetNames. The sizes are in the array dblArrayPlanetSizes.

 

For each planet, you'll first need to get its name and size from these arrays. Printing the name is easy. Printing the stars consists in

  1. finding the number of stars to print (i.e. the size divided by 1000 and rounded up)
  2. creating a string containing that many stars (I've showed you how already)
  3. printing that string to the output
 

From there it's only a matter of language syntax. Do you not understand what is an array, or how to get an element from an array? Or how to perform a division? Do you not understand what a for loop is? It's not clear where exactly lies the source of your confusion.

  • Like 1
  • 0

The best way to learn is by troubleshooting. Come up with a flowchart first, then write pseudo code for each block. This will makes things a lot clearer when writing code (at first).

 

If you just copy someone else's code, you will forget it after a few minutes.

  • 0

Yeah that's true but i do not learn a lot since this is an online class and the teacher never teach us or told us to read page something so i always google and read by myself which is kinda hard for me and there are several things that i have not learn maybe? or not? and yes it's very hard for me to know these codes... and btw 68k i tried using your code but then it multiply a lot of times and i do not really get the term of IntY = 0 so do we like start from the first number or?

this is what i have so far:

For intX = 0 To intNUM_PLANETS - 1
 
                'output name and size'
                lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
            Dim numberOfStars As Double = dblArrayPlanetSizes(CInt((intX) / 1000))
            For intY = 0 To numberOfStars
                lstPlanets.Items.Add("*")
            Next
 
        Next
  • 0

Yeah that's true but i do not learn a lot since this is an online class and the teacher never teach us or told us to read page something so i always google and read by myself which is kinda hard for me and there are several things that i have not learn maybe? or not? and yes it's very hard for me to know these codes... and btw 68k i tried using your code but then it multiply a lot of times and i do not really get the term of IntY = 0 so do we like start from the first number or?

this is what i have so far:

For intX = 0 To intNUM_PLANETS - 1

 

                'output name and size'

                lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))

            Dim numberOfStars As Double = dblArrayPlanetSizes(CInt((intX) / 1000))

            For intY = 0 To numberOfStars

                lstPlanets.Items.Add("*")

            Next

 

        Next

What is CInt((intX) / 1000) supposed to be? It's the array index divided by 1000 and converted to an integer - which it already is. The value of this expression will always be 0. This doesn't make sense. You're not thinking about what your values represent.

 

Then you create a variable named numberOfStars which you assign to one of the values of dblArrayPlanetSizes array. This doesn't make sense: this array contains planet sizes. If you take an element from it, this element is a planet size, not a number of stars.

 

Also, you don't want to add each star as a separate list item; this will add each star on its own line. Create the string as I showed you and then add it as a single item to the list.

 

You want to 1) get the planet size from the array, 2) divide that size by 1000; 3) use the result of that division as the number of stars to create your string.

 

That's three sub-problems to solve. You know how to solve each of these individually; every one is just a single basic of code. Here are some hints (replace the comments with your code):

 

Dim planetSize = '... get the size from the array'
Dim numberOfStars = '... divide that size by 1000 and round up'
Dim stars = New String("*"C,  '... use the calculated number of stars to specify how many stars to put'
lstPlanet.Items.Add('... add that string the output'

By the way please use code tags when you post code.

  • 0

+Asik

I manage to get it working but is there another way to round this up?

 
               lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
            Dim numberOfStars As Double = Math.Round(dblArrayPlanetSizes(intX) / 1000) '... divide that size by 1000 and round up'
            Dim stars = New String("*"c, CInt(numberOfStars))  '... use the calculated number of stars to specify how many stars to put'
            lstPlanets.Items.Add(stars + "*") '... add that string the output'
  • 0

... why would you create a new duplicate array containing the planet sizes? How could you then divide that array by 1000? That wouldn't even compile.

 

The snippet I gave you goes inside your for loop. Use the loop index variable to access the existing planet size array you have, and retrieve a single planet size at a time.

 

By the way, the code you post often doesn't even compile. If you're working inside an IDE like Visual Studio, you should see red squiggles under the problematic lines and errors listed below. You don't need people on a forum to tell you your code doesn't work when Visual Studio already tells you that. Always make sure your code compiles and runs before anything else.

  • 0

 

Yeah that's true but i do not learn a lot since this is an online class and the teacher never teach us or told us to read page something so i always google and read by myself which is kinda hard for me and there are several things that i have not learn maybe? or not? and yes it's very hard for me to know these codes... and btw 68k i tried using your code but then it multiply a lot of times and i do not really get the term of IntY = 0 so do we like start from the first number or?

this is what i have so far:

For intX = 0 To intNUM_PLANETS - 1
 
                'output name and size'
                lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
            Dim numberOfStars As Double = dblArrayPlanetSizes(CInt((intX) / 1000))
            For intY = 0 To numberOfStars
                lstPlanets.Items.Add("*")
            Next
 
        Next

 

From http://msdn.microsoft.com/en-us/library/vstudio/5z06z1kb.aspx:

 

You use a For...Next structure when you want to repeat a set of statements a set number of times.

In the following example, the index variable starts with a value of 1 and is incremented with each iteration of the loop, ending after the value of index reaches 5.

For index As Integer = 1 To 5
    Debug.Write(index.ToString & " ")
Next
Debug.WriteLine("")
' Output: 1 2 3 4 5

Every time the For loop is initiated the variable "index" would be reset to "1".

  • 0

i tried it and it work well :|

Why are you surprised? IMO the method you're following doesn't seem to teach you the fundamentals right; you don't really understand what you're doing and just rushing through exercices. You'll hit a wall pretty soon at that rate. Get a good book and read it carefully. Learn to:

 

 - understand compiler error messages and how to find information about them

 - step through your code in the debugger so you can visualize what your program is doing at every statement

 

With good foundations you shouldn't need to ask people to guide you every step of the way, and then wonder how the program ended up working. Unfortunately, many courses are just plain bad, so you need to get additional resources by yourself. When I learned C++ I bought myself a huge tutorial book and read it on the bus; the material shown in class and exercices was insufficient by far.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Weekend PC Game Deals: Anno 117, Final Fantasy VII, Rematch, and more by Pulasthi Ariyasinghe Weekend PC Game Deals is where the hottest gaming deals from all over the internet are gathered into one place every week for your consumption. So kick back, relax, and hold on to your wallets. The Epic Games Store's mystery giveaways may have ended, but its regular freebies didn't miss a step this week. The double drop was for copies of Warhammer 40K Speed Freeks and The Ouroboros King. Speed Freeks lands for multiplayer racing fans, but with plenty of competitive shooting elements too. You will be piloting Ork buggies, tanks, and aircraft modeled after the popular tabletop miniatures while trying to complete objectives and pass finish lines. Next, Ouroboros King is a crossover between chess and tactical roguelikes, offering the chance to create your own army with special rules to beat incoming foes on the board. The double giveaway on the Epic Games Store will be available until June 11, and replacing it will be Citizen Sleeper and ROBOBEAT. The Humble Store brought a new charity bundle to check out this week too. Landing with the name The Complete Inkle Library, this is a large collection of interactive narrative puzzle games from the publisher Inkle. This begins with Heaven's Vault, four parts from the Sorcery series, 80 Days, Overboard, and Pendragon: Narrative Tactics within the starting tier for $9. Hopping up a step to the $12 tier gets you TR-49, Expelled, and A Highland Song for paying at least $12. If you go for the $20 tier, you get four e-books from the Heaven's Vault series. The bundle has almost three weeks on its counter before it goes away. Big Deals There is a larger than normal amount of weekend specials happening this time, including multiple publisher deals, franchise discounts, and indie gems to grab. With those and more, here's our hand-picked big deals list for the weekend: Anno 117: Pax Romana – $44.99 on Steam Clair Obscur: Expedition 33 – $39.99 on Steam Timberborn – $27.99 on Steam EARTH DEFENSE FORCE 6 – $26.39 on Steam Rust – $19.99 on Steam FINAL FANTASY VII REBIRTH – $19.99 on Steam Street Fighter 6 – $19.99 on Steam Returnal – $19.79 on Steam Shape of Dreams – $17.49 on Steam Far Cry 6 – $14.99 on Steam Assassin's Creed Valhalla – $14.99 on Steam Quarantine Zone: The Last Check – $14.99 on Steam REMATCH – $14.99 on Steam EA SPORTS FC 26 – $13.99 on Steam FINAL FANTASY VII REMAKE INTERGRADE – $13.99 on Steam Magicraft – $12.79 on Steam Cult of the Lamb – $12.49 on Steam Dying Light 2: Reloaded Edition – $11.99 on Steam Cuphead – $11.99 on Steam Assassin's Creed Odyssey – $11.99 on Steam Hunt: Showdown 1896 – $11.99 on Steam Sektori – $11.99 on Steam Just Shapes & Beats – $11.99 on Steam Gunfire Reborn – $10.99 on Steam 33 Immortals – $9.99 on Epic Store Baby Steps – $9.99 on Steam Sifu – $9.99 on Steam Hearts of Iron IV – $9.99 on Steam DREDGE – $9.99 on Steam DAVE THE DIVER – $9.99 on Steam Pacific Drive – $9.89 on Steam Mycopunk – $9.74 on Steam Sons Of The Forest – $8.99 on Steam Jotunnslayer: Hordes of Hel – $8.99 on Steam Nuclear Throne – $8.99 on Steam Mechabellum – $8.99 on Steam Deep Rock Galactic: Survivor – $8.44 on Steam TerraTech Legion – $7.99 on Steam Inscryption – $7.99 on Steam Assassin's Creed Unity – $7.49 on Steam Minishoot' Adventures – $7.49 on Steam The Stanley Parable – $7.49 on Steam Oxygen Not Included – $7.49 on Steam Megabonk – $6.99 on Steam Look Outside – $5.99 on Steam Vampire Hunters – $5.24 on Steam MOTHERGUNSHIP – $4.99 on Steam My Friend Pedro – $3.99 on Steam The Messenger – $3.99 on Steam Vampire Survivors – $3.74 on Steam Brotato – $2.99 on Steam Enter the Gungeon – $2.99 on Steam Loop Hero – $2.99 on Steam GRIS – $2.99 on Steam Exit the Gungeon – $2.49 on Steam Hitman: Absolution – $1.99 on Steam CARRION – $1.99 on Steam Don't Starve Together – $1.49 on Steam Golf With Your Friends – $1.49 on Steam Hotline Miami – $0.99 on Steam The Ouroboros King – $0 on Epic Store Warhammer 40K Speed Freeks – $0 on Epic Store DRM-free Specials Hopping over to the DRM-free deals, the GOG store has plenty of discounts running this weekend too. Here are some highlights: Fallout 4: Game of the Year Edition - $15.99 on GOG Fallout: New Vegas Ultimate Edition - $9.99 on GOG Disco Elysium - The Final Cut - $9.99 on GOG Crysis - $9.99 on GOG Tyranny - Standard Edition - $7.49 on GOG Frostpunk: Game of the Year Edition - $7.35 on GOG Banished - $6.79 on GOG Fallout 3: Game of the Year Edition - $6.59 on GOG The Forgotten City - $6.25 on GOG The Age of Decadence - $5.99 on GOG SimCity 3000 Unlimited - $4.99 on GOG Assassin's Creed: Director's Cut - $4.99 on GOG SimCity 4 Deluxe Edition - $3.99 on GOG Vampyr - $3.99 on GOG Torchlight II - $3.99 on GOG Deus Ex GOTY Edition - $3.49 on GOG Primordia - $3.09 on GOG Theme Hospital - $2.99 on GOG SimCity 2000 Special Edition - $2.99 on GOG Total Annihilation: Kingdoms + Iron Plague - $2.99 on GOG Deus Ex: Human Revolution - Director’s Cut - $2.99 on GOG Master of Orion 1+2 - $2.39 on GOG Prince of Persia: The Sands of Time - $1.99 on GOG Prince of Persia: Warrior Within - $1.99 on GOG EVERSPACE - $1.99 on GOG Total Annihilation: Commander Pack - $0.99 on GOG Keep in mind that availability and pricing for some deals could vary depending on the region. That's it for our pick of this weekend's PC game deals, and hopefully, some of you have enough self-restraint not to keep adding to your ever-growing backlogs. As always, there are an enormous number of other deals ready and waiting all over the interwebs, as well as on services you may already subscribe to if you comb through them, so keep your eyes open for those, and have a great weekend.
    • When will the Photos app be updated to remember the window size and position when reopened? They addressed this issue in a 2024 version of the app (though I can't recall the build number). Unfortunately, after that specific version, the problem persists! Please prioritise this fix in your K2 schedule. Additionally, the Snipping Tool has lost the ability to capture the Windows Taskbar starting from the 2024 version!
    • Same, never saw it on Android or iOS. Guess only some people got it *shrugs*
    • Anthropic pulls Fable 5 and Mythos 5 after US export control order by Pradeep Viswanathan In April this year, Anthropic launched the Claude Mythos Preview frontier model with state-of-the-art cyber and coding capabilities for a select set of companies around the world. After preparing appropriate guardrails, early this week, Anthropic launched Claude Fable 5 and Mythos 5, its most capable AI models. Claude Fable 5 is for general users and comes with strict safeguards, while Mythos 5 is designed with fewer safeguards for cybersecurity and biology use cases. Today, Anthropic abruptly suspended access to its Fable 5 and Mythos 5 AI models for all customers after receiving an export control directive from the US government. The company received the directive from the government today at 5:21 p.m. ET, and the received letter did not provide any details regarding the national security concern. Anthropic understands that the government became aware of a method to bypass, or “jailbreak,” Fable 5, which might be the reason behind the directive. The order was issued under national security authorities and requires the company to suspend all access to Fable 5 and Mythos 5 by any foreign national, whether they are inside or outside the United States. The restriction also applies to foreign national employees working at Anthropic. As a result, the company has disabled both models for all customers to ensure compliance. Access to previous Anthropic models like Opus and Sonnet is not affected by this government order. The company highlighted that it had developed strong safeguards to reduce the possibility that Fable is misused for tasks related to cybersecurity. In fact, many developers are complaining that the safeguards are going overboard. Additionally, the company worked with the US government, the UK AISI, multiple private third-party organizations, and internal teams to red-team Fable’s safeguards for thousands of hours. Finally, Anthropic noted that no testers have yet been able to find a universal jailbreak on Fable 5. As expected, Anthropic disagrees that a narrow potential jailbreak should lead to the recall of a commercial model used by hundreds of millions of people. It warned that applying this standard across the AI industry could effectively halt new frontier model deployments. Anthropic concluded by mentioning that it is working to restore access to Fable 5 and Mythos 5 as soon as possible and plans to share more details within the next 24 hours.
  • Recent Achievements

    • Week One Done
      ssd21345 earned a badge
      Week One Done
    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • Dedicated
      jordanspringer earned a badge
      Dedicated
    • Rookie
      Rimplesnort went up a rank
      Rookie
    • One Year In
      Markus94287 earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      176
    3. 3
      PsYcHoKiLLa
      147
    4. 4
      ATLien_0
      92
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!