Some difficulty with loops

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 3
Reputation: ecksdee is an unknown quantity at this point 
Solved Threads: 0
ecksdee ecksdee is offline Offline
Newbie Poster

Some difficulty with loops

 
0
  #1
Feb 14th, 2008
Hello...
I am pretty new to Java and I am experiencing some frustration with loops. I have a few problems here and I hope that you guys could point me in the right direction.

My first problem is with the looping in the program below. No matter what is input as "quitYesNo", the program loops...

  1. /*
  2. This program asks for number to convert into degrees Celsius or degrees Fahrenheit. It also asks for a 'C' or and 'F' (ignoring the case) to indicate what the user would like
  3. to convert to. It loops until the user indicates they wish to terminate.
  4. */
  5. import java.util.*;
  6.  
  7. public class ConvTempCont
  8.  
  9. {
  10. public static void main(String[] args)
  11.  
  12. {
  13. Scanner kb = new Scanner(System.in);
  14.  
  15. System.out.println("Give me a temperature to convert and I will convert it to degrees\n"
  16. + "Celsius or degrees Fahrenheit.");
  17.  
  18. int degIn, degOut;
  19. String degConv, quitYesNo;
  20. char degConvChar;
  21.  
  22. do
  23. {
  24. System.out.print("Enter the temperature:");
  25. degIn = kb.nextInt();
  26.  
  27. System.out.print("Enter 'C' to convert to Celsius and 'F' for Fahrenheit:");
  28. degConv = kb.next();
  29.  
  30. degConvChar = degConv.charAt(0);
  31.  
  32. switch (degConvChar)
  33. {
  34. case 'C':
  35. case 'c':
  36. degOut = 5*((degIn)-32)/9;
  37. System.out.println(degOut + " degrees Celsius.");
  38. break;
  39. case 'F':
  40. case 'f':
  41. degOut = (9*(degIn)/5)+32;
  42. System.out.println(degOut + " degrees Fahrenheit.");
  43. break;
  44. }
  45.  
  46. System.out.println("Enter 'Q' to quit or hit any other key to continue.");
  47. quitYesNo = kb.next();
  48. }while ((quitYesNo != "Q") || (quitYesNo != "q"));
  49. }
  50. }

Next, I have this program that is supposed to print a star made of asterisks based on the number input by the user. For example, if the user input 5 then it should print:
*
**
***
****
*****
****
***
**
*

I figured out how to make it print HALF of that and I can't figure out how to print the other half...that is to say, my program would print this out instead:
*
**
***
****
*****

  1. /*
  2. This program asks the user to enter the size of a triangle to print out,
  3. then prints the triangle by printing a series of lines consisting of
  4. asterisks.
  5. */
  6. import java.util.*;
  7.  
  8. public class TriangleLoop
  9.  
  10. {
  11. public static void main(String[] args)
  12.  
  13. {
  14. Scanner kb = new Scanner(System.in);
  15.  
  16. int triangleSize, j, k;
  17.  
  18. System.out.println("Enter the size of a triangle");
  19. System.out.print("(an integer from 1 to 50):");
  20. triangleSize = kb.nextInt();
  21.  
  22. for (j=0; j<triangleSize+1; j++)
  23. {
  24. for (k=0; k<j; k++)
  25. {
  26. System.out.print('*');
  27. }
  28. System.out.println();
  29. }
  30. }
  31. }

Thank you all in advance...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Some difficulty with loops

 
0
  #2
Feb 15th, 2008
for 1st:
use quitYesNo.compareToIgnoreCase("q");
see the doc
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Some difficulty with loops

 
0
  #3
Feb 15th, 2008
for 2nd:


  1. for(int j=0; j< 2*triangleSize+1; j++)
  2. {
  3. int i=j;
  4. if(j>triangleSize)
  5. i=2*triangleSize-(j+1);
  6. for (int k=0; k<i; k++)
  7. {
  8. System.out.print("*");
  9. }
  10. System.out.println();
  11. }
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: Some difficulty with loops

 
0
  #4
Feb 15th, 2008
Or for 2nd one, add the following right after your for() loop:
  1. for (j=0;j <triangleSize;j++ ) {
  2. for(k=triangleSize-1;k>j;k--){
  3. System.out.print('*');
  4. }
  5. System.out.println();
  6. }
Last edited by new_2_java; Feb 15th, 2008 at 2:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 3
Reputation: ecksdee is an unknown quantity at this point 
Solved Threads: 0
ecksdee ecksdee is offline Offline
Newbie Poster

Re: Some difficulty with loops

 
0
  #5
Feb 15th, 2008
Originally Posted by DangerDev View Post
for 1st:
use quitYesNo.compareToIgnoreCase("q");
see the doc
Use that in the while statement?
Doesn't it need to be boolean?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 3
Reputation: ecksdee is an unknown quantity at this point 
Solved Threads: 0
ecksdee ecksdee is offline Offline
Newbie Poster

Re: Some difficulty with loops

 
0
  #6
Feb 15th, 2008
Thank you both for the suggestions on the second problem
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 985 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC