#loop or no loop?

1 messages · Page 1 of 1 (latest)

fringe anchor
#

Im meant to make a program that will give the same output as stated in the image. I got it down up till the point where it displays the '*', did it in a particular way my teacher told me.

says not to use lists and so i didn't, however i dont see it possible to get the same results without using a list, and he wants me to keep the portion of code beginning from the inner loop and up the same.

random shardBOT
#

<@&987246399047479336> please have a look, thanks.

random shardBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

fringe anchor
#

class Untitled{
    public static void main(String[] args){
        String asterisk = "";
        int value;
        int howMany;
       
        Scanner stores = new Scanner(System.in);
        System.out.print("how many");
        Scanner many = new Scanner(System.in);
        howMany = many.nextInt();
        
        for(int i = 0; i < howMany; i++ ){
            System.out.print("Enter today's sales for store "+ (i+1)+": $ ");
            value = stores.nextInt();
            for(int j=1;j<=value/100;j++){
                asterisk +=  "*" ;
            }
            asterisk += "\n";
            
            
        }
        System.out.println("SALES BAR CHART");
        
        System.out.println("Each * = 100");
        for(int i = 0; i < howMany;i++){
            System.out.println("Store "+(i+1)+" :$"+asterisk);
        }
        
    }
}```
random shardBOT
# fringe anchor ```import java.util.Scanner; class Untitled{ public static void main(String...

Detected code, here are some useful tools:

Formatted code
import java.util.Scanner;

class Untitled {
  public static void main(String[] args) {
    String asterisk = "";
    int value;
    int howMany;
    Scanner stores = new Scanner(System.in);
    System.out.print("how many");
    Scanner many = new Scanner(System.in);
    howMany = many.nextInt();
    for (int i = 0; i < howMany; i++) {
      System.out.print("Enter today's sales for store " + (i + 1) + ": $ ");
      value = stores.nextInt();
      for (int j = 1; j <= value / 100; j++) {
        asterisk += "*";
      }
      asterisk += "\n";
    }
    System.out.println("SALES BAR CHART");
    System.out.println("Each * = 100");
    for (int i = 0; i < howMany; i++) {
      System.out.println("Store " + (i + 1) + " :$" + asterisk);
    }
  }
}
fringe anchor
#

results below

#

i want it to print starting from store 1, till the one given and their respective asterisks

exotic sluice
#

@fringe anchor you don't need two scanner objects btw

#

Here's what the idea is

#
  1. take input
#
  1. do stuff and find no of asterisk to print
fringe anchor
#

and print the asterisks in their respective stores

exotic sluice
#

does the output have to look exactly the same?

#

and without lists

fringe anchor
#

yeah, he wants it to look like that without lists

#

i dont see how i could do so without including them

exotic sluice
#

i was thinking to print first but now that's out of the equation

#

maybe get variables since no of stores is just 5

#

or did they say no for that too?

fringe anchor
#

said no to that too ;-; ;-;

exotic sluice
#

then the only thing i could think of is to build a string and print it later

fringe anchor
#

i would assume inside the for loop no?

exotic sluice
#

i do have a feeling there should be something better than this tho

fringe anchor
#

Definitely, i wanted to try and do it on my own but ive given up and so resorted to discord or waiting till tomorrow and asking how ;-;

exotic sluice
#

ye you take input and maybe use a StringBuilder to make a string, then store it in a variable or something but then again we are using variablesthinkies

fringe anchor
exotic sluice
#

thing is you have to take 5 inputs

#

and without storing it somewhere you gonna lose that input

#

the best case then would be take input, print stuff and repeat

#

but then console won't look exactly like that

fringe anchor
#

tried that too, asked if it was "acceptable", got flat out rejected

#

so in general, im assuming it isnt really possible unless lists are involved or extra variables?

exotic sluice
#

i mean it's a machine before reading a new value it needs to store the prev one somewhere

#

either use it right away or it's gone

fringe anchor
#

igh, will leave it like this for now and complain tomorrow then PikaThink

exotic sluice
#

maybe they meant something else or my brain isn't functioning normally, i'd love to hear what they had in store if you manage to ask em.

fringe anchor
#

I'll make sure to remember and get back to you

#

do you know if the question stays up till closed ?

#

or does it close automatically ?

exotic sluice
#

it'll stay up for a while after inactivity, hopefully someone else will respond with some insights on this.

plush wolf
#

I'm assuming array would he the way to go

storm bane
#

I think there’s a more efficient method with string builder

fringe anchor
#

that helped out wonders

fringe anchor
#

class Untitled{
    public static void main(String[] args){
        String asterisk = "";
        int value;
        int howMany;
        
        Scanner stores = new Scanner(System.in);
        
        System.out.print("how many: ");
        
        howMany = stores.nextInt();
        
        for(int i = 0; i < howMany; i++ ){
            System.out.print("Enter today's sales for store "+ (i+1)+": $ ");
            value = stores.nextInt();
            asterisk +="Store:" + (i + 1) + ":";
            
            for(int j=1;j<=value/100;j++){
                asterisk +=  "*" ;
            }
            asterisk += "\n";
            
            
        }
        
        System.out.println(asterisk);
        
    }
}```
random shardBOT
# fringe anchor ```import java.util.Scanner; class Untitled{ public static void main(String...

Detected code, here are some useful tools:

Formatted code
import java.util.Scanner;

class Untitled {
  public static void main(String[] args) {
    String asterisk = "";
    int value;
    int howMany;
    Scanner stores = new Scanner(System.in);
    System.out.print("how many: ");
    howMany = stores.nextInt();
    for (int i = 0; i < howMany; i++) {
      System.out.print("Enter today's sales for store " + (i + 1) + ": $ ");
      value = stores.nextInt();
      asterisk += "Store:" + (i + 1) + ":";
      for (int j = 1; j <= value / 100; j++) {
        asterisk += "*";
      }
      asterisk += "\n";
    }
    System.out.println(asterisk);
  }
}
exotic sluice