89.2k views
2 votes
Give two separate print statements: one will print your name, the other will print your major. Ensure that both will print on the same line to the screen

1 Answer

7 votes

Answer:

// here is code in java.

// import package

import java.util.*;

// class definition

class Main

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// print the name

System.out.print("my name is Sam. ");

// print the major

System.out.print("my major is CS.");

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

In java, System.out.print() will print the statement but didn't go to the next line.If there is another System.out.print(), then it will also print into the same line.So here first the System.out.print() will print the name and second will print the major in the same line.

Output:

my name is Sam. my major is CS.

User Serhat
by
4.8k points