Java program to find the minimum and maximum values of input data
Hey i'm writing this code...and well.
I'm very new to java ><...
There are two things that I am trying to do,
Basically, without using Arrays,
- I want to write a program that asks the user how many integers that they want to input, then have the user input those variables and then have the program determine and display the minimum and maximum values of whatever the user inputs.
This is what I have so far but it isn't working...
Code :
// Import Scanner Class
import java.util.Scanner;
public class maxMinValues
{
// main method is the start point of java applications
public static void main(String args[])
{
// variable to read user input
Scanner input = new Scanner(System.in);
//Title
System.out.printf("\t\n Finding the largest & smallest Value ");
System.out.printf("\t\n How many integers are ");
//Variables
int lowValue=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
while ( a >= 0 )
{
System.out.printf("\n\n Enter Integer: ");
a = input.nextInt();
if ( a == -100 )
{
break;
}
}
if (a <= b && a <= c && a <= d && a <= e || a == b || a == c || a == d || a == e)
lowValue = a;
else if (b <= a && b <= c && b <= d && b <= e || a == b || b == c || b == d || b == e)
lowValue = b;
else if (c <= a && c <= b && c <= d && c <= e || c == a || c == b || c == d || c == e)
lowValue = c;
else if (d <= a && d <= b && d <= c && d <= e || d == a || d == b || d == c || d == e)
lowValue = d;
else if (e <= a && e <= b && e <= c && e <= d || e == a || e == b || e == c || e == d)
lowValue = e;
System.out.printf ("\n Min Value:%d", lowValue);
}
int highValue=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
{
if (a >= b && a >= c && a >= d && a >= e)
highValue = a;
else if (b >= a && b >= c && b >= d && b >= e)
highValue = b;
else if (c >= a && c >= b && c >= d && c >= e)
highValue = c;
else if (d >= a && d >= b && d >= c && d >= e)
highValue = d;
else if (e >= a && e >= b && e >= c && e >= d)
highValue = e;
System.out.printf ("\n Max Value:%d", highValue);
} // end main()
} // end class
Then...
- program that converts temperatures from -5 to 15 C. by increments of 1 to Fahrenheit. Repeating it 3 times 1 with a for loop, 1 with while and 1 with do.
Code :
// Import Scanner Class
import java.util.Scanner;
public class tempConvert
{
// main method is the start point of java applications
public static void main(String args[])
{
// variable to read user input
Scanner input = new Scanner(System.in);
//Title
System.out.printf("\t\n Celcius To Fahrenheit (-5 to 15) ");
int getFahrenheit=0;
for (int f= getFahrenheit(); f <= 212; f= getFahrenheit())
System.out.println(f+"F = "+convertToC(f)+"C");
System.out.println("Enter temperature in "); // prompt for input
x = input.nextInt(); // read first integer in farenheit
result = (5.0/9)*(x-32);
System.out.println("Fahrenheit Celsius", result);
} //end method main
} // end class
- Im not sure how to input the other 2 loops aswell >< ugh.
no idea what to do after this point. I'm using blueJ btw:
if someone could help that would be wonderful.
thanks. >.<
Re: code help 2 programs. MinMax Values & Converting Temps.
Hello awake77.
Welcome to the Java Programming Forums.
I am going to look at your first program to start with..
I am a bit confused as to what you want it to do. Can you please give me an example input and expected output?
Re: code help 2 programs. MinMax Values & Converting Temps.
For a start off I sure your class name should start with a capital letter, (Java Convention ) so
public class maxMinValues
should read
public class MaxMinValues
To answer JavaPF's question
What the program should do, we have 5 variables 'a' to 'e', the program finds the lowest number and the highest number, example lets say we have a=3 b=4 c=1 d=9 and e=4
it gives 1 as the lowest number and 9 as the highest number.
but I see we have trouble getting the numbers into 'a' to 'e' you could just change the code assign a number to the variable when you declare it, and make sure that part of the code works. hang on, why not just have a array of variables like a[0] to a[4] then you input the numbers that way otherwise you would have to repeat the input variable part of the code 5 times.
like
Code :
System.out.printf("\n\n Enter Integer A: ");
a = input.nextInt();
Code :
System.out.printf("\n\n Enter Integer B: ");
b = input.nextInt();
Re: code help 2 programs. MinMax Values & Converting Temps.
Quote:
Originally Posted by
Eric
hang on, why not just have a array of variables like a[0] to a[4] then you input the numbers that way otherwise you would have to repeat the input variable part of the code 5 times.
like
System.out.printf("\n\n Enter Integer A: ");
a = input.nextInt();
System.out.printf("\n\n Enter Integer B: ");
b = input.nextInt();
I was thinking the same thing but apparently he cannot use an Array? Check the first post..
Re: code help 2 programs. MinMax Values & Converting Temps.
As for the second program there is a error with
Code :
for (int f= getFahrenheit(); f <= 212; f= getFahrenheit())
System.out.println(f+"F = "+convertToC(f)+"C");
I might try
Code :
for (int f= getFahrenheit(); f <= 212; getFahrenheit++)
System.out.println(f+"F = "+convertToC(f)+"C");
Re: code help 2 programs. MinMax Values & Converting Temps.
Is there a reason why you cant use Arrays? Here is an example ive coded using Arrays which works perfectly:
Code :
import java.util.Scanner;
import java.util.Arrays;
public class MaxMinValues {
public static void main(String[] args) {
int largest;
int smallest;
int numOfInt = 0;
int[] vars;
int count = 1;
Scanner input = new Scanner(System.in);
System.out.println("\t\n Finding the largest & smallest number.");
System.out.println("\t\n How many int values?");
System.out.println("");
numOfInt = input.nextInt();
vars = new int[numOfInt];
for (int a = 0; a < numOfInt; a++) {
System.out.println("");
System.out.println("Enter value for int #" + count + ":");
input = new Scanner(System.in);
vars[a] = input.nextInt();
count++;
}
System.out.println("");
System.out.println("All " + numOfInt + " int values entered.");
System.out.println("");
Arrays.sort(vars);
smallest = vars[0];
largest = vars[numOfInt - 1];
System.out.println("SMALLEST int value is " + smallest);
System.out.println("LARGEST int value is " + largest);
}
}
Re: code help 2 programs. MinMax Values & Converting Temps.
I am still waiting for my Hexadecimal answer on a different thread
............ in the mean time only a country full of twitters would want to keep the Fahrenheit system.. Water the most useful liquid on the plant freezes at 0ºC and evaporates at 100ºC
Tell me/us what freezes at 0ºF and evaporates at 100ºF
OK sorry about that, I must go back and focus on Java, have a nice day.