Uni Student
Uni Student

Reputation: 19

Java program that accepts int values until the user enters a zero, and then finds the minimum positive integer

How do I make sure that 0 is not included as the minimum? I have made sure that the answer is not a negative number, but now I do not know how to exclude 0 as the input when it is pressed in order to find the minimum.

import java.util.Scanner;
public class Q2
{
public static void main (String [] args)
{
    System.out.println("Write a list of integers and type 0 when you are finished");
    Scanner kb=new Scanner(System.in);
    int input=kb.nextInt();
    int smallestValue=Integer.MAX_VALUE; 
    boolean negative=false;
    do{
        input=kb.nextInt();
        if(!negative){
           smallestValue=input;
           negative=false;
        }
        if (input<smallestValue){
           smallestValue=input;
        }
    }
    while(input!=0);
    System.out.println(smallestValue+">0");
  }
}       

Upvotes: 2

Views: 2214

Answers (3)

Uday Chauhan
Uday Chauhan

Reputation: 1148

Solution using Do-While loop:

import java.util.Scanner;

public class Task3 {

    public static void main(String[] args) {
        System.out.println("Write a list of integers and type 0 when you are finished");
        Scanner kb = new Scanner(System.in);
        int input = kb.nextInt();
        int smallestValue = Integer.MAX_VALUE;
        do {
            if (input < smallestValue)
                smallestValue = input;
            input = kb.nextInt();
        } while (input != 0);
        System.out.println(smallestValue + ">0");
    }

}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

Test that it isn't 0 and is less than smallestValue. Change

if (input<smallestValue){

to

if (input != 0 && input<smallestValue){

but, the rest of your algorithm appears to be a little flawed. I think you wanted,

// if(!negative){
//    smallestValue=input;
//    negative=false;
// }
if (input > 0) {
   smallestValue = Math.min(smallestValue, input);
}

You also currently discard the first value. A full working example

public static void main(String[] args) {
    System.out.println("Write a list of integers and type 0 when you are finished");
    Scanner kb = new Scanner(System.in);
    int smallestValue = Integer.MAX_VALUE;
    int input;
    do {
        input = kb.nextInt();
        if (input > 0) {
            smallestValue = Math.min(input, smallestValue);
        }
    } while (input != 0);
    System.out.println(smallestValue + " > 0");
}

You could also write the inner if as

if (input > 0 && input < smallestValue) {
    smallestValue = input;
}

Upvotes: 0

Aditya K
Aditya K

Reputation: 456

I would recommend using while loop instead of dowhile loop

System.out.println("Write a list of integers and type 0 when you are finished");
Scanner kb=new Scanner(System.in);
int input=kb.nextInt();
int smallestValue=Integer.MAX_VALUE; 

while(input!=0){//loop until user inputs 0
    if(input>0) {//check for inputs greater than 0 and ignore -ve values
        if(input<smallestValue) // save the smallest value
            smallestValue = input;
    }
    input=kb.nextInt();
}
System.out.println(smallestValue+">0");

Upvotes: 1

Related Questions