구구단 기본
public class Gugudan1 {
public static int[] calculate(int times) {
int[] result = new int[9];
for (int i = 0; i < result.length; i++) {
result[i] = times * (i + 1);
}
return result;
}
public static void main(String[] args) {
int[] result = new int[9];
for (int i = 0; i < result.length; i++) {
result[i] = 2 * (i + 1);
System.out.println(result[i]);
}
}
}
구구단 메서드로 분리
public class GugudanMethod {
public static int[] calculate(int times) { //int times - 입력값, int[] - 반환값
int[] result = new int[9]; //타입과 메서드의 반환값을 일치시켜 준다
for (int i = 0; i < result.length; i++) {
result[i] = times * (i + 1); //times의 값이 2,3...9단을 결정
}
return result; //int[] = result
}
public static void print(int[] result) { //배열에 담겨있는 값 출력
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
public static void main(String[] args) {
for (int i = 2; i < 10; i++) {
int[] result = calculate(i); //2,3,4,5...9단 까지
print(result);
}
}
}
구구단 클래스로 분리
public class GugudanClass { //클래스로 분리
public static int[] calculate(int times) {
int[] result = new int[9];
for (int i = 0; i < result.length; i++) {
result[i] = times * (i + 1); // result[2] = 2 * (i+1);
}
return result;
}
public static void print(int[] result) {
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
}
public class GugudanMain {
public static void main(String[] args) {
for (int i = 2; i < 10; i++) {
int[] result = GugudanClass.calculate(i);
GugudanClass.print(result);
}
}
}
구구단 입력값
import java.util.Scanner;
public class GugudanLast {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String value = sc.nextLine(); //사용자가 입력한 값을 문자열로 받는다
String[] inputValue = value.split(","); //사용자가 입력한 값을 쉼표(,)를 기준으로 분리한다
int first = Integer.parseInt(inputValue[0]);
int second = Integer.parseInt(inputValue[1]);
//쉽표를 기준으로 분리한 문자열을 숫자로 변환한다 - Integer클래스의 parseInt 함수는 String 타입의 숫자를 int타입으로 변환해줌
for (int i = 2; i <= first; i++) {
for (int k = 1; k <= second; k++) {
System.out.println(i + "X" + k + " = " + i * k);
}
}
sc.close();
}
}
계산기 기본
import java.util.Scanner;
public class Calculator1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("첫 번째 입력 값 : ");
int first = scanner.nextInt();
System.out.println(first);
int result = first; //앞 단계에서 계산한 결과 값은 이후 연산의 첫번째 값으로 사용해야 한다
while (true) {
System.out.println("사칙연산 기호 : ");
String symbol = scanner.next();
System.out.println(symbol);
if (symbol.equals("quit")) { //"quit"이라는 문자열을 입력하는 경우 break을 사용해 프로그램을 종료할 수 있다
System.out.println("최종 결과 값 : " + result);
break; //프로그램을 종료하려면 break 키워드를 사용하면 된다
}
System.out.println("두 번째 입력 값 : ");
int second = scanner.nextInt();
System.out.println(second);
if (symbol.equals("+")) {
result = result + second;
System.out.println("덧셈 : " + result);
} else if (symbol.equals("-")) {
result = result - second;
System.out.println("뺄셈 : " + result);
} else if (symbol.equals("*")) {
result = result * second;
System.out.println("곱셈 : " + result);
} else if (symbol.equals("/")) {
result = result / second;
System.out.println("나눗셈 : " + result);
} else {
System.out.println("사칙연산 기호가 아닙니다");
}
}
}
}
계산기 메서드로 분리
import java.util.Scanner;
/**
* 요구사항 : 사용자의 값을 입력하는 구현, 입력 값에 따라 사칙연산 구현, 사칙연산 결과 값을 출력하는 구현을 서로 다른
* 메서드로 나누어 구현한다
* 사용자의 값을 입력하는 구현은 첫 번째 숫자 값, 사칙연산 기호 및 quit, 두 번쨰 숫자 값 입력으로 나뉜다.
*/
public class CalculatorMethod {
//1
static int getFirstValue(Scanner scanner) { //static이 있는 메서드에서 가져올땐 static을 붙여야 한다
System.out.println("첫 번째 입력 값 : " );
int first = scanner.nextInt();
System.out.println(first);
return first;
}
//2
static String getSymbol(Scanner scanner) {
System.out.println("사칙연산 기호 : " );
String symbol = scanner.next();
System.out.println(symbol);
return symbol;
}
//3
static int getSecondValue(Scanner scanner) {
System.out.println("두 번째 입력 값 : ");
int second = scanner.nextInt();
System.out.println(second);
return second;
}
//4
static int calculate(int first, String symbol, int second) {
int result = 0;
if (symbol.equals("+")) {
result = result + second;
System.out.println("덧셈 : " + result);
} else if (symbol.equals("-")) {
result = result - second;
System.out.println("뺄셈 : " + result);
} else if (symbol.equals("*")) {
result = result * second;
System.out.println("곱셈 : " + result);
} else if (symbol.equals("/")) {
result = result / second;
System.out.println("나눗셈 : " + result);
} else {
System.out.println("사칙연산 기호가 아닙니다");
}
return result;
}
//5
static void print(int result) { //void
System.out.println("최종 결과값 = " + result);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//1
int first = getFirstValue(scanner);
int result = first; //앞 단계에서 계산한 결과 값은 이후 연산의 첫번째 값으로 사용해야 한다
while (true) {
//2
String symbol = getSymbol(scanner);
if (symbol.equals("quit")) {
//5
print(result);
break;
}
//3
int second = getSecondValue(scanner);
//4
result = calculate(result, symbol, second);
//result = first 이기 떄문에 변수 first 자리에 넣고 결과값은 result 이다
}
}
}
문자열 계산기
/**
* 문자열 계산기는 사칙연산의 계산 우선순위가 아닌 입력 값에 따라 계산 순서가 정해진다
*
* 문자열을 입력 받은 후(scanner의 nextLine() 메서드 활용) 빈 공백 문자열을 기준으로 문자들을 분리해야 한다
* String value = scanner.nextLine();
* String[] values = value.split(" ");
*
* 문자열을 숫자로 변경하는 방법
* int number = Integer.parseInt("문자열");
*/
import java.util.Scanner;
public class StringCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("계산할 값 입력 : " );
String value = scanner.nextLine();
System.out.println("입력 값 = " + value);
String[] values = value.split(" ");
int first = Integer.parseInt(values[0]);
System.out.println("first = " + first);
int result = first;
for(int i = 1; i < values.length; i = i + 2) {
String symbol = values[i];
System.out.println("symbol = " + symbol);
int second = Integer.parseInt(values[i + 1]);
System.out.println("second = " + second);
//4
result = CalculatorMethod.calculate(result, symbol, second);
}
CalculatorMethod.print(result);
}
}
'1일 1커밋' 카테고리의 다른 글
(개인 공부일지) 9월 20일 화 자바의 정석 기초편 객체지향부터 복습 (0) | 2022.09.20 |
---|---|
< 스프링 MVC 1편 > 백엔드 웹 개발 핵심 기술 2회독 완료 (0) | 2022.09.18 |
< 스프링 MVC 1편 > 백엔드 웹 개발 핵심 기술 복습 (0) | 2022.09.13 |