J
J
Janna16032020-05-26 17:01:55
Java
Janna1603, 2020-05-26 17:01:55

How to write a Max Value program?

public class Main {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    ArrayList<String> myList = new ArrayList<>();
    System.out.println("Введите числа");
    while (true) {
        String number = input.next();
        System.out.println(number);
    if (number == "!") {
        break;
    }

        myList.add(number);
        System.out.println(myList);
    }

    }
}


I was trying to write a program where numbers are entered first, after entering "!", the program should compare all the numbers and output the largest one. At first I tried to at least just exit the program after the "!", But it doesn't exit ... Please help

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Orkhan, 2020-05-26
Hasanly @azerphoenix

I'm a Java student myself, and I want to focus on the following points:
First,
ArrayList<String> myListyou create an ArrayList with a parameterized String data type.
From here, the following problems are possible in the future:
1) How to make sure that the user enters exactly a number, and not a word. If you want to use exactly the String type, then you should also use regexp and the method matches()and check that the user entered exactly a digit or number.
2) How to compare numbers if they are variables of type String?
Unless it will be necessary to iterate over the list and use Integer.parseInt(str)After all, you cannot compare strings and you will have to cast strings to int type.
3) if(number == "!")
Strings are best compared using the methodequals(). This is exactly why you can't exit the loop. use equals()
It's probably better to just accept an int using sc.nextInt(), and exit the loop in a different way. For example, by pressing a button that you assign to exit the loop. For example, adding an event listener for the Enter button and exiting the loop when the button is pressed. And then iterate over the list and compare the data and give the max. meaning

O
Ostic, 2020-05-26
@Ostic

int[] nums={6,-1,-2,-3,0,1,2,3,4};
Arrays.sort(nums);
System.out.println( "Minimum = " + nums[0] );
System.out.println( "Maximum = " + nums[nums. length-1] );

A
Arkady, 2020-06-04
@Esodic

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        List<Integer> myList = new ArrayList<>();
        while (true) {
            System.out.print("Введите число: ");
            String number = input.next();
            if (number.equals("!")) {   //Используйте IDE и не забивайте на её подсказки.
                                        // Хочу вам предложить перейти на Python или Kotlin.
                                        // По крайне мере на первое время там не будет возникать вопросов, "почему equals".
                break;
            } else {
                String trim = number.trim();//Убрать пробелы и прочее по бокам (можно и не использовать)
                int e = Integer.parseInt(trim); // Спарсить число
                myList.add(e);
            }
        }
        int max = max(myList);  // вместо самописной функции можно было бы использовать стримы, но так пока что проще.
                                // и опять же повод посмотреть в сторону Kotlin
        System.out.println(max);
    }

    private static int max(List<Integer> arr) {
        int max = Integer.MIN_VALUE;
        for (int i : arr) {
            if (i > max) max = i;
        }
        return max;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question