Question:
Java programing - Loops and Arrays HELP!?
2011-09-29 12:55:46 UTC
This is my assignment:

Your program should present a canvas to the user, and then repeatedly prompt the user for characters to add to the canvas, and where to add them.

To initialize the canvas, the program should ask how large of a canvas (how many characters wide and high) the user wants.
It should loop repeatedly, and each time through the loop it should:
Ask the user if they are finished painting. If so, the program should exit.
Ask the user which part of the canvas they would like to fill in.
Ask the user what letter or character they would like to paint in that part of the canvas.
Update the canvas, as per the user's instructions.
"Re-draw" (print to the screen) the updated version of the canvas.
If the user enters an incorrect part of the canvas (outside the range of possible values), the program should not crash or exit. It should print an error message, and then continue to prompt for a valid part of the canvas


and this is what I have so far:
import java.util.Scanner;
public class Main {


public static void main(String[] args) {

Scanner keys = new Scanner(System.in);


System.out.println("Please enter the height of your canvas");
int height = keys.nextInt();
System.out.println("Please enter the width of your canvas");
int width = keys.nextInt();

char[][]Canvas = new char[height][width];
for(int i=0; i < height; i++)
{
for(int j=0; j < width; j++)
Canvas[i][j] = ' ';
}
int row = 0;
while(row {
if(row==0 || row ==height-1)
{
for(int col = 0; col {
System.out.print("*");
}
}
else
{
System.out.print("*");
for(int col=0; col < width; col++){
System.out.print(Canvas[row][col]);
}
System.out.print("*");
}
System.out.println();
row++;
}

System.out.println("Do you want to continue Picasso? (Y/N)");
String answer = keys.next();


{
if (answer.equalsIgnoreCase("N"))
{
System.out.println("Adios!");
System.exit(0);
}
else
{
System.out.println("What position would you like it in?"
+ " Enter the x displacement, followed by a space"
+ " and then the y displacement");
int x = keys.nextInt();
int y = keys.nextInt();
System.out.println("What character would you like to use?"
+ " Enter it now: (Pick a good one!)");
String s = keys.next();

Canvas[x][y] = s.charAt(0);
row = 0;
while(row {
if(row==0 || row ==height-1)
{
for(int col = 0; col {
System.out.print("*");
}
}
else
{
System.out.print("*");
for(int col=0; col < width; col++){
System.out.print(Canvas[row][col]);
}
System.out.print("*");
}
System.out.println();
row++;

}
}



I can't get it to continue looping to ask the question about resuming or keep printing what the user has already inputted. HELP PLEASEEE!
Three answers:
failla
2016-11-11 00:37:37 UTC
Recursion works, while you're no longer mushy with it right this is an untested attempt at an trouble-free iterative version. i attempted to decrease the conditional nesting, yet i might have messed it up by using attempting to be slick. additionally if the comparable variety looks two times in the array then whilst the pairing 10-10 is reached the sum of the style is seen to be 10 rather of 0 or 20. no longer somewhat confident what the final habit is for this difficulty I in basic terms took a wager on the grounds that by using the given wording 10 might desire to be between 10 and 10. EDIT: I replaced the initialization of maxSum to MIN_VALUE so as that it will paintings for all arrays with a great number of adverse integers. int[] array = new int[10]; int maxSum = Integer.MIN_VALUE; int tempSum = 0; int num1, num2; for (int i = 0; i < array.length; i++) { for (int j = i + a million; j < array.length; j++) { num1 = array[i]; num2 = array[j]; int step = ( num1 < num2 ? a million : -a million ); // come to a variety no be counted if to step up or down tempSum = 0; for ( int ok = num1; ok != num2; ok += step ) { // no longer often a remarkable thought to ascertain for precise experience. tempSum + ok; } tempSum += num2 // Pulled exterior the loop to guard pairings of the comparable integer. maxSum = ( tempSum > maxSum ? tempSum : maxSum ); } device.out.print("optimal sum chanced on: " + maxSum);
some guy named mike
2011-09-29 13:25:26 UTC
you need to figure out what causes the loop, the condition.

in this case, it loops if the user doesn't answer no



answer = ""

while ( answer != no ) {

  - ask for position

  - ask for character

  - add to the canvas[][]

  - print out canvas[][]

  - ask if user wants to continue -> answer

}
mr. mister
2011-09-29 12:57:41 UTC
This is why I quit Computer Science.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...