java - Finding the greatest integer from user input without using lists -
java - Finding the greatest integer from user input without using lists -
sorry if same question exists already, couldn't find actual answer.
i'd figure out way find greatest integer among user-inputted values without storing of numbers array , allowing negative numbers. problem i've been having can't initialize 'greatest' value before first asking 1 value. here's have far:
import java.util.scanner; public class findgreatest { public static void main(string[] args) { int greatest; int current; boolean first = true; boolean keepgoing = true; boolean initialized = false; scanner in = new scanner(system.in); while (keepgoing) { system.out.print("input integer: "); string input = in.nextline(); if (input.equals("")) { keepgoing = false; } else { seek { current = integer.parseint(input); if (first) { greatest = current; first = false; initialized = true; } else if (current > greatest) { // compiler error greatest = current; } } grab (numberformatexception e) { // } } } if (initialized) { system.out.println(greatest); // compiler error } } } the lastly print statement isn't working out though out looks me there should no way greatest uninitialized @ point.
if can spot error or offer cleaner solution it'd nice
edit: initializing 'greatest' random value apparently plenty prepare particular snippet
add :
int greatest = integer.min_value; delete :
if (first) { greatest = current; first = false; initialized = true; } else if (current > greatest) { greatest = current; } add:
if(current > greatest) greatest = current; java
Comments
Post a Comment