Thursday, November 15, 2012
Javascript Working With While Loops
// CSC 15 Lab5 Part 1 While loops
// Christopher Smith
// Got creative and added if to it to check the range values
//
import java.util.Scanner; //importing scanner pckg for user input
public class lab5p1 {
public static void main (String[] args) {
int start; //defining variables splitting up because easier for me
int end;
int count;
int sum;
Scanner keyboard = new Scanner (System.in);//defining scanner for keyboard input
System.out.println("Lets set the range"); //print out
System.out.println("Enter the first number in the range");//prompt for first number in range
start = keyboard.nextInt(); //input for first number in range
System.out.println("Now enter the last number in the range");//prompt for last number in range
end = keyboard.nextInt();//input for end of range number
if (start > end)
{
System.out.println("The end number needs to be bigger that the start of the range.");
System.out.println("Please enter a new end number");
end = keyboard.nextInt(); //re-input for end of range value
}
//setting up while loop using example 6 on readme for lab5
sum = 0;
count = start;
while ( count <= end )
{
sum = sum + count; //adding sum and count rewriting sum
count++; //going up an integer for count
}
System.out.println("After the loop, the sum is " + sum);
System.out.println("After the loop the count is " + count);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment