简体   繁体   English

循环/数学逻辑时的Java

[英]Java while loops/ math logic

I'm new to Java and also new to while, for, and if/else statements. 我是Java的新手,也是while,for和if / else语句的新手。 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 . 在第一个循环中,仔细考虑增加tempnum的条件。 What happens when it's odd? 什么时候发生奇怪的事情? Does tempnum get incremented? tempnum会增加吗?

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. 如果您不知道如何调试代码,可以使用“println”调试来打印出变量。

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). 取输入3和1并逐行浏览您的程序,并考虑答案将在您的头脑中(或在纸上)。 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), ... 考虑将不同的输出分成不同的子程序: 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. while(tempnum <= secondnum)这些行应该是for循环。 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. 该代码的一个问题是,如果第一个数字<然后是第二个(例如输入110),程序将永远循环,因为如果数字是奇数,则tempnum不会递增。
  • while (tempnum <= secondnum) should probably be for (int tempnum = firstnum; tempnum <= secondnum; tempnum++) while (tempnum <= secondnum)应该是for (int tempnum = firstnum; tempnum <= secondnum; tempnum++)
  • while (number <= 10) should be for (int number = 1; number <= 10; number++) while (number <= 10)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) . 执行类似println(msgString + resultValue)
  • Take a look at StringBuilder() instead of msg = msg + ... type of logic. 看看StringBuilder()而不是msg = msg + ...逻辑类型。 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. 我想你应该return那里。
  • 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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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