-
September 29th, 2009, 04:01 AM
#1
[RESOLVED] Java homework help!
1. Write a program that allows a user to input a sequence of consecutive numbers
A. Create a program SequentialNumbers that prompts the user for integer numbers. The program should let the user enter numbers as long as the current number is exactly one greater than the previous number.
I tried using this program:
import java.util.Scanner;
public class Sequence {
public static void main(String[] args) {
//input number which is exactly one greater or lower than the previous one, if they deviate,
// put out an error paragraph stating the difference is more than 2
int number;
int number1;
Scanner sc = new Scanner(System.in);
System.out.println("Input sequence of number differing by 1");
do {
System.out.print("Please input a number : ");
number = sc.nextInt();
System.out.print("Please input a number : ");
number1 = sc.nextInt();
} while ( ((number1 - number) == 1) || ((number1 - number) == -1) );
System.out.println("Your first number is " + number);
System.out.println("Your second number is " + number1);
System.out.println("The difference is " + (number1 - number) + ", which differs by more than 1. Try the program again");
}
}
However, it is flawed because it will not detect the difference if you input the data between 2nd and 3rd line (beginning of a new loop). This is driving me crazy, help me out please!
You are supposed to be able to input any number sequence as long as it only differs by 1. I have not learned arrays yet, I think I am supposed to complete this program using only basic for or do-while loop.
2. Write a program that allows a user to input a sequence of positive integer numbers terminated by any negative value. The program should report the smallest and greatest numbers that were entered.
A. Create a program MinMax that allows the user to input a sequence of positive integer numbers one by one.
o Each number should be compared to a Min and Max numbers which have been initialized to the first number entered by the user.
o If a new number entered is smaller that the min number, assign the value of the new number to the min number.
o If a new number entered is greater that the max number, assign the value of the new number to the max number.
o When the user enters a negative number, display the values of the min number and max number and terminate the program.
For this one, I am not even sure how to start... Using do-while loop, I can prompt for user input. However, the variable storing the user input also changes with every user input, which means, there is no way to compare values without storing it inside an array!!!! Yet, this assignment is given prior to our array class next week... Any solution?
-
September 29th, 2009, 04:31 AM
#2
Re: Java homework help!
Originally Posted by chrome_fox
However, it is flawed because it will not detect the difference if you input the data between 2nd and 3rd line (beginning of a new loop).
can u explain the problem better ??
Please use code tags like this : [SIGPIC][/SIGPIC]
Code Your Dreams
-
September 29th, 2009, 04:47 AM
#3
Re: Java homework help!
the 2nd exercise is actually quite simple, you only have to store the smallest and highest value, the rest of the numbers don't mater, so you always test if the submitted number is the highest until then, if yes you store it in a variable for the maximum value (overwriting the preveous highest value), the same goes on for the minimum
Code:
package test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int min = 1000;
int max = 0;
int number = 0;
boolean stop = false;
Scanner sc = new Scanner(System.in);
while(!stop){
System.out.println("Geef een getal: ");
number = sc.nextInt();
if (number < 0){
stop = true;
}else if (number >max){
max = number;
}else if(number < min){
min = number;
}
}
System.out.println("The maximum was " + max);
System.out.println("The minimum was " + min);
}
}
-
September 29th, 2009, 05:05 AM
#4
Re: Java homework help!
Originally Posted by holestary
can u explain the problem better ??
Okay, so the program is supposed terminate itself if the number inputted by the user differs by more than 1. However, it does not work in this case: I input 2 and 3, and i can input any number for the next one and the program would not terminate itself. The while expression is only evaluated after every loop, hence there is a flaw which I can't fix there.
-
September 29th, 2009, 05:22 AM
#5
Re: Java homework help!
Originally Posted by Shaddix
the 2nd exercise is actually quite simple, you only have to store the smallest and highest value, the rest of the numbers don't mater, so you always test if the submitted number is the highest until then, if yes you store it in a variable for the maximum value (overwriting the preveous highest value), the same goes on for the minimum
Code:
package test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int min = 1000;
int max = 0;
int number = 0;
boolean stop = false;
Scanner sc = new Scanner(System.in);
while(!stop){
System.out.println("Geef een getal: ");
number = sc.nextInt();
if (number < 0){
stop = true;
}else if (number >max){
max = number;
}else if(number < min){
min = number;
}
}
System.out.println("The maximum was " + max);
System.out.println("The minimum was " + min);
}
}
Thanks! I did not think it would be this simple, I'll need to practice more, I guess
-
September 29th, 2009, 05:33 AM
#6
Re: Java homework help!
Originally Posted by Shaddix
the 2nd exercise is actually quite simple, you only have to store the smallest and highest value, the rest of the numbers don't mater, so you always test if the submitted number is the highest until then, if yes you store it in a variable for the maximum value (overwriting the preveous highest value), the same goes on for the minimum
It would be more useful had not given direct code. if chrome_fox want to learn JAVA he/she must try first..
Okay, so the program is supposed terminate itself if the number inputted by the user differs by more than 1. However, it does not work in this case: I input 2 and 3, and i can input any number for the next one and the program would not terminate itself. The while expression is only evaluated after every loop, hence there is a flaw which I can't fix there.
i understand..
Code:
Please input a number :
2
Please input a number :
3
Please input a number : // compare here new value and number1 value..and it is 5 like this break loop..
5
Please input a number :
regards
holestary
Please use code tags like this : [SIGPIC][/SIGPIC]
Code Your Dreams
-
September 29th, 2009, 07:37 AM
#7
Re: Java homework help!
Originally Posted by chrome_fox
However, it is flawed because it will not detect the difference if you input the data between 2nd and 3rd line (beginning of a new loop). This is driving me crazy, help me out please!
You really need to figure out how your program will work before you write any code. If you can step through the sequence on paper, you'll quickly see what the problems are and how to work them out. Once you know exactly how it should work, then write the code.
In this case, your code is dealing with pairs of numbers each time in the loop, so you're comparing the 1st and 2nd, then 3rd and 4th, etc. What you want is to compare 1st and 2nd, then 2nd and 3rd, etc. This clearly means only having one number inside the loop, so you can compare each new number with the last one. If you only read one number inside the loop, you clearly have to read the first one before the loop, so you have two numbers to compare at the end of the loop. Each time round the loop, the number previously entered in the loop becomes the number the new entry must be compared with, so the sequence goes something like this:
1. Read number 2 (you'll see why at step 3)
2. Enter loop
3. Set Number 1 from number 2
4. Read number 2
5. End of loop - compare numbers and repeat loop (step 2)
Try stepping through that sequence on paper with some example numbers so that you understand exactly what it is doing, then you can think about translating it to Java. It will save you a lot of frustration in the long run.
The sole justification of teaching, of the school itself, is that the student comes out of it able to do something he could not do before. I say do and not know, because knowledge that doesn't lead to doing something new or doing something better is not knowledge at all...
J. Barzun
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 29th, 2009, 08:47 AM
#8
Re: Java homework help!
Thanks for the fast reply. I managed to form a brief idea on how to do it during dinner, and arrived at a similar method prescribed by dlorde. Once again, thanks!
-
January 30th, 2016, 12:03 PM
#9
Re: [RESOLVED] Java homework help!
I have been studying on Java courses at my high school. Well, it seems everything is clear at lesson but when I try to manage with homework it's far not easy. I looked through all internet in help search and I have to say if you want to get good knowledge and do java homework yourself, then just read Java documentation properly as well as professional literature and forums. There, you can find plenty of useful tips and get real practice and not only walking around.
Last edited by nritu; March 4th, 2016 at 01:33 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|