- #1
smilesofmiles
- 19
- 0
I am having a difficult time with methods in java. I've been able to get my program to run correctly without methods. My for loop does as intended by printing an alternating rectangular pattern with no separator at the ends. However, after trying to decompose my code into 3 methods I've hit a wall. Any hints, suggestions or help would be greatly appreciated.
Code:
package patternmakerwithmethods;
import java.util.Scanner;
public class PatternMakerWithMethods {
public static void getNumbers(int userRows, int userColumns){
Scanner in = new Scanner(System.in);
userRows = in.nextInt();
userColumns = in.nextInt();
}
public static void getStrings(String starterStr, String secondStr, String separator){
Scanner in = new Scanner(System.in);
starterStr = in.nextLine();
secondStr = in.nextLine();
separator = in.nextLine();
}
public static void createPattern(int i, int j){
for(i = 1; i <= userRows; i++){
for(j = 1; j <= userColumns; j++){
if(((i + j) % 2) == 0){
System.out.print(starterStr);
}
else{
System.out.print(secondStr);
}
if (j != userColumns){
System.out.print(separator);
}
}
System.out.println("");
}
}
public static void main(String[] args) {
int userRows = getNumbers("Please enter an integer value between 1 and 10 inclusive for the amount of rows.");
int userColumns = getNumbers("Please enter another value between 1 and 10 inclusive amount of columns.");
String starterStr = getStrings("Please enter a string for the starting pattern.");
String secondStr = getStrings("Please enter a second string for the pattern.");
String separator = getStrings("Please enter a string separator.");
createPattern(userRows, userColumns, starterStr,secondStr, separator);
}
}