public static int getIntegerBetween(int min, int max) {
int value = 0;
do {
try {
value = Integer.parseInt(input.nextLine());
}
catch (Exception e) {
System.out.printf("Error! Please re-enter value [%d-%d]:\n", min, max);
continue;
}
} while (value < min || value > max);
return value;
}
lecture4
lecture4
java1-lecture4-MethodScope
HW1 (due by 3/27)
HW1 (due by 3/27)
2D Array
// 2D array
char[] ops = {'+', '-', '*', '/'};
int[][] numbers = {{60,50}, {40,30}, {20,10}};
for (char op : ops) {
for (int i = 0; i < numbers.length; i++) {
int result = calc(numbers[i][0], numbers[i][1], op);
System.out.printf("result = %d %s %d = %d\n", numbers[i][0], op, numbers[i][1], result);
}
}
result = 60 + 50 = 110
result = 40 + 30 = 70
result = 20 + 10 = 30
result = 60 – 50 = 10
result = 40 – 30 = 10
result = 20 – 10 = 10
result = 60 * 50 = 3000
result = 40 * 30 = 1200
result = 20 * 10 = 200
result = 60 / 50 = 1
result = 40 / 30 = 1
result = 20 / 10 = 2
Person Array
Person[] pArray = new Person[5];
// 만약 Person 객체를 하나만 생성한 후 for문에서 공유해 사용할 경우
// 마지막으로 입력된 데이터로 모든 데이터값이 치환됨
Person p = new Person();
for (int i = 0; i < 5; i++) {
System.out.print("\n\nEnter Person name : ");
Scanner input = new Scanner(System.in);
p.setName(input.nextLine()); // 입력정보
System.out.print("\n\nEnter Person age [int] : ");
p.setAge(input.nextInt()); // 입력정보
p.print();
pArray[i] = p; // 리스트에 들어간 모든 원소는 동일한 p
}
System.out.println("pArray : " + Arrays.toString(pArray));
Person[] pArray = new Person[5];
// 아래와 같이 for문 안에 Person p = new Person()와같이
// 새로운 객체를 생성해야 각자 다르게 입력된 정보가 들어가게 됨
for (int i = 0; i < 5; i++) {
Person p = new Person();
System.out.print("\n\nEnter Person name : ");
Scanner input = new Scanner(System.in);
p.setName(input.nextLine()); // 입력정보
System.out.print("\n\nEnter Person age [int] : ");
p.setAge(input.nextInt()); // 입력정보
p.print();
pArray[i] = p; // 이때 p는 새로운 Person객체
}
System.out.println("pArray2 : " + Arrays.toString(pArray2));
Integer Array
int[] integerArray = new int[3];
integerArray[0] = 1;
integerArray[1] = 2;
integerArray[2] = 3;
for (int i : integerArray)
System.out.println(i);
for (int j = 0; j < integerArray.length; j++)
System.out.println(integerArray[ j ]);
int k = 0;
while (k < integerArray.length)
System.out.println(integerArray[k++]);
// int array
// 만약 int value1를 for문에서 공유해 사용한다해도
// intArray1[i]에 값이 들어가므로 각각 다른 입력값이 들어가게 됨
int value1 = 0;
int[] intArray1 = new int[3];
for (int i = 0; i < intArray1.length; i++) {
System.out.print("Please enter int number: ");
value1 = scan.nextInt(); // 10 20 30
intArray1[i] = value1;
}
for (int v : intArray1) System.out.println(v); // 10 20 30
// int value2를 for문에서 내부에서 매번 생성해서 사용한다해도
// intArray2[i]에 값이 들어가므로 각각 다른 입력값이 들어가게 됨
int[] intArray2 = new int[3];
for (int i = 0; i < intArray1.length; i++) {
System.out.print("Please enter int number: ");
int value2 = scan.nextInt(); // 100 200 300
intArray2[i] = value2;
}
for (int v : intArray2) System.out.println(v); // 100 200 300
BasicCalculation
import java.util.Scanner;
// for, foreach, if/else-if, switch, array, 2D array
public class BasicCalculation {
public static Scanner scan = new Scanner(System.in);
// calc (using if/else-if)
public static int calc(int x, int y, char op) {
if (op == '+') return x + y;
else if (op == '-') return x - y;
else if (op == '*') return x * y;
else if (op == '/') return x / y;
else {
System.out.println("op not found");
return 0;
}
}
// calc2 (using switch)
public static int calc2(int x, int y, char op) {
switch(op) {
case '+': return x + y;
case '-': return x - y;
case '*': return x * y;
case '/': return x / y;
default:
System.out.println("op not found");
return 0;
}
}
// get user input op[+,-,*,/]
static public char getUserInputOp() {
char value = '\0';
do {
System.out.printf("Please enter the operator [+,-,*,/]: ");
try {
value = scan.next().charAt(0);
System.out.println("value=" + value);
}
catch (Exception e) {
System.out.printf("Error! Please re-enter!\n");
scan.next();
continue;
}
} while (value != '+' && value != '-' && value != '*' && value != '/');
return value;
}
public static void main(String[] args) {
// for
for (int i = 0; i < 5; i++) {
System.out.println("i=" + i);
}
// while
int j = 0;
while (j < 5) {
System.out.println("j=" + j);
j++;
}
// do-while
int k = 0;
do {
System.out.println("k=" + k);
k++;
} while (k < 5);
// break (0~4)
int x = 0;
while (true) {
if (x >= 5) break;
System.out.println("x=" + x);
x++;
}
// continue (0~4)
int y = 0;
while (y < 5) {
y++; // (1~5)
if (y % 2 == 1) // 홀수
continue;
System.out.println("y=" + y); // 짝수만 출력
}
// for-each
System.out.print("Please enter two numbers: ");
x = scan.nextInt();
y = scan.nextInt();
char[] ops = {'+', '-', '*', '/', '^'}; // char array
for (char op : ops) {
int z = calc(x, y, op);
System.out.printf("z = %d %s %d = %d\n", x, op, y, z);
}
// 2D array
int[][] numbers = {{49, 57}, {36, 29}, {88, 66}};
for (char op: ops) {
for (int i = 0; i < numbers.length; i++) {
int r = calc2(numbers[i][0], numbers[i][1], op);
System.out.printf("r = %d %c %d = %d\n",
numbers[i][0], op, numbers[i][1], r);
}
}
// user input
System.out.print("Please enter two numbers: ");
x = scan.nextInt();
y = scan.nextInt();
char op = getUserInputOp();
int w = calc2(x, y, op);
System.out.printf("w = %d %s %d = %d\n", x, op, y, w)
}
}
Enum
TripleCalculator – TripleCalculator calculates MIN/MAX/MEDIAN/MEAN/SUM of 3 numbers.
PersonTest-Gender – Person has name, age, gender
EnumTest – Weekday
lecture3
lecture3
java1-lecture3-ControlFlow
Control Flow
- If-Else/Switch/Do-While/While/For/Foreach
- Break/Continue
- UserInput
- Array