Q BgQuestion:

      
Pupil
Karma Points: 55
Respect (99%):
posted by  Ruger_Mk2 on 11/3/2009 4:21:34 PM  |  status: Closed  |  Earned Karma: 55

PLZ HELP WILL RATE LIFESAVER!!!! JAVA

Course Textbook Chapter Problem Needs by
N/A N/A N/A N/A 11/3/2009 at 11:00:00 PM
Question Details:
Write a program that reads five integers from the keyboard.  Each integer is between
1 and 10 (you will need to validate the user’s input; if you reject the user’s
input, you must re-read the input until a valid quantity is entered).  For each
input, your program should print a line of dollar signs, of length "x", where x is the
integer value entered. Name the .java file Lab7_Ex1.   
 
Example input/output:  
 
Enter the next number: 7 [enter]
$$$$$$$
Enter the next number: 11 [enter]
That is an invalid number.
Enter the next number:        (the program would continue...)
Bonus Point Alert! Earn +15 additional karma points for helping this platinum member.

AAnswers:

Answer Question Ask for clarification
Oracle
Karma Points: 18,830
posted by Marth on 11/3/2009 6:34:34 PM  |  status: Live
Asker's Rating: Lifesaver   
Response Details:
Let us first write a method to print x number of dollar signs.

public static void printDollarSigns(int number)
{
      for(int x = 0; x < number; x++)
      {
            System.out.print('$');
      }
      System.out.println();
}



Now in our main method, we need to get user input as well as call the printDollarSigns() method for each valid input.

public static void main(String[] args)
{
      // open user input
      Scanner kb = new Scanner(System.in);

      // read user input

      // count will store the number of valid inputs
      int count = 0;

      while(count < 5)
      {
            // get user input
            int input = kb.nextLine();

            // check for validity
            if(input<1 || input>10)
            {
                  // invalid
                  System.out.println("That is an invalid number.");
                  continue;
            }
            // otherwise the number is valid
            else
            {
                  // print dollar signs
                  printDollarSigns(input);

                  // increment count
                  count++;
            }
      }
}


Thank you for rating my response. Feel free to PM me if you have further questions.

Java Genius

"Lois Lane is falling, accelerating at an initial rate of 32ft per second per second. Superman swoops down to save her by reaching out two arms of steel. Ms. Lane, who is now traveling at approximately 120 miles per hour, hits them, and is immediately sliced into three equal pieces." - Sheldon
Oracle
Karma Points: 31,807
posted by rapunzel on 11/3/2009 7:38:19 PM  |  status: Live
Asker's Rating: Helpful   
Response Details:
please rate - thanks

import java.util.*;
public class Lab7_Ex1
{public static void main(String[] args)
  {Scanner in=new Scanner(System.in);
   int n=0,i,j;
   for(i=0;i<5;i++)
    {while(n<1||n>10)
         {System.out.print("Enter the next number: ");
         n=in.nextInt();
            if(n<1||n>10)
               System.out.println("That is an invalid number.");
            }
        for(j=0;j<n;j++)
            System.out.print("$");
        System.out.println();
        n=0;
        }
 
 }
}

Note to all members 1. Please 1 question per post 2. Show respect to your fellow members by rating all answers. 3. When rating remember that the first answer is not necessarily the best answer. 4. When answering questions, explain what you are doing, so that the asker will learn, don't just give a meaningless number. 5. Your answers should be your work. Don't copy from another member, this is Karma abuse and possible disciplinary actions against you. and GOOD LUCK to you all!!
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »