3 Replies - 999 Views - Last Post: 11 November 2011 - 02:35 AM Rate Topic: -----

#1 bleedblue55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 11

i am having a hard time writing a java program using while loops

Posted 10 November 2011 - 04:13 PM

I am writing a prgram that in the end while take user input to create a diamond shape using nested while loops. so far this is all i have.
import java.util.*;
public class diamond
{
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
String s;
char character;
System.out.println(" Please Enter A Character");
s= sc.next();
character = s.charAt (0);
int x;
System.out.println(" Please enter an Integer");
x = sc.nextInt();
// if (x % 2 ){
// x = x+1; }
//
// if ( x = 0) {
// System.out.println(character);}
int count;
int numCh;
numCh = 1;
int count2;
int count3;
int z;
count = 0;
z = (x-1)/2;
int spaces;
spaces = z + 1;
count3 = 0;
count2 = 0;
while (count < z + 1){


while(count2 < spaces)
{
System.out.print(" ");
count2 = count2 + 1;
}

while (count3 < numCh) {
System.out.print(character);
count3++;



count++;



}
System.out.println();
numCh = numCh + 2;
spaces = spaces - 1;
}
}}

that should print the top half in theory but i cant get it to work anyone see what my problem is?

Is This A Good Question/Topic? 0
  • +

Replies To: i am having a hard time writing a java program using while loops

#2 The_Programmer-   User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Re: i am having a hard time writing a java program using while loops

Posted 10 November 2011 - 04:17 PM

Please use the code tags.
Was This Post Helpful? 0
  • +
  • -

#3 bleedblue55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 11

Re: i am having a hard time writing a java program using while loops

Posted 10 November 2011 - 04:21 PM

import java.util.*;
public class diamond
{
public static void main(String [] args) {
  Scanner sc = new Scanner(System.in);
  String s;
  char character;
  System.out.println(" Please Enter A Character");
  s= sc.next();
  character = s.charAt (0);
  int x;
  System.out.println(" Please enter an Integer");
  x = sc.nextInt();
//   if (x % 2 ){
//       x = x+1; }
//   
//         if ( x = 0) {
//             System.out.println(character);}
  int count;
  int numCh;
  numCh = 1;
  int count2;
  int count3;
  int z;
  count = 0;
  z = (x-1)/2;
  int spaces;
  spaces = z + 1;
  count3 = 0;
  count2 = 0;
  while (count < z + 1){
 
      
      while(count2 < spaces)
      {
          System.out.print(" ");
          count2 = count2 + 1;
        }
        
        while (count3 < numCh) {
            System.out.print(character);
            count3++;
            
            
        
        count++;
        
        
            
        }
        System.out.println();
        numCh = numCh + 2;
        spaces = spaces - 1;
}
}}



my appologies did not know about those.
Was This Post Helpful? 0
  • +
  • -

#4 Veitch   User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: i am having a hard time writing a java program using while loops

Posted 11 November 2011 - 02:35 AM

Can you give some examples how the output shall look like for given inputs?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1