繁体   English   中英

循环/数学逻辑时的Java

[英]Java while loops/ math logic

我是Java的新手,也是while,for和if / else语句的新手。 我真的一直在与这个问题的野兽挣扎。

代码和说明如下。 它编译,但我没有按预期计算。 我不确定这是一个数学逻辑错误,循环布局错误,还是两者兼而有之。

我一直在研磨我的齿轮已经有一段时间了,而且我无法看到它。 我觉得我真的很亲密......但还是那么遥远。

码:

/* 
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);

 }
} 

在第一个循环中,仔细考虑增加tempnum的条件。 什么时候发生奇怪的事情? tempnum会增加吗?

您的代码存在许多问题。 我宁愿你自己解决这个问题。 如果您不知道如何调试代码,可以使用“println”调试来打印出变量。

取输入3和1并逐行浏览您的程序,并考虑答案将在您的头脑中(或在纸上)。 看看它是否符合您的预期结果。

以下是有关您的代码的一般注释:

  • 考虑将不同的输出分成不同的子程序: dumpOddNumbers(low, high), sumEvenNumbers(low, high), ...
  • 尝试尽可能限制变量范围。 不要在顶部定义变量,然后再使用它们。 尝试在需要之前定义它们。 这将限制您的意外后果。 除非是临时计数器,否则尽量不要重复使用变量。
  • while(tempnum <= secondnum)这些行应该是for循环。 该代码的一个问题是,如果第一个数字<然后是第二个(例如输入110),程序将永远循环,因为如果数字是奇数,则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) { 

希望这可以帮助。

暂无
暂无

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

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