Question:
help with my java homework please?
Tara D
2008-09-26 18:51:48 UTC
ok this is what im suppose to do:

using a WHILE LOOP:

prompt user for two integers, output odd numbers between
the two integers, output the even numbers between the two
integers using a while loop

using a FOR LOOP:

output all numbers and their squares between 1 and 10

using a DO WHILE LOOP:

output all the uppercase letters

this is what i have so far and i have never ending loops how can i end them so they are never ending. I am still in basic java here and learning. Thank you for any help! :-D

//******************************************************
// Tara D
// CS1 -- 10:00 -- Pgm #5
//
// Program Convert: create a while loop, for loop, and a
// do while loop
//******************************************************
/*
prompt user for two integers, output odd numbers between
the two integers, output the even numbers between the two
integers using a while loop, then using a for loop to output
all numbers and their squares between 1 and 10, and a do
while loop to output all the uppercase letters
*/
//******************************************************


import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

public class looping
{

static Scanner console = new Scanner( System.in);

public static void main(String[] args)
{


int firstNum;
int secondNum;
int oddNums = 0;
int evenNumbs = 0;
int number;


firstNum = Integer.parseInt(JOptionPane.showInputDialog("Enter an integer: "));
secondNum = Integer.parseInt(JOptionPane.showInputDialog("Enter an integer larger than first number: "));


while (firstNum > secondNum)
System.out.println("First number can't be larger than second number, try again.");

for (number = firstNum + 1; number < secondNum; number++)
{
while (number%2 == 0/2)
System.out.println(number);

while (number%2 != 0/2);
System.out.println(number);
}






}
}
Three answers:
warsql
2008-09-26 20:15:31 UTC
First:

while (firstNum > secondNum)

System.out.println("First number can't be larger than second number, try again.");



This will cause an infinite loop if firstNum is > secondNum. You need to get new input values, otherwise the value of (firstNum > secondNum) will remain true and keep printing the error message.



Next:

while (number%2 == 0/2)

System.out.println(number);

causes an infinite loop for the same reason. You need to use "if", not "while". You don't want to keep printing the number, you only want to print once if the condition (number%2 == 0/2) is true.



Same situation here:

while (number%2 != 0/2);

System.out.println(number);



Additionally, 0/2 is 0. Not sure what you are trying to do there.
?
2016-12-15 10:16:26 UTC
have you ever tried writing pseudocode? that often works for me- in simple terms attempt to understand all the subtasks first after which you would be sturdy to pass. do you recognize the thank you to loop in direction of the array? locate the minimum? locate the optimal? locate the ordinary? How do you decide directly to end all of those issues? Writing code without pseudocode first is like attempting to pass to a clean place with no map. you may get there finally, besides the shown fact that it takes lots longer and you're many times no longer very pleased once you get there.
Anonymous
2008-09-26 19:43:33 UTC
the squares one:

for(int i=1;i<=10;i++){

System.out.println(i + "^2=" + i*i);

}



uppercase letters (i is the ascii value of the letter so casting it to a char gives you the letter):

int i=65;

do{

System.out.println((char)i);

while(++i<=90);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...