Program is exiting at the wrong time - java

I've gotten my loop to work and exit when the value '999' is entered.
I'm not sure what to do from here as my program isn't functioning exactly how I want it to.
Currently, the loop is exiting after the values 'num1' 'num2' and 'num3' have been fulfilled. I'm wanting the program to exit as soon as the value '999' is entered; not when num1, 2 and 3 have all been entered.
Here's my code:
import java.util.Scanner;
public class SumAverage {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
System.out.printf("Please Enter the first number%n");
float num1 = keyboard.nextFloat();
System.out.printf("Please enter the second number%n");
float num2 = keyboard.nextFloat();
System.out.printf("Please enter the third number%n");
float num3 = keyboard.nextFloat();
float sum = (num1 + num2 + num3); //calculates sum
float average = (num1 + num2 + num3) / 3; //calculates average
for(int x = 0; x < 1; x++) {
if (num1 == 999 || num2 == 999 || num3 == 999) {
break;
}
System.out.printf("Sum: %.0f %nAverage: %.2f", sum, average);
}
}
}

If you really want the program to end as soon as a value of 999 is entered, you can change your main() method to this:
public static void main(String[] args) {
System.out.printf("Please Enter the first number%n");
float num1 = keyboard.nextFloat();
if (num1 == 999) {
System.exit(0);
}
System.out.printf("Please enter the second number%n");
float num2 = keyboard.nextFloat();
if (num2 == 999) {
System.exit(0);
}
System.out.printf("Please enter the third number%n");
float num3 = keyboard.nextFloat();
if (num3 == 999) {
System.exit(0);
}
float sum = (num1 + num2 + num3); // calculates sum
float average = (num1 + num2 + num3) / 3; // calculates average
System.out.printf("Sum: %.0f %nAverage: %.2f", sum, average);
}

If we look at the way your code is currently written, the user will always be required to input 3 values due to not having any kind of conditionals attached.
I recommend using "System.exit" with conditionals to test if the value the user is inputting is 999. Try going from there to see what you can make happen.
Here is some documentation regarding "System.exit" to help you gain a better understanding of how it works.

Your for loop is looping only once so what I think you'll need to do is change some aspects of code
public class Calculator{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
float num1;
float num2;
float num3;
while (true){ //This will run forever until we break the loop
num1 = keyboard.nextFloat();
if (num1 == 999){
break;
}
num2 = keyboard.nextFloat ();
if (num2 == 999){
break;
}
num3 = keyboard.nextFloat ();
if (num3 == 999){
break;
}
System.out.println("The average is: " + (num1 +num2+num3)/3);
}
}
}

Related

Why wont Min and Max num update

Very new to Java (please don't laugh)and trying to do a project that asks user for a number and will keep track of the highest and lowest number when the user enter a sentinel value. My error is the I cannot get max and min values to update when going through the while loop.
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a double number, or 'q' to quit");
double currentNum = 0.0;
double maxNum=0.0;
double minNum=0.0;
int count = 0;
while(sc.hasNextDouble())
{
currentNum = sc.nextDouble();
count++;
System.out.println(count);
if(count ==1)
{
minNum = currentNum;
maxNum = currentNum;
System.out.println("Please enter a double number, or 'q' to quit");
}
else if(count!=1){
if (currentNum > maxNum)
{
currentNum = maxNum;
System.out.println("You are in currentNum > maxNum");
System.out.println(maxNum);
System.out.println("Please enter a double number, or 'q' to quit");
System.out.println(maxNum);
}
else if (currentNum < minNum)
{
currentNum = minNum;
System.out.println("You are in currentNum < minum");
System.out.println("Please enter a double number, or 'q' to quit");
}
}
}
System.out.println("Min num " + minNum);
System.out.println("Max num " + maxNum);
}
}
I think you got your logic mixed up.
When you get a number larger or smaller than maxNum you want to update maxNum to currentNum not the other way around.
Also the count == 1 if condition is redundant.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a double number, or 'q' to quit");
double currentNum = 0.0;
double maxNum=(double)Integer.MIN_VALUE;
double minNum=(double)Integer.MAX_VALUE;
int count = 0;
while(sc.hasNextDouble()) {
currentNum = sc.nextDouble();
count++;
System.out.println(count);
if (currentNum > maxNum){
maxNum = currentNum;
System.out.println("You are in currentNum > maxNum");
System.out.println("Maximum : "+maxNum);
}
else if (currentNum < minNum){
minNum = currentNum;
System.out.println("You are in currentNum < minum");
System.out.println("Minimum : "+minNum);
}
System.out.println("Please enter a double number, or 'q' to quit");
}
System.out.println("Min num " + minNum);
System.out.println("Max num " + maxNum);
}
}
One thing that I noticed is that you're comparing double variables. Generally it's not a good idea comparing floating point values because they have precision problems. You should use long or int instead. If you really don't have any other choice and have to use double, you should checkout this question: How to compare two double values in Java?
As pointed out in comment, you're updating currentNum instead of maxNum/minNum. So your else-if block for updating min-max value should look like this:
if (currentNum > maxNum) {
System.out.printf("You are in currentNum(%d) > maxNum(%d)", currentNum, maxNum);
//currentNum = maxNum; // <- Wrong, doesn't update maxNum; currentNum would be updated in next loop iteration anyway
maxNum = currentNum; // <- What you should be doing
}
else if (currentNum < minNum) {
System.out.printf("You are in currentNum(%d) < minNum(%d)", currentNum, minNum);
//currentNum = minNum; // <- Wrong, doesn't update minNum
minNum = currentNum; // <- What you should be doing
}

Not a statement? (Bluej)

So the program i question is supposed to be really simple, take three inputted numbers, and multiply them by itself and display the result, only, Bluej insists that each time I ask it to multiply, it's not a statement. (Tbf, i'm new in the java business, so i still might be 100% wrong)
private static double Square ( int num1, int num2, int num3) {
if ( num1 > 0) {
num1 * num1;
System.out.println (num1);
} else {
System.out.println( "Enter a correct number, please.");
}
if ( num2 > 0) {
num2 * num2;
System.out.println (num2);
} else {
System.out.println("Really? Again. Do the right thing this time, jeez.");
}
if ( num3 > 0) {
num3*num3;
System.out.println (num3);
} else {
System.out.println("Just make it more then one!!");
}
}
I think it's been Like this.
code
private static double Square ( int num1, int num2, int num3) {
if ( num1 > 0 && num2 > 0 && num3 > 0) {
num1 *= num1;
num2 *= num2;
num3 *= num3;
System.out.println (num1);
System.out.println (num2);
System.out.println (num3);
} else {
System.out.println( "Enter positive numbers please.");
}
return 0;
}
OR you can do like this also.
code
private static double Square ( int num1, int num2, int num3) {
if ( num1 > 0 && num2 > 0 && num3 > 0) {
System.out.println (num1*num1);
System.out.println (num2*num2);
System.out.println (num3*num3);
} else {
System.out.println( "Enter positive numbers please.");
}
return 0;
}
You always need to return double
even if it's 0.0

Print data from another method, in a while loop in the main method only once

The goal of this assignment is to read two numbers from the user, compute the distance between those numbers, then continue to ask the user for two numbers until they are equal, each time competing the distance, and the goal is to find the minimum distance between two numbers. We have to create a method that finds the minimum distance, and once the two numbers are equal, to print that minimum distance (only once). And the distance between the numbers that are equal does not count as a minimum distance. How do I do this? Thank you.
This is the code that I currently have.
double myMin = Double.MAX_VALUE;
while ( !(num1==num2)) {
double dist;
pairsMin(dist,myMin,num1,num2);
// re-ask for user input of num1 and num2
Min1 = pairsMin(dist,myMin,num1,num2);
}
System.out.print("\nThe minimum distance is: " + Min1 + "\n");
} // end of main method
public static double pairsMin( double dist, double myMin, double num1,double num2){
dist = Math.abs(num1-num2);
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
} // end of pairsMin method
You have to make minor changes to your logic. Try below
public static int pairsMin(int min, int num1, int num2) {
int dist = 0;
dist = Math.abs(num1 - num2);
if (dist != 0 && dist < min) {
min = dist;
}
return min;
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int num1;
int num2;
int mymin = Integer.MAX_VALUE;
do {
System.out.print("Enter number 1: ");
num1 = kb.nextInt();
System.out.print("Enter number 2: ");
num2 = kb.nextInt();
mymin = pairsMin(mymin, num1, num2);
} while (!(num1 == num2));
System.out.print("\nThe minimum distance is: " + mymin + "\n");
} // end of main method
This is what I know have, using your suggestions, and it is working. I didn't think that my if statement in my while loop would execute because I thought once num1==num2, that the while loop would end, but it worked. Thank you!
import java.util.Scanner;
public class hw5_pairs {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number 1: ");
double num1 = in.nextDouble();
System.out.print("Enter number 2: ");
double num2 = in.nextDouble();
double Min1;
double myMin = Double.MAX_VALUE;
double dist = Math.abs(num1-num2);
while ( !(num1==num2)) {
pairsMin(dist, num1, num2, myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
Min1 = pairsMin(dist,num1, num2, myMin);
if (num1==num2) {
System.out.print("\nThe minimum distance is: " + Min1 + "\n");
}
}
}
public static double pairsMin( double dist, double num1, double num2, double myMin){
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
}
}
My new problem is that myMin is equalling the last distance before the numbers are equal instead of the actual minimum. e.g. say the first two numbers I enter are 1 and 2, and the next are 1 and 3, and then 1 and 1. It is saying my minimum is 2.0.
This is what I'm supposed to get for the assignment.
Enter number 1: 9
Enter number 2: 1
Enter number 1: 7
Enter number 2: 2
Enter number 1: 4
Enter number 2: 4
The minimum distance is: 5.0.
Enter number 1: 20
Enter number 2: 3
Enter number 1: 23
Enter number 2: 23
5.0 + 17.0 = 22.0
MY CODE:
double myMin = Double.MAX_VALUE;
double Min1,Min2;
while ( !(num1==num2) ) {
pairsMin( num1, num2, myMin);
Min1 = pairsMin( num1, num2, myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if (num1==num2) {
System.out.print("\nThe minimum distance is: " + Min1 + "\n\n");
myMin = Double.MAX_VALUE;
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
while ( !(num1==num2)) {
pairsMin( num1, num2, myMin);
Min2 = pairsMin(num1,num2,myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if(num1==num2) {
double totMin = Min1+Min2;
System.out.print("\n" + Min1 + " + " + Min2 + " = " + totMin + "\n");
}
}
}
} // end while loop
} // end main method
public static double pairsMin( double num1, double num2, double myMin){
double dist = Math.abs(num1-num2);
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
}
}

