[Solved] “Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.” How can I apply real numbers


One of the easiest option you have is

java.util.Scanner

Defention: A simple text scanner which can parse primitive types and strings
using regular expressions.

  1. A Scanner breaks its input into tokens
    using a delimiter pattern, which by default matches whitespace
    .

  2. The resulting tokens may then be converted into values of different types
    using the various next methods.

Why using Scanner API?

1. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

2. A scanning operation may block waiting for input.

3 .A Scanner is not safe for multithreaded use without external synchronization.

For example:

 Scanner input = new Scanner(System.in);
 int i = sc.nextInt();
 System.out.println("the number you entered is " + i);

Explanation:

you read from console and feed scanner variable which is input and you just want to read int. at the end, you print the read number on the console

Resources

  1. first one
  2. second one

Another option is using BufferedReader API

Reads text from a character-input stream, buffering characters so as
to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The
default is large enough for most purposes.

take a look at this sample for your BufferReader need

BufferReader vs Scanner

  1. BufferedReader has significantly larger buffer memory than Scanner. Use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream.

  2. Scanner can use tokenize using custom delimiter and parse the stream into primitive types of data, while BufferedReader can only read and store String.

  3. BufferedReader is synchronous while Scanner is not. Use BufferedReader if you’re working with multiple threads.

In your case:

    int a = 0;
    int b = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter two numbers");
    a = input.nextInt();
    b = input.nextInt();
    JOption jp = new JOption();
    jp.makes10(a, b);
}

public boolean makes10(int a, int b) {
    return ((a + b) == 10 || a == 10 || b == 10);

}

solved “Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.” How can I apply real numbers