I'm trying to create a program that outputs an alternate tile design from the user's input. I.E. if the use inputs 3 the result would be a 3x3 design that looks like:
|R|B|R|
|B|R|B|
|R|B|R|
I'm having problems with getting the right amount of tiles for the output. For the input of 3, row 2 has an extra "|R|" and a 4th row is subsequently created. The output comes out to:
|R|B|R|
|B|R|B|R|
|R|B|R|
|B
I've attached my code below. I know it has something to do with:
if (r%2 == 0){
System.out.println("|");
System.out.print("|B");
Any thoughts?
import java.util.*;
public class tileFloor {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
System.out.println("Enter x:");
int x;
x = input.nextInt();
if (x < 10)
{ int c = 0;
int r = 0;
while (r < x ){
while (c < x ){
if (c %2 == 0 )
System.out.print("|R");
else if (c%2 != 0)
System.out.print("|B");
c++;
}//end 'while (c<x)' loop
if (r%2 == 0){
System.out.println("|");
System.out.print("|B");
}
else if (r%2 != 0)
System.out.println("|");
c = 0;
r++;
}//end 'while (r<x)' loop
}//end if statement
input.close();
}//end main
}//end class
What about this solution? It's definitly more clear what it does:
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
System.out.print("Enter x: ");
int x = input.nextInt();
if (x < 10) {
int r = x;
int c;
while (r-- > 0) {
c = x;
while (c-- > 0) {
System.out.print("|" + ((c + r & 1) == 0 ? "R" : "B"));
}
System.out.println("|");
}
}
}
}
Try this
import java.util.*;
class tileFloor {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
System.out.println("Enter x:");
int x;
x = input.nextInt();
int count = 0;
if (x < 10)
{ int c = 0;
int r = 0;
while (r < x ){
if(r%2 == 0)
{
count = 0;
}
else
{
count = 1;
}
while (c < x ){
if (count %2 == 0)
{
System.out.print("|R");
}
else
{
System.out.print("|B");
}
count++;
c++;
}//end 'while (c<x)' loop
System.out.println("|");
c = 0;
r++;
}//end 'while (r<x)' loop
}//end if statement
input.close();
}//end main
}//end class
Related
I'm trying to add spaces and a counter between consecutive runs in a simple java coin toss program.
I want this output: HHHHTHTTTTTTTHTTTHHHTTTTHTTHHHTTTTHHTHHHHHTTTTTTHT
to look print like this: HHHH4 T1 H1 TTTTTTT7 H1 TTT3 HHH3 TTTT4 H1 TT2 HHH3 TTTT4 HH2 T1 HHHHH5 TTTTTT6 H1 T1
I am not sure how to position the conditions in the loop so that spaces and a counter are printed between the consecutive 'T's and 'H's. Do I need to use a different kind of loop? I have tried rearranging the loop and using break; and continue; but haven't gotten the result to print correctly.
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("How many times do you want to flip the coin? ");
int timesFlipped = scnr.nextInt();
Random randomNum = new Random();
for (int i=0; i < timesFlipped; i++) {
int currentflip = randomNum.nextInt(2);
int previousFlip = 0;
int tailsCount = 0;
int headsCount = 0;
if (currentflip == 0) {
System.out.print("H");
previousFlip = 0;
headsCount++;
}
else if (currentflip == 1) {
System.out.print("T");
previousFlip = 1;
tailsCount++;
}
if (previousFlip == 0 && currentflip == 1) {
System.out.print(headsCount + " ");
headsCount = 0;
}
else if (previousFlip == 1 && currentflip == 0) {
System.out.print(tailsCount + " ");
tailsCount = 0;
}
}
}
You can just store the last flip and a counter like
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("How many times do you want to flip the coin? ");
int timesFlipped = scnr.nextInt();
Random randomNum = new Random();
int counter = 1;
int previousFlip = randomNum.nextInt(2);
printFlip(previousFlip);
for (int i=1; i < timesFlipped; i++) {
int currentflip = randomNum.nextInt(2);
if (currentflip == previousFlip) {
counter++;
} else {
System.out.print(counter + " ");
counter = 1;
previousFlip = currentflip;
}
printFlip(currentflip);
}
System.out.print(counter);
}
private static void printFlip(int currentflip) {
if (currentflip == 0) {
System.out.print("H");
}
else if (currentflip == 1) {
System.out.print("T");
}
}
Via a single method:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("How many times do you want to flip the coin? ");
int timesFlipped = scnr.nextInt();
if (timesFlipped <= 0)
return;
Random randomNum = new Random();
boolean isHeads = false;
int sequence = 0;
for (int i = 0; i < timesFlipped; i++) {
boolean prevFlip = isHeads;
isHeads = randomNum.nextBoolean();
if (i > 0 && isHeads != prevFlip) {
System.out.print(sequence + " ");
sequence = 0;
}
sequence++;
System.out.print(isHeads ? "H" : "T");
}
System.out.println(sequence);
}
The program works the way it should but gets user's input on whether they want to play again or not and then does nothing after that. I am setting all of the original input values to 0 which is probably why. How can I get a new input from the user when they want to play again. If someone knows a better way make this program without arrays I would greatly appreciate it if you could post the code below.
import java.util.Scanner;
import java.util.*;
public class Shapes {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("You can view any of the following shapes: ");
System.out.print("\n1 Square");
System.out.print("\n2 Right-angle Triangle");
System.out.print("\n3 Pyramid");
System.out.print("\n4 Hourglass");
System.out.print("\n5 Diamond");
System.out.print("\nEnter a integer to choose a shape: ");
String shape = userInput.nextLine();
System.out.print("\nEnter the height of the shape: ");
int inputOne = userInput.nextInt();
System.out.print("Enter a character: ");
char ch = userInput.next().charAt(0);
System.out.println("\n");
do {
if (shape.equalsIgnoreCase("1")) {
square(ch, inputOne);
System.out.println();
}
if (shape.equalsIgnoreCase("2")) {
triangle(ch, inputOne);
System.out.println();
}
if (shape.equalsIgnoreCase("3")) {
pyramid(ch, inputOne);
System.out.println();
}
if (shape.equalsIgnoreCase("4")) {
diamond(ch, inputOne);
System.out.println();
}
if (shape.equalsIgnoreCase("5")) {
hourglass(ch, inputOne);
System.out.println();
}
shape = "0";
inputOne = 0;
ch = 0;
} while (playAgain());
}
private static boolean playAgain() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Play again? (Y/N): ");
String replay = keyboard.nextLine();
return replay.equalsIgnoreCase("Y");
}
public static void square(char c, int n) {
char[] a = new char[n];
Arrays.fill(a, c);
for (; n-- > 0;)
System.out.println(a);
}
public static void triangle(char c, int n) {
for (int i = 1; i <= n; i++) {
char[] a = new char[i];
Arrays.fill(a, c);
System.out.println(a);
}
}
public static void pyramid(char c, int n) {
for (int i = 0; i < n; i++) {
while (true) {
char[] s = new char[n - i - 1];
Arrays.fill(s, ' ');
System.out.print(s);
char[] a = new char[i * 2 + 1];
Arrays.fill(a, c);
System.out.println(a);
}
}
}
public static void diamond(char c, int n) {
boolean odd = n % 2 == 1;
n++;
int mid = n / 2;
int mi = mid;
if (odd)
mi--;
for (int y = 1; y < n; y++) {
for (int x = 1; x < n; x++) {
System.out.print((Math.abs(x + y - n) > mi || Math.abs(x - y) > mi) ? ' ' : c);
}
System.out.println();
}
}
public static void hourglass(char c, int n) {
boolean odd = n % 2 == 1;
if (odd)
n++;
int mid = n / 2;
for (int y = 0; y < n; y++) {
if (odd && y == mid)
continue;
for (int x = 1; x < n; x++) {
int a = 0;
if (Math.abs(x + y - mid) >= mid)
a++;
if (Math.abs(x - y - mid) >= mid)
a++;
System.out.print((a % 2 == 0) ? c : ' ');
}
System.out.println();
}
}
}
You only prompt the user for input once and you set them to 0. On the next iteration of the loop, shape is 0 so none of the if statements evaluate to true and thus your program does nothing.
Retrieve the user's input inside the loop and remove zeroing-out the variables:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
do {
System.out.print("You can view any of the following shapes: ");
System.out.print("\n1 Square");
System.out.print("\n2 Right-angle Triangle");
System.out.print("\n3 Pyramid");
System.out.print("\n4 Hourglass");
System.out.print("\n5 Diamond");
System.out.print("\nEnter a integer to choose a shape: ");
String shape = userInput.nextLine();
System.out.print("\nEnter the height of the shape: ");
int inputOne = userInput.nextInt();
System.out.print("Enter a character: ");
char ch = userInput.next().charAt(0);
System.out.println("\n");
if (shape.equalsIgnoreCase("1")) {
square(ch, inputOne);
System.out.println();
}
if (shape.equalsIgnoreCase("2")) {
triangle(ch, inputOne);
System.out.println();
}
if (shape.equalsIgnoreCase("3")) {
pyramid(ch, inputOne);
System.out.println();
}
if (shape.equalsIgnoreCase("4")) {
diamond(ch, inputOne);
System.out.println();
}
if (shape.equalsIgnoreCase("5")) {
hourglass(ch, inputOne);
System.out.println();
}
} while (playAgain());
}
You need to prompt for the user input inside of the do/while loop. Right now you are prompting the user right above the loop - so it only happens once in your code.
//move this stuff into the do/while
Scanner userInput = new Scanner(System.in);
System.out.print("You can view any of the following shapes: ");
System.out.print("\n1 Square");
System.out.print("\n2 Right-angle Triangle");
System.out.print("\n3 Pyramid");
System.out.print("\n4 Hourglass");
System.out.print("\n5 Diamond");
System.out.print("\nEnter a integer to choose a shape: ");
String shape = userInput.nextLine();
System.out.print("\nEnter the height of the shape: ");
int inputOne = userInput.nextInt();
System.out.print("Enter a character: ");
char ch = userInput.next().charAt(0);
System.out.println("\n");
do {
^^^^ move that stuff in here
}while(playAgain())
It's also worth noting that this would be easy to discover if you simply step through your code in a debugger.
I want make like this:
input number[0][0]=201
input number[0][1]=202
input number[1][0]=203
input number[1][1]=204
input last = 203
then find if last input same with above, if true, s.o.p find, else not found
my code:
import java.util.Scanner;
public class array_input {
public static void main(String[] args) {
int a[][];
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print("input number[" + i + "][" + j + "]");
int b = scan.nextInt();
a[i][j] = b;
}
}
System.out.print("input what u want");
if (a[i][j] == b) {
System.out.print("found");
} else {
System.out.print("not found");
}
}
}
Maybe you mean something like this ?
import java.util.Scanner;
public class array_input
{
public static void main(String [] args){
int a [][] = new int[2][2];
Scanner scan = new Scanner(System.in);
for(int i = 0;i < 2; i++){
for(int j = 0; j < 2; j++){
System.out.printf("input number[%d][%d]=", i, j);
int b = scan.nextInt();
a[i][j]=b;
}
}
System.out.print("input last = ");
int needle = scan.nextInt();
for (int[] row : a){
for (int col : row){
if(col == needle){
System.out.println("found");
return;
}
}
}
System.out.print("not found");
}
}
Ok, I guess that this is what you want. This checks if the last input of the array is in the array (excluding the last input).
boolean valueInArray = false;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
if(a[i][j]==b && (i != 2 || j != 2)){
valueInArray = true;
}
}
}
if(valueInArray){
System.out.print("found");
} else {
System.out.print("not found");
}
I have this java code that basically just prints a christmas tree of X height.However, the program ask for the number, then print the tree and then just end.I would like it to loop until I enter 0,wich would end the program,and also I would like to make it print only if the number entered is from 1-40(not over 40).Im begining in the java world and I dont know how to do that.Heres my code for now:
public class xtree {
public static void main(String[] args)
{
Scanner scan = new Scanner(in);
out.print("please enter a number: ");
int temp = scan.nextInt();
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp-1; i++)
{
for(int j=0; j<=y; j++)
{
out.print(" ");
}
for(int k = 0; k<z; k++)
{
out.print("*");
}
out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
out.print(" ");
}
out.println("*");
}
}
Thank you, im a beginner to java so please be lenient ;)
Well, I would separate the method out into two:
A printChristmasTree method, which accepts the height as a parameter
Your main method, which just deals with taking user input and calling printChristmasTree or exiting
Most of your current main method would go into the printChristmasTree, and main would be a loop. Something like:
public static void main(String[] args) {
Scanner scan = new Scanner(in);
while (true) {
System.out.print("Please enter a number (0-40): ");
int height = scan.nextInt();
if (height == 0) {
// Exit the program
return;
} else if (height >= 1 && height <= 40) {
printChristmasTree(height);
} else {
System.out.println("Invalid input.");
}
}
}
There are other approaches you could use instead of returning from a while (true) loop, but this looks the simplest to me.
The separation of the "taking input" from the "printing the Christmas tree" aspects leads to much more readable code than keeping them combined, in my view - and it's more flexible in terms of things like writing a different program to print all valid Christmas trees.
Use a while loop:
Scanner scan = new Scanner(System.in);
System.out.print("please enter a number: ");
int temp = scan.nextInt();
while (temp>0) {
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp-1; i++)
{
for(int j=0; j<=y; j++)
{
System.out.print(" ");
}
for(int k = 0; k<z; k++)
{
System.out.print("*");
}
System.out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
System.out.print(" ");
}
System.out.println("*");
temp = scan.nextInt();
}
Here's the code for doing that:
public class xtree {
public static void main(String[] args)
{
int temp;
do{
Scanner scan = new Scanner(in);
out.print("please enter a number: ");
temp = scan.nextInt();
if(temp >= 1 && temp <= 40){ //don't display a tree higher than 40
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp-1; i++)
{
for(int j=0; j<=y; j++)
{
out.print(" ");
}
for(int k = 0; k<z; k++)
{
out.print("*");
}
out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
out.print(" ");
}
out.println("*");
}else if(temp != 0){
out.print("Please enter a number between 1 and 40!");
}
}while(temp != 0);
}
}
Use nested for loops statements to draw hallow boxes of "*"s. The boxes have the same number of rows and columns and this number should be input from the user (valid range: 5 to 21). I'm having trouble coming up with a way to make the box hollow. this is what i have for the code and it comes as a complete square, but i need it to be hollow or just the border.
System.out.println("How many rows/columns(5-21)?");
rows=input.nextInt();
while(rows<5||rows>21){
System.out.println("Out of range. Reenter: ");
rows=input.nextInt();
}
for(m=1;m<=rows;m++){
for(c=1;c<=rows;c++){
System.out.print("*");
}
System.out.println();
}
the output should look like this:
How many rows/columns (5-21)? 25
Out of range. Reenter: 7
*******
* *
* *
* *
* *
* *
*******
You need to print only some of the *s, so add a test before the print("*"). One option would be to explicitly test the four conditions (top, bottom, left, right) and OR them together logically:
if( (m==1) || //top
(m==rows) || //bottom
(c==1) || //left
(c==rows) //right
) {
System.out.print("*");
} else {
System.out.print(" ");
}
Each m== test or c== test identifies one piece of the square. Since the four tests
are ORed together, the if() is true (and a * is printed) if any one of the four tests is true. If none of them are true, the else runs and prints a space.
I also recommend renaming m to rowIndex and c to colIndex or something. When you come back to the code a week or two later, more descriptive names will make it easier to pick up where you left off. (Ask me how I know!)
import java.util.Scanner;
public class icibos {
public static void main(String[] args) {
Scanner gir = new Scanner(System.in);
System.out.print("Karenin kenarını girin: ");
int kenar = gir.nextInt();
for (int i = 1; i <= kenar; i++) {
for (int j = 1; j <= kenar; j++) {
if (i == 1 || i == kenar || j == 1 || j == kenar)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
for (int i=1;i<=lgh;i++){
for (int a=1;a<=lgh;a++){
if(i>1 && i<lgh && a>1 && a<lgh)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println("");
}
Try this , its bit hard code.
Working Example here
String myStars="*******";
String oneStar="*";
int count=0;
System.out.println(myStars);
count++;
while(count<=7)
{
System.out.println(oneStar+" "+oneStar);
count++;
}
System.out.print(myStars);
You can try this
Example is Here
String pattern;
int noOfTimes;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the pattern to print : ");
pattern = scanner.nextLine();
System.out.print("Enter number of times it should get printed : ");
noOfTimes = scanner.nextInt();
for(int i=1; i<=noOfTimes; i++) {
System.out.println();
if(i==1 || i==noOfTimes) {
for(int j=1; j<=noOfTimes; j++){
System.out.print(pattern+" ");
}
}
else {
for(int k=1; k<=noOfTimes;k++) {
if(k==1 || k == noOfTimes) {
System.out.print(pattern + " ");
}
else {
System.out.print(" ");
}
}
}
}
import java.util.Scanner;
public class holsqr{
public static void main(String args[]){
Scanner ma=new Scanner(System.in);
System.out.print("Enter the number:");
int max=ma.nextInt();
for(int i=1;i<=max;i++){
for(int j=1;j<=max;j++){
if((i==1)||(i==max)){
System.out.print("#");
}else{
if(j==1||j==max){
System.out.print("#");
}
else{
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
It pretty simple, using two while loop:
public static void main(String[] args) {
int size = 0;
int r = 0;
String star = "*";
String space = " ";
System.out.print("Input size of side of square: ");
Scanner input = new Scanner(System.in);
size = input.nextInt();
while (r < size) {
int c = 0;
while (c < size) {
System.out.print(r > 0 && r < size - 1 && c > 0 && c < size - 1 ? space : star);
++c;
}
System.out.println();
++r;
}
}