簡體   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