d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Problem
Add Reply New Topic New Poll
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Apr 2 2013 06:36pm
I have 2 variables, firstNum and secondNum, where firstNum must be less than secondNum.

I have to use while loops.

I'm trying to:

1.) Output the sum of all the even numbers between firstNum and secondNum inclusive.

2.) Output all the numbers and their squares between 1 and 10.

3.) Output the sum of the squares of all the odd numbers between firstNum and secondNum inclusive.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 2 2013 06:42pm
Post what code you already have and describe your problem
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Apr 2 2013 06:48pm
Quote (carteblanche @ Apr 2 2013 08:42pm)
Post what code you already have and describe your problem


This is 3 parts of a larger problem; my problem here is I'm not sure how I would do these.

Edit: 1.) 2.) 3.) refer to parts C,D,E, respectively.

Here is my full code so far though.. :

//Ben Graham
// Program D
//Page 321 #9/10


import java.util.*;

public class ProgD
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
int firstNum;
int secondNum;
int sumEven;
int sumSquareOdd;
char chCounter;
int counter;
//A
System.out.println("Enter two integers. The first number must be less than the second number.");
firstNum = console.nextInt();
secondNum = console.nextInt();

//B
if (firstNum % 2 == 0)
counter = firstNum + 1;
else
counter = firstNum;
while (counter <= secondNum)
{
System.out.print(counter + " ");
counter = counter + 2;
}
//Part C

//Part D

//Part E

//Part F
chCounter = 'A';

while (chCounter <= 'Z')
{
System.out.print(chCounter + " ");
chCounter++;
}
System.out.println();

}
}

This post was edited by ben_graham7 on Apr 2 2013 06:48pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 2 2013 06:56pm
C builds on B. you found a way to iterate over all the even numbers between n1 and n2. to get the sum, just use another variable and add counter to it each time

D: loop from 1 to 10. for each number, print n and n*n

E: combine the previous two ideas
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Apr 2 2013 07:07pm
I attmepted D, then got this error message:

ProgD.java:48: error: variable chCounter might not have been initialized
while (chCounter <= 'Z')
^
1 error


Here's the full code now...

//Ben Graham
// Program D
//Page 321 #9/10


import java.util.*;

public class ProgD
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
int firstNum;
int secondNum;
int sumEven;
int sumSquareOdd;
char chCounter;
int counter;
//A
System.out.println("Enter two integers. The first number must be less than the second number.");
firstNum = console.nextInt();
secondNum = console.nextInt();

//B
if (firstNum % 2 == 0)
counter = firstNum + 1;
else
counter = firstNum;
while (counter <= secondNum)
{
System.out.print(counter + " ");
counter = counter + 2;
}
//Part C

//Part D
counter = 1;
System.out.print(counter + " " + counter * counter);
while (counter <= 10)


//Part E

//Part F
chCounter = 'A';

while (chCounter <= 'Z')
{
System.out.print(chCounter + " ");
chCounter++;
}
System.out.println();

}
}
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Apr 2 2013 07:10pm
Found the problem there, but now I'm not getting the output that I want.
I am getting this:
The numbers I used were 1 and 11 by the way.

1 3 5 7 9 11 1 12 43 94 165 256 367 498 649 8110 100A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

with this code:

//Ben Graham
// Program D
//Page 321 #9/10


import java.util.*;

public class ProgD
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
int firstNum;
int secondNum;
int sumEven;
int sumSquareOdd;
char chCounter;
int counter;
//A
System.out.println("Enter two integers. The first number must be less than the second number.");
firstNum = console.nextInt();
secondNum = console.nextInt();

//B
if (firstNum % 2 == 0)
counter = firstNum + 1;
else
counter = firstNum;
while (counter <= secondNum)
{
System.out.print(counter + " ");
counter = counter + 2;
}
//Part C

//Part D
counter = 1;
while (counter <= 10)
{System.out.print(counter + " " + counter * counter);
counter++;
}

//Part E

//Part F
chCounter = 'A';

while (chCounter <= 'Z')
{
System.out.print(chCounter + " ");
chCounter++;
}
System.out.println();

}
}

This post was edited by ben_graham7 on Apr 2 2013 07:13pm
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Apr 2 2013 07:16pm
D is fixed.

This post was edited by ben_graham7 on Apr 2 2013 07:17pm
Member
Posts: 6,106
Joined: Jun 22 2008
Gold: 116.80
Apr 2 2013 10:19pm
Finished the problem; Thanks.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll