| | |
Some difficulty with loops
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 0
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...
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:
*
**
***
****
*****
Thank you all in advance...
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)
/* 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 to convert to. It loops until the user indicates they wish to terminate. */ import java.util.*; public class ConvTempCont { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.println("Give me a temperature to convert and I will convert it to degrees\n" + "Celsius or degrees Fahrenheit."); int degIn, degOut; String degConv, quitYesNo; char degConvChar; do { System.out.print("Enter the temperature:"); degIn = kb.nextInt(); System.out.print("Enter 'C' to convert to Celsius and 'F' for Fahrenheit:"); degConv = kb.next(); degConvChar = degConv.charAt(0); switch (degConvChar) { case 'C': case 'c': degOut = 5*((degIn)-32)/9; System.out.println(degOut + " degrees Celsius."); break; case 'F': case 'f': degOut = (9*(degIn)/5)+32; System.out.println(degOut + " degrees Fahrenheit."); break; } System.out.println("Enter 'Q' to quit or hit any other key to continue."); quitYesNo = kb.next(); }while ((quitYesNo != "Q") || (quitYesNo != "q")); } }
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)
/* This program asks the user to enter the size of a triangle to print out, then prints the triangle by printing a series of lines consisting of asterisks. */ import java.util.*; public class TriangleLoop { public static void main(String[] args) { Scanner kb = new Scanner(System.in); int triangleSize, j, k; System.out.println("Enter the size of a triangle"); System.out.print("(an integer from 1 to 50):"); triangleSize = kb.nextInt(); for (j=0; j<triangleSize+1; j++) { for (k=0; k<j; k++) { System.out.print('*'); } System.out.println(); } } }
Thank you all in advance...
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Indian Developer
http://falaque.wordpress.com/
for 2nd:
java Syntax (Toggle Plain Text)
for(int j=0; j< 2*triangleSize+1; j++) { int i=j; if(j>triangleSize) i=2*triangleSize-(j+1); for (int k=0; k<i; k++) { System.out.print("*"); } System.out.println(); }
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Indian Developer
http://falaque.wordpress.com/
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
Or for 2nd one, add the following right after your for() loop:
Java Syntax (Toggle Plain Text)
for (j=0;j <triangleSize;j++ ) { for(k=triangleSize-1;k>j;k--){ System.out.print('*'); } System.out.println(); }
Last edited by new_2_java; Feb 15th, 2008 at 2:19 pm.
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 0
Use that in the while statement?
Doesn't it need to be boolean?
Doesn't it need to be boolean?
![]() |
Similar Threads
- Pointers (archived tutorial) (C++)
- C++ Structures and Functions (C++)
- awk script (Shell Scripting)
- Finding integers in an array (C++)
- need advice on nested loops. (PHP)
- help with parrallel arrays (C++)
- Pointers (C++)
- Having difficulty with a WordCount Program (Java)
- Zodiac sign (VB.NET)
- Case Study in VB.Net (VB.NET)
Other Threads in the Java Forum
- Previous Thread: setting path
- Next Thread: toUpperCase
Views: 985 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database developmenthelp draw eclipse encode error event exception file fractal game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number object online oracle print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing system template test textfields threads time title transfer tree tutorial-sample update windows working





