943,598 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1308
  • Java RSS
Feb 14th, 2008
0

Some difficulty with loops

Expand Post »
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...

Java Syntax (Toggle Plain Text)
  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:
*
**
***
****
*****

Java Syntax (Toggle Plain Text)
  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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ecksdee is offline Offline
3 posts
since Feb 2008
Feb 15th, 2008
0

Re: Some difficulty with loops

for 1st:
use quitYesNo.compareToIgnoreCase("q");
see the doc
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Feb 15th, 2008
0

Re: Some difficulty with loops

for 2nd:


java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Feb 15th, 2008
0

Re: Some difficulty with loops

Or for 2nd one, add the following right after your for() loop:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 6
Junior Poster
new_2_java is offline Offline
127 posts
since Apr 2007
Feb 15th, 2008
0

Re: Some difficulty with loops

Click to Expand / Collapse  Quote originally posted by DangerDev ...
for 1st:
use quitYesNo.compareToIgnoreCase("q");
see the doc
Use that in the while statement?
Doesn't it need to be boolean?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ecksdee is offline Offline
3 posts
since Feb 2008
Feb 15th, 2008
0

Re: Some difficulty with loops

Thank you both for the suggestions on the second problem
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ecksdee is offline Offline
3 posts
since Feb 2008
Aug 4th, 2010
0
Re: Some difficulty with loops
how to transeverse a triangle using looping
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danzz143 is offline Offline
1 posts
since Aug 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: getBytes() does not encode some Strings in Java?
Next Thread in Java Forum Timeline: Protected access modifier in Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC