Note that there are some explanatory texts on larger screens.

plurals
  1. POJava while loops/ math logic
    primarykey
    data
    text
    <p>I'm new to Java and also new to while, for, and if/else statements. I've really been struggling with this beast of a problem. </p> <p>The code and description is below. It compiles, but I'm it doesn't calculate as expected. I'm not really sure if it's a mathematical logic error, loop layout error, or both.</p> <p>I've been grinding my gears for quite some time now, and I'm not able to see it. I feel like I'm really close... but still so far away. </p> <p>Code:</p> <pre><code>/* This program uses a while loop to to request two numbers and output (inclusively) the odd numbers between them, the sum of the even numbers between them, the numbers and their squares between 1 &amp; 10, the sum of the squares of odd numbers. */ import java.io.*; import java.util.*; public class SumOfaSquare { static Scanner console = new Scanner(System.in); public static void main (String[] args) { int firstnum = 0, secondnum = 0, tempnum = 0; int sum = 0,squaresum = 0, squarenum = 0; int number = 1; String oddOutputMessage = "The odd numbers between" + firstnum + " and " + secondnum + " inclusively are:"; String evenSumMessage = "The sum of all even numbers between " + firstnum + " and " + secondnum + "is: "; String oddSquareMessage = "The odd numbers and their squares are : "; String squareMessage = "The numbers and their squares from 1-10 are : "; System.out.println ("Please enter 2 integers. The first number should be greater than the second: "); firstnum = console.nextInt(); secondnum = console.nextInt(); //used to find out if first number is greater than the second. If not, inform user of error. if (firstnum &gt; secondnum) { tempnum = firstnum; System.out.println ("You entered: " + firstnum + " and: " + secondnum); } else System.out.println ("Your first number was not greater than your second number. Please try again."); //while the frist number is greater, do this.... while (tempnum &lt;= secondnum) { //if it's odd.... if (tempnum %2 == 1) { oddOutputMessage = (oddOutputMessage + tempnum + " "); squaresum = (squaresum + tempnum * tempnum); } //otherwise it's even.. else { sum = sum + tempnum; evenSumMessage = (evenSumMessage + sum + " "); tempnum++; } } // figures squares from 1 - 10 while (number &lt;=10) { squarenum = (squarenum + number * number); squareMessage = (squareMessage + number + " " + squarenum); number++; } oddSquareMessage = oddSquareMessage + squaresum; System.out.println (oddOutputMessage); System.out.println (oddOutputMessage); System.out.println (squareMessage); System.out.println (evenSumMessage); System.out.println (oddSquareMessage); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload