84.8k views
5 votes
1. Type a statement that reads a user-entered integer into variable numUsers. Assume scnr already exists.

2.Write a statement that outputs variable numCars as follows. End with a newline.
There are 99 cars.
Note: Whitespace (blank spaces / blank lines) matters; make sure your whitespace exactly matches the expected output.
public class OutputExample {
public static void main (String [] args) {
int numCars = 99;
Scannerscnr=new Scanner(System.in);
numCars=scnr.nextInt();
System.out.println(numCars);
return;
}
}

3. Type the statements. Then, correct the one syntax error in each statement. Hints: Statements end in semicolons, and string literals use double quotes.

System.out.printl("Predictions are hard.");
System.out.print("Especially ');
System.out.println("about the future.").
System.out.println("Num is: " - userNum);
import java.util.Scanner;

public class Errors {
public static void main(String [] args) {
int userNum = 5;

System.out.println ("Predictions are hard. ");
System.out.print("Especially '");
System.out.print("about the future.");
System.out.println("Num: "- userNum);
return;
}
}

Errors.java:10: operator - cannot be applied to java.lang.String,int System.out.println("Num: "- userNum); ^ 1 error

User Jkindwall
by
4.9k points

1 Answer

5 votes

Answer:

Question 1:

int numUsers = scnr.nextInt();

Question 2:

public class OutputExample {

public static void main (String [] args) {

int numCars = 99;

Scannerscnr=new Scanner(System.in);

numCars=scnr.nextInt();

System.out.println("There are "+numCars+" cars");

return;

}

}

Question 3:

import java.util.Scanner;

public class Errors {

public static void main(String [] args) {

int userNum = 5;

System.out.println ("Predictions are hard. ");

System.out.print("Especially ");

System.out.print("about the future.");

System.out.println("Num is: "+userNum);

return;

}

}

Step-by-step explanation:

In Question 1, the statement int numUsers = scnr.nextInt(); reads a new integer value from the keyboard and assigns it to the variable numUsers.

In question 2, Concatenation is used to format the print output.

In question 3, care is taken to fix each of the syntax errors (missing double quotes, semi-colon, concatenation)

User Sdex
by
5.3k points