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...

/*
	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:
*
**
***
****
*****

/*
	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...

Recommended Answers

All 6 Replies

for 1st:
use quitYesNo.compareToIgnoreCase("q");
see the doc

for 2nd:

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();
	}

Or for 2nd one, add the following right after your for() loop:

for (j=0;j <triangleSize;j++ ) {
      for(k=triangleSize-1;k>j;k--){
            System.out.print('*');
      }
      System.out.println();
}

for 1st:
use quitYesNo.compareToIgnoreCase("q");
see the doc

Use that in the while statement?
Doesn't it need to be boolean?

Thank you both for the suggestions on the second problem :D

how to transeverse a triangle using looping

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.