3 Replies - 208 Views - Last Post: 28 January 2013 - 05:54 PM Rate Topic: -----

#1 bd0652  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-September 12

I am having a trouble adding a loop or while loop to my program

Posted 28 January 2013 - 01:28 PM

I am currently in a intro java programming course at my college. My code works fine I am just trying to add a loop that will ask the user (y or n) if they would like to input another weight. The professor had posted a yesorno java program example although it is not available and still have not heard back. I emailed the lab professor who had said to add a while loop with

while( condition ) {
//do input and output of weights
//ask if they want to go again
}



although i could not get that to work either or maybe I am doing it wrong. This is what I currently have... thanks.

import java.util.*;

//I have set Planets as my main class
public class Planets 
{
	public static void main(String[] args)
	{
	
//I have declared the following variables that will be used in my program
//I used double since decimals will be used in weights.

	double weight;
	double mercury;
	double venus;
	double jupiter;
	double saturn;
	
//I have created a Scanner object and connected it to the system in object

	
	Scanner scan = new Scanner(System.in);

//I have prompted the user to input a value for the weight variable
	
	System.out.println("Please enter your Earth weight in pounds.");
	weight = scan.nextDouble();
	
	
//I have included the calculations used to determine weights on other planets

	mercury = 0.4 * weight;
	venus = 0.9 * weight;
	jupiter = 2.5 * weight;
	saturn = 1.1 * weight;
	
//This line will display the weights.

	System.out.print("Your weight on the following planets are: " + "\nMercury =" + mercury + "\nVenus = " + venus + "\nJupiter = " + jupiter + "\nSaturn =" + saturn);

//This will ask the user if they would like to enter another weight

	{String cont= "Y";
	while(cont.equals("Y"));
	
	System.out.println("Do you want to continue Y/N ?");
	cont=scan.nextLine();
	}
	}
	}
	
	
	


Is This A Good Question/Topic? 0
  • +

Replies To: I am having a trouble adding a loop or while loop to my program

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2884
  • View blog
  • Posts: 9,568
  • Joined: 12-December 12

Re: I am having a trouble adding a loop or while loop to my program

Posted 28 January 2013 - 01:55 PM

You have to put all the code that will be repeated inside the while loop. Something like

    String cont= "Y";
    while (cont.equals("Y")) 
    {
        // bulk of your code here..
        
        System.out.println("Do you want to continue Y/N ?");
        cont=scan.nextLine();
        // the program will loop to the top of this 
        // section, checking if the value is still "Y"
    }

I'm setting it to "Y" initially so that the code will run at least once, before asking if they want to go again.

This post has been edited by andrewsw: 28 January 2013 - 01:59 PM

Was This Post Helpful? 1
  • +
  • -

#3 bd0652  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-September 12

Re: I am having a trouble adding a loop or while loop to my program

Posted 28 January 2013 - 05:19 PM

OK I got it to work but I was trying to add an either toUpperCase() or equalsIgnoreCase( ) to read Y or y from the user. Now It will not loop at all. Also I assume toUpperCase() would be easier to put in than equal?

import java.util.*;

//I have set Planets as my main class
public class Planets 
{
	public static void main(String[] args)
	{
	
//I have declared the following variables that will be used in my program
//I used double since decimals will be used in weights.


	double weight;
	double mercury;
	double venus;
	double jupiter;
	double saturn;
	
//I have created a Scanner object and connected it to the system in object

	
	Scanner scan = new Scanner(System.in);

//I have prompted the user to input a value for the weight variable
//Below is where I have input the while loop
	String cont = "Y";
	while (cont.equals("Y"))
		  {
	
	System.out.println("Please enter your Earth weight in pounds.");
	weight = scan.nextDouble();
	
	
//I have included the calculations used to determine weights on other planets

	mercury = 0.4 * weight;
	venus = 0.9 * weight;
	jupiter = 2.5 * weight;
	saturn = 1.1 * weight;
	
//This line will display the weights.

	System.out.print("Your weight on the following planets are: " + "\nMercury = " + mercury + "\nVenus = " + venus + "\nJupiter = " + jupiter + "\nSaturn = " + saturn);
	
	System.out.println("Do you want to input another weight? Y/N");
	cont = scan.nextLine();
//I have ended the loop. It had worked but does not work anymore?
	
}	
}
}
	

Was This Post Helpful? 0
  • +
  • -

#4 bd0652  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-September 12

Re: I am having a trouble adding a loop or while loop to my program

Posted 28 January 2013 - 05:54 PM

I finished it at a different approach. Thank you for your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1