Open biryu2205 opened 7 years ago
2.Java Stdin and Stdout I
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
scanner.close();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
3.Java IF Else
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String ans = "";
if (n % 2 == 1) {
ans = "Weird";
} else {
}
System.out.println(ans);
}
}
4.Java Stdin and Stdout II
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
5.Java Output Formatting
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("================================");
for (int i = 0; i < 3; i++) {
String s1 = sc.next();
int x = sc.nextInt();
//Complete this line
System.out.printf("%-15s%03d%n", s1, x);
}
System.out.println("================================");
}
}
6.Java Loops I
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int i = 0;
int R;
while (i < 10) {
i = i + 1;
R = N * i;
System.out.print(N + " x " + i + " = " + R);
System.out.print("\n");
}
}
}
7.Java loops II
import java.util.*;
import java.io.*;
class Solution {
public static void main(String[] argh) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
}
in.close();
}
}
8.Java Datatypes
import java.util.*;
import java.io.*;
class Solution {
public static void main(String[] argh) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
try {
long x = sc.nextLong();
System.out.println(x + " can be fitted in:");
if (x >= -128 && x <= 127) System.out.println("* byte");
if (x >= Short.MIN_VALUE && x <=
Short.MAX_VALUE) {
System.out.println("* short");
}
if (x >= Integer.MIN_VALUE && x <=
Integer.MAX_VALUE) {
System.out.println("* int");
}
if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE
) {
System.out.println("* long");
}
} catch (Exception e) {
System.out.println(sc.next() + " can't be fitted anywhere.");
}
}
}
}
9.Java End-of-file
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = 0;
while (scan.hasNext()) {
String s = scan.nextLine();
i++;
System.out.printf("%d %s \n", i, s);
}
}
}
10.Java Static Initializer Block
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int B, H;
static boolean flag;
static {
Scanner cin = new Scanner(System.in);
B = cin.nextInt();
H = cin.nextInt();
if (B > 0 && H > 0) {
flag = true;
} else {
System.out.println("java.lang.Exception: Breadth and height must be positive");
}
}
public static void main(String[] args) {
if (flag) {
int area = B * H;
System.out.print(area);
}
}
}
11.Java Int to String
import java.util.*;
import java.security.*;
public class Solution {
public static void main(String[] args) {
DoNotTerminate.forbidExit();
try {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.close();
String s = Integer.toString(n);
if (n == Integer.parseInt(s)) {
System.out.println("Good job");
} else {
System.out.println("Wrong answer.");
}
} catch (DoNotTerminate.ExitTrappedException e) {
System.out.println("Unsuccessful Termination!!");
}
}
}
//The following class will prevent you from terminating the code using exit(0)!
class DoNotTerminate {
public static class ExitTrappedException extends SecurityException {
private static final long serialVersionUID = 1;
}
public static void forbidExit() {
final SecurityManager securityManager = new SecurityManager() {
@Override
public void checkPermission(Permission permission) {
if (permission.getName().contains("exitVM")) {
throw new ExitTrappedException();
}
}
};
System.setSecurityManager(securityManager);
}
}
13.Java Currency Formatter
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scanner = new Scanner(System.in);
double payment = scanner.nextDouble();
scanner.close();
/* Create custom Locale for India.
I used the "IANA Language Subtag Registry" to find India's country code */
Locale indiaLocale = new Locale("en", "IN");
/* Create NumberFormats using Locales */
NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
/* Print output */
System.out.println("US: " + us.format(payment));
System.out.println("India: " + india.format(payment));
System.out.println("China: " + china.format(payment));
System.out.println("France: " + france.format(payment));
}
}
__12.Java Currency Formatter
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scanner = new Scanner(System.in);
double payment = scanner.nextDouble();
scanner.close();
/* Create custom Locale for India.
I used the "IANA Language Subtag Registry" to find India's country code */
Locale indiaLocale = new Locale("en", "IN");
/* Create NumberFormats using Locales */
NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
/* Print output */
System.out.println("US: " + us.format(payment));
System.out.println("India: " + india.format(payment));
System.out.println("China: " + china.format(payment));
System.out.println("France: " + france.format(payment));
}
}
Not done yet. Read my issue again. You should put you code in a maven project, and push it to github instead.
Add test case. One I verify your test and code are completed, you can close this issue.
Deadline for the testing: 2017/11/08 12PM
1.Welcome to Java!