Spaces:
Running
Running
File size: 2,826 Bytes
5ce8dad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
// use Scanner class
// to read user input
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
System.out.print("Input your name:");
Scanner reader = new Scanner(System.in);
String name = reader.nextLine();
System.out.println("Greetings, " + name);
}
}
Example output:
Input your name: Mike Madeup
Greetings, Mike Madeup
//eg2
// int in user input
//Integer.valueOf
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner reader= new Scanner(System.in);
System.out.print("Give the first number: ");
int first = Integer.valueOf(reader.nextLine());
System.out.print("Give the second number: ");
int second = Integer.valueOf(reader.nextLine());
int sum = first + second;
System.out.println("Sum: " + sum);
}
}
Example output:
Give the first number: 5
Give the second number: 11
Sum: 16
//eg3
//double in user input
//Double.valueOf
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Give the first number: ");
double first = Double.valueOf(reader.nextLine());
System.out.print("Give the second number: ");
double second = Double.valueOf(reader.nextLine());
System.out.print("Give the third number: ");
double third = Double.valueOf(reader.nextLine());
double average= (first + second + third) / 3;
System.out.println("Average: " + average);
}
}
Example output:
Give the first number: 2
Give the second number: 4.5
Give the third number: 7
Average: 4.5
"""
Write a program that asks the user to enter their name and then prints a welcome message to the user, as shown in the example below.
In the example, the user's input is given in bold. Note that the output must be exactly as shown in the example.
Input your name: Ethan Example
Welcome to the course, Ethan Example!!!
"""
import java.util.Random;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
final Random r = new Random();
Scanner reader = new Scanner(System.in);
System.out.print("Input your name: ");
String name = reader.nextLine();
System.out.println("Welcome to the course, " + name + "!!!");
}
}
"""output
Testing with input Ethan Example
Input your name: Ethan Example
Welcome to the course, Ethan Example!!!
Testing with input Niko Nonexistent
Input your name: Niko Nonexistent
Welcome to the course, Niko Nonexistent!!!
Testing with input Maddie Madeup
Input your name: Maddie Madeup
Welcome to the course, Maddie Madeup!!!
""" |