Java while循环 / 数学逻辑

3

我是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);

 }
} 

该死的大学生...... - hvgotcodes
2个回答

2

在你的第一个循环中,认真思考增加tempnum的条件。当它是奇数时会发生什么?tempnum会被增加吗?


不,它没有,但是应该有。谢谢。 - barnaby jones

0

你的代码存在一些问题。我更希望你自己解决这些问题。如果你不知道如何调试代码,可以使用“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) {

希望这能有所帮助。

灰色,谢谢。这确实有很大帮助。我明白你说的使用“for”的意思了。这样会更简洁,但是任务要求使用“while”循环。我理解你的观点,如果数字顺序不正确,就不能结束循环。 - barnaby jones
然后,我会确保您拥有以下类型的模式(带有换行符):int number = 1; while (number <= 10) { ... ; // last line ; number++; } - Gray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接