Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of problem input in Java algorithm

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "case Analysis of Java algorithm problem input problem". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Given the range, determine how many data to enter

Enter the data range directly using normal Scanner, and then enter subsequent data in a loop using for.

For example:

Scanner scanner = new Scanner (System.in); / / the range of input data int n = scanner.nextInt (); for (int I = 0 arrays I < n politics I +) {arrays [I] = scanner.nextInt ();} 2. There is no given range, but the Terminator is given

Use a while loop to exit the loop when you enter a Terminator

Scanner scanner = new Scanner (System.in); / / suppose you use "0" as the Terminator / / infinite loop, compare it with the Terminator in the loop, and the same stops the loop while (true) {String str = scanner,nextLine (); if (str = = "0") {break } / / does not end, then the str processing} / / to determine whether the input data is "0", "0" to stop the loop, not "0" to continue to cycle while (! scanner.hasNext ("0") {String str = scanner.nextLine (); / / a pair of str processing, as long as the input is not "0", it can continue to cycle} 3. No given range, directly given multiple sets of data (this is the most important thing to pay attention to)

Scanner can no longer be used for input at this time, because it cannot be terminated, so we need to use (BufferedReader) character buffered input stream for input.

BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); while ((str = br.readLine ())! = null) {/ / loop when the next line of read-in data is not empty. Here str deals with the difference between next () and nextLine () in 4.Scanner.

The next () input will not contain the data after the space, but will only enter the characters before the first space. The nextLine () input can include a space and will end only when a delimiter (such as carriage return) is encountered.

Scanner scanner = new Scanner (System.in); String str1 = scanner.next (); / / input hello worldString str2 = Scanner.nextLine (); / / input hello worldSystem.out.println (str1); / / output helloSystem.out.println (str2); / / output hello world5. Enter multiple lines of numbers, unknown number of lines

There is no processing for a row of multiple numbers, and the processing method is the same as the above input.

System.out.println ("enter multiple lines of numbers:"); List list = new ArrayList (); String input = ""; while (true) {input = sc.nextLine (); if (! input.equals ("Q")) {list.add (input);} else {break }} for (String s: list) {/ / can only convert one number per line, and multiple numbers need additional operation / / int intValue = Integer.valueOf (s); System.out.println (s);} "Java algorithm problem input problem example Analysis" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report