How to print multiples of a given variable in java?

There are 3 numbers that are given to the user. These three numbers define the multiple, and the range. For example if these three numbers are (3, 6 , 17) the program should print 6,9,12,15. The first number is the base multiple and the second and third numbers are the lowest and the highest numbers (the range). I also know that I do not need all the import statements that I have.
This is what I have so far but I'm not sure how to continue.
import java.util.*;
import java.io.*;
import java.text.*;
import java.lang.Math.*;
public class printMultiplesOf{
public static void main (String [] args){
Scanner reader = new Scanner(System.in);
int num1, num2, num3;
System.out.println("Enter the 1st number");
num1 = reader.nextInt();
System.out.println("Enter the 2nd number");
num2 = reader.nextInt();
System.out.println("Enter the 3rd number");
num3 = reader.nextInt();
printMultiplesOf(num1, num2, num3);
}
public static void printMultiplesOf(int num1, int num2, int num3){
int start = num2
int end = num3
for (int i = num1; i <= num1; i++){
System.out.println(i + " ");
}
}
}
The main issue here is finding the loop's start and end point. The start point needs to the first number divisible by num1 equal or larger than num2. You can find it by dividing num2/num1 using floating-point division, ceil the result and multiply it back by num1. Similarly, the loop's end point should be the largest number divisible by num1 which is less than or equal to num3. You can find it by dividing num3/num1 using integer division (which would effectively floor the result and then multiplying it back again by num1. From there on, it's just a matter of looping in steps the size of num1. E.g.:
public static void printMultiplesOf(int num1, int num2, int num3) {
int start = ((int) Math.ceil((double) num2 / num1)) * num1;
int end = (num3 / num1) * num1;
for (int i = start; i <= end; i+= num1) {
System.out.println (i + " ");
}
}
Loop through all the numbers in the range, and check if it's a multiple.
public static void printMultiplesOf(int num1, int num2, int num3) {
for(int i=num2; i<= num3; i++){
if(i % num1 == 0)
System.out.print(i +" ");
}
}
This is your function. You simply add your num1 to i each time loop is executed.
I tried it and it is working ;)
public static void printMultiplesOf(int num1, int num2, int num3){
int multi = num1;
int start = num2;
int end = num3;
for (int i = start; i <= end; i += multi){
System.out.println(i + " ");
}
}

Fibonnaci sequence explanation

Can someone explain to me some parts of this code:
public static void sequence(int nterms){
int num1 = 0;
int num2 = 1;
int num = 2;
if (nterms <= 0){
System.out.println("Enter a positive integer");
}
else if (nterms == 1){
System.out.println(" fibonacci sequence: " + num1);
}
else{
System.out.println(num1 );
System.out.println(num2);
while (num < nterms){
int nth = num1 + num2;
System.out.println(nth);
num1 = num2;
num2 = nth;
num++;
}
}
}
The number sequence that's in the output is correct. So the code works. But why do you do num++ in the end? I know nth is previous two numbers added together but why do you nterms == 1 and print "" +num1? I don't get that.
Here it is
public static void sequence(int nterms){
/*instancation of needed variables : nth = num1+numb2 later in the code*/
int num1 = 0;
int num2 = 1;
/*num is the number of term you've encoutered, as the first two are in
the initialization, you start at 2*/
int num = 2;
if (nterms <= 0){ //check wether the function has a correct input or not*/
System.out.println("Enter a positive integer");
}
else if (nterms == 1){ /*if it's the first term we know in num1*/
System.out.println(" fibonacci sequence: " + num1);
}
else{
System.out.println(num1);/*we print the first two terms*/
System.out.println(num2);
while (num < nterms){ /*we loop till we reached the wanted number*/
/*we know a term is equal to the two terms before it, at least I hope you do*/
int nth = num1 + num2;
System.out.println(nth); /*we've got our number so we print it*/
num1 = num2; /*we make sure num1 and num2 are the last two encountered, so num1 becomre num2 and num2 become the current term (nth)*/
num2 = nth;
num++;/*we increment the variable as we've met printed a new term*/
}
}
}

Categories