[Java] First of all, there\'s a tester in the link. Please check your code with
ID: 3810722 • Letter: #
Question
[Java]
First of all, there's a tester in the link. Please check your code with the tester. Everything including the picture object will be provided in that link. Thank you.
http://www.codecheck.it/files/1612272035e710807wpz0ef8qxlmq9diqx2
Write an application RowsOfCats to do the following: Ask the user to enter a number between 5 and 10 (inclusive) Use this prompt: System.out.print("Enter a number between 5 and 10: ");
Keep asking until the user enters a number in the correct range. Do not throw an exception if the user enters a non-integer. Just consume the bad data and ask again.
If the user enters 6, the drawing will look like this:
[Here's my code]
import java.util.Scanner;
public class RowsOfCats
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int range = 0;
while (range < 5 || range > 10)
{
System.out.print("Enter a number between 5 and 10: ");
number = scan.nextInt();
}
System.out.print(number);
Picture pic = new Picture("cat4.gif");
for (int i = 0; i < number; i++)
{
for (int j = 0; j < i; j++)
{
pic.draw();
}
pic.draw();
}
}
}
There's a tester in the link. Please check your code with the tester. Everything including the picture object will be provided in that link. Thank you.
http://www.codecheck.it/files/1612272035e710807wpz0ef8qxlmq9diqx2
Explanation / Answer
import java.util.Scanner;
public class RowsOfCats
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String range = null;
int number = 0;
do{
try
{
System.out.print("Enter a number between 5 and 10: ");
range = sc.nextLine();
number = Integer.parseInt(range);
}
catch(NumberFormatException er)
{
continue;
}
}while(number < 5 || number >10);
System.out.println(number);
Picture pic = new Picture("cat4.gif");
for(int i=0;i<=number;++i){
pic.draw();
}
}
====
For the Input Validation I have done : Tests are passing
For Pictures you need to provide, How do we acess Image and print them.
Check it
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.