简体   繁体   中英

Java while loops/ math logic

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.

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.

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.

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'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 <=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);

 }
} 

In your first loop, think hard about the conditions under which you increment tempnum . What happens when it's odd? Does tempnum get incremented?

There are a number of problems with your code. I'd rather you work through the problem yourself. You can use "println" debugging to print out the variables along the way if you don't know how to debug code.

Take the input 3 and 1 and walk through your program line by line and think about what the answer is going to be in your head (or on paper). See if that matches your expected results.

Here are some general comments about your code:

  • Consider breaking the different output into different subroutines: dumpOddNumbers(low, high), sumEvenNumbers(low, high), ...
  • Try to limit a variables scope as much as possible. Don't define the variables at the top and then use them later. Try to define them right before you need them. This will limit your unintended consequences. Try to not re-use variables unless it is temporary counters.
  • while (tempnum <= secondnum) These sort of lines should be for loops. One of the problems with the code is that if the first number is < then the second (the input 1 10 for example), the program loops forever because tempnum is not incremented if the number is odd.
  • while (tempnum <= secondnum) should probably be for (int tempnum = firstnum; tempnum <= secondnum; tempnum++)
  • while (number <= 10) should be for (int number = 1; number <= 10; number++)
  • You define the message at the top of your program but you shouldn't tack on results later. Do something like println(msgString + resultValue) .
  • Take a look at StringBuilder() instead of msg = msg + ... type of logic. Much more efficient.
  • When you check the numbers are in the right order and spit out an error message, are you sure you want to continue? I think you should return there.
  • The following code does not match the comment. Which is correct?

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM