Java while loop / math logic

I am new to Java and also new to while, for and if / else operations. I really struggled with this beast of the problem.

Code and description below. It compiles, but I don't count as expected. I'm not sure if this is a mathematical logic error, a loop layout error, or both.

I grind my gears for quite some time, and I don't see it. I feel that I am very close ... but still so far away.

the 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 & 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 > 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 <= secondnum)
 { 
  //if it odd....
  if (tempnum %2 == 1)
   {
   oddOutputMessage = (oddOutputMessage + tempnum + " ");
   squaresum = (squaresum + tempnum * tempnum);
   }

  //otherwise it even.. 
  else
   {
   sum = sum + tempnum;
   evenSumMessage = (evenSumMessage + sum + " ");
   tempnum++;
   }
 }
 // figures squares from 1 - 10
 while (number <=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);

 }
} 
+3
source share
2 answers

. , . "println", , , .

3 1 , ( ). , .

:

  • : dumpOddNumbers(low, high), sumEvenNumbers(low, high), ...
  • . , . , . . , .
  • while (tempnum <= secondnum) for. , < (, 1 10), , tempnum , .
  • while (tempnum <= secondnum) for (int tempnum = firstnum; tempnum <= secondnum; tempnum++)
  • while (number <= 10) for (int number = 1; number <= 10; number++)
  • , . - println(msgString + resultValue).
  • StringBuilder() msg = msg + ... . .
  • , , , ? , return.
  • . ?

    // while the frist number is greater, do this
    while (tempnum <= secondnum) {
    

, .

0

, tempnum. , ? tempnum?

+2

Source: https://habr.com/ru/post/1772932/


All Articles