Write the last statement using the characters that were input for each integer:
System.out.println("The sum of " + line1 + " plus " + line2 +" is " + sum );
Remember that when Java sees + next to a String,
it tries to make an even longer string.
In this version of the last statement, the Strings line1
and line2
are concatenated to output String.
No conversion needs to be done.
Then,
the int
sum is converted to a String,
and concatenated to the end.
Here is a new program made by modifying the first program.
dividend
and divisor
.quotient
and the remainder
.quotient
* divisor
+ remainder
.import java.io.*; class IntDivideTest { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) ); String top, bottom; // input Strings int dividend, divisor ; // int versions of input int quotient, remainder ; // results of "/" and "%" System.out.println("Enter the dividend:"); // read the dividend top = stdin.readLine(); dividend = Integer.parseInt( top ); System.out.println("Enter the divisor:"); // read the divisor bottom = stdin.readLine(); divisor = Integer.parseInt( bottom ); quotient = dividend / divisor ; // perform int math remainder= dividend % divisor ; System.out.println( dividend + " / " + divisor + " is " + quotient ); System.out.println( dividend + " % " + divisor + " is " + remainder ); System.out.println( quotient + " * " + divisor + " + " + remainder + " is " + (quotient*divisor+remainder) ); } }
Run the program a few times. See what happens when negative integers are input.