1. Prompt the user to enter a series of positive integers until they enter -1.
2. After all of the integers have been entered, display the min and max values of all the integers to the user.
NOTE: -1 should never be considered as a min or max
can only use up to what ive learned in class so no arrays.
now the theory was, when the user inputs a number like 5 , that number becomes the max and the min. then when the next number comes in , you compare the 2 numbers and assign the value of max or min to the highest and lowest number right? been trying to figure out how to make that happen all night lol, i got the code as that i pasted at the bottom. hopefully , u can point me in the right direction cause i really dont know what to do and i know my code is probably way off lol.
int number;
int max=0;
int min=0;
int counter = 0;
System.out.println("Enter an integer ( press -1 to quit)");
do {
System.out.print("Enter a positive integer (enter -1 to end): ");
number = input.nextInt();
if (number> max ){
number = max;
}
else if (number<max ) {
number = min;
}
}
while ( number !=-1);
System.out.println("max value is " +max);
System.out.println("min value is "+min);
}
}
[/code=]
sorry about that , the code came out all wrong. is there an edit button? lol cant seem to find it
[code]
import java.util.Scanner;
public class MinMax {
public static void main(String [] args) {
// creates the scanner
Scanner input = new Scanner(System.in);
int number;
int max=0;
int min=0;
int counter = 0;
do {
System.out.print("Enter a positive integer (enter -1 to end): ");
number = input.nextInt();
if (number> max ){
number = max;
}
else if (number<max ) {
number = min;
}
}
while ( number !=-1);
System.out.println("max value is " +max);
System.out.println("min value is "+min);
}
}
sorry about that , the code came out all wrong. is there an edit button? lol cant seem to find it. updated teh code below
import java.util.Scanner;
public class MinMax {
public static void main(String [] args) {
// creates the scanner
Scanner input = new Scanner(System.in);
int number;
int max=0;
int min=0;
int counter = 0;
do {
System.out.print("Enter a positive integer (enter -1 to end): ");
number = input.nextInt();
if (number> max ){
number = max;
}
else if (number<max ) {
number = min;
}
}
while ( number !=-1);
System.out.println("max value is " +max);
System.out.println("min value is "+min);
}
}

New Topic/Question
Reply



MultiQuote



|