Jump to content

Need help with loop

cjxxvll27

So my teacher taught us loops (in Java)  today and i don't think i fully understood what was going on. (AP Comp Sci)

He gave us this assignment and I'm struggling a lot on it and i was wondering if you guys could help me out with it.

Here's the Assignment :

The program in LoveCS.java prints “I love Computer Science!!” 10 times.  Copy it to your directory and compile and run it to see how it works.  Then modify it as follows:

 

// ****************************************************************

//   LoveCS.java

//   Use a while loop to print many messages declaring your

//   passion for computer science

// ****************************************************************

 

public class Lov

{

    public static void main(String[] args)

    {

         final int LIMIT = 10;

         int count = 1;

         while (count <= LIMIT)

{

               System.out.println("I love Computer Science!!");

               count++;

         }

    }

}

1.   Instead of using constant LIMIT, ask the user how many times the message should be printed.  You will need to declare a variable to store the user's response and use that variable to control the loop.  (Remember that all caps is used only for constants!)

 

2.   Number each line in the output, and add a message at the end of the loop that says how many times the message was printed. So if the user enters 3, your program should print this:

 

1 I love Computer Science!!

  2 I love Computer Science!!

  3 I love Computer Science!!

  Printed this message 3 times. 

 

This is what I have so far:

 

 import java.util.Scanner;  // Import the Scanner class

public class LoveCS
{
public static void main(String[] args)
{
  Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("How many times do you want the message to print?");

    Integer AmountOfMessages = myObj.nextInt();  // Read user input
     
   final int LIMIT = AmountOfMessages;
  int count = AmountOfMessages;
  while (count == LIMIT)
 
{
    System.out.println("I love Computer Science!!");
    count++;
    System.out.println("Printed this message " + AmountOfMessages + " times");
  }
}
}

Link to comment
Share on other sites

Link to post
Share on other sites

Consider using a for loop. A while loop is for when you don't know when the loop should end, e.g. when you're asking for user input until they use a certain letter.. That is unpredictable. A number the user gives is predictable.

That kind of cleans up your code a bit (removes the count++ in the loop).

 

Consider that a loop runs a certain piece of code that is in the loop, whenever the loop is ran.

You probably don't want it to say "printed this message X amount of times" multiple times, just once.

 

This is probably what you're looking for:

// Ask user for input here..
final int LIMIT = UserInput;

for(i = 0; i < LIMIT; i++)
{
  	// Add variable 'limit' to the beginning of the line. That should print out:
  	// 1. I love Computer Science
  	// 2. I love Computer Science
  	// etc..
	System.out.println(LIMIT + ". I love Computer Science!");
}
// Only say the amount of times the loop ran *after* running the loop multiple times. 
System.out.println("Printed this: " + LIMIT + " times.";

 

I see the sample code you got uses a while loop, but I really don't want to pretend like that's okay for this purpose :P 

Of course it works, but it's definitely not clean programming..

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Thank youuuu I finally got it to work

So the last part of the assignment is to count the numbers from 1 to whatever number the user entered and get the sum of those numbers

 

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, cjxxvll27 said:

Thank youuuu I finally got it to work

So the last part of the assignment is to count the numbers from 1 to whatever number the user entered and get the sum of those numbers

 

Keep in mind I don't have all the context of your assignment, so I don't know what user entered numbers you're talking about.

You can go about this in two ways:

1. make an int at the beginning of your program and add all user input numbers to that int (and print out that int at the end)

2. add each number input to an array and count up the array at the end

 

The better solution will depend on the context of your program..

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×