시작단과 종료단을 정의하여 3행2열로 출력하라.
import java.util.Scanner;
public class GuGuDan_3X3 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// 디폴트값 지정
int a = 2;
int b = 9;
System.out.print("시작단 >> ");
a = sc.nextInt();
System.out.print("종료단 >> ");
b = sc.nextInt();
// 시작단이 종료단보다 높은경우 (9,2를 입력), 다시 바꿔줌
if (a > b) {
int tmp = a;
a = b;
b = tmp;
}
for(int i = a; i <= b; i += 3) { // i = 2부터 9까지 2,5,8
for(int dan=0; dan<3; dan++){ // dan = 0,1,2 동안
if(dan+i<10 && dan+i <=(b)){ // dan+i가 10보다 작고, 9이하일 경우|즉, 10일 넘을 경우에만 출력
System.out.print("["+(dan+i)+"단]\t");
}
else{
System.out.println("");
}
}
System.out.println();
for(int j = 1; j < 10; j++) {
for(int k = 0; k < 3; k++) { // (2+0|2+1|2+2),(5+0|5+1|5+2), (8+0|8+1|8+2)
String gugu = (i+k) + "*" + j + "=" + (i+k)*j;
// 삼항연산자=a?b:c(a가 참이면b,거짓이면c) 10보다 작으면 gugu연산, 10이상이면 공백
System.out.print(i+k<10 && i+k <=(b)? gugu + "\t" : "");
}
System.out.println();
}
}
}
}
import java.util.Random;
import java.util.Scanner;
public class Open_Challenge_Chapter3 {
static Scanner sc;
static Random rand;
static int mn;
static int mx;
static int a;
static int num;
static int cnt;
static {
sc = new Scanner(System.in);
rand = new Random();
mn = 0;
mx = 100;
a = 1 + rand.nextInt(100);
cnt = 1;
}
public static void main(String[] args) {
while(true) {
System.out.print("1~100사이 입력 >>> ");
for(num = sc.nextInt(); a != num; num = sc.nextInt()) {
if (a > num) {
System.out.println("더 높게");
mn = num;
} else if (a < num) {
System.out.println("더 낮게");
mx = num;
}
cnt++;
System.out.println("힌트 : " + mn + "~" + mx);
}
while(true){
System.out.println("계속하시겠습니까? (Y or N) > ");
String yn = sc.next();
if (yn.equals("y")) break;
else if (yn.equals("n")){
System.out.println("수고하셨습니다.");
System.out.printf("정답입니다.%d회만에 맞추셨습니다.\n", cnt);
return;
}
}
}
}
}
package org.comstudy.ch;
public class Chapter3Ex01 {
public static void main(String[] args) {
int sum = 0, i = 0;
while (i < 100) {
sum += i;
i += 2;
}
System.out.println(sum);
ForTest.fortest();
DoWhileTest.dowhiletest();
}
}
class ForTest{
public static void fortest() {
int sum = 0;
for(int i=0;i<100;i+=2){
sum+=i;
}
System.out.println(sum);
}
}
class DoWhileTest{
public static void dowhiletest(){
int sum =0, i=0;
do{
sum+=i;
i+=2;
}while(i<100);
System.out.println(sum);
}
}
package org.comstudy.ch;
public class Chapter3Ex02 {
public static void main(String[] args) {
int n [][] = {{1}, {1,2,3}, {1}, {1,2,3,4}, {1,2}};
for(int i=0; i<n.length; i++){ // 0~5
for (int j = 0; j < n[i].length; j++) {
System.out.print(n[i][j] + " ");
}
System.out.println();
}
}
}
package org.comstudy.ch;
import java.util.Scanner;
public class Chapter3Ex03 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("정수를 입력하시오.>>");
int a = sc.nextInt();
for(int i = 0; i<a; i++){
for(int j=0; j<a-i;j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
package org.comstudy.ch;
import java.util.*;
public class Chapter3Ex04 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("소문자 알파벳 하나를 입력하시오>>");
String a = sc.next();
char c = a.charAt(0);
for (int i = 0; i <= c-'a'; i++) {
for (int j = i; j <= c-'a'; j++) {
System.out.print((char)('a'+j));
}
System.out.println();
}
}
}
package org.comstudy.ch;
import java.util.*;
public class Chapter3Ex05 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int[] arr = new int[10];
System.out.print("양의 정수 10개를 입력하시오>>");
for(int i=0; i<10;i++){
int a = sc.nextInt();
arr[i] = a;
}
System.out.print("3의 배수는 ");
for(int j=0; j< arr.length; j++){
if(arr[j]%3==0){
System.out.print(arr[j]+" ");
}
}
}
}
package org.comstudy.ch;
import java.util.*;
public class Chapter3Ex07 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int[] arr = new int[10];
int sum = 0;
//랜덤 생성 + 합
for(int i=0; i<10; i++){
arr[i] = (int)(Math.random()*10+1);
sum += arr[i];
}
System.out.print("랜덤한 정수들 : ");
for(int i=0; i<10; i++){
System.out.print(arr[i]+" ");
}
System.out.println("\n평균은 "+sum/ arr.length);
}
}
package org.comstudy.ch;
import java.util.Random;
public class Chapter3Ex09 {
static Random rand = new Random();
public static void main(String[] args) {
// 배열 생성
int [][] num_arr = new int [4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
num_arr[i][j] = rand.nextInt(10) + 1;
}
}
// 배열 출력
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(num_arr[i][j] + " ");
} System.out.println();
}
}
}
package org.comstudy.ch;
public class Chapter3Ex13 {
public static void main(String[] args) {
int n, m;
for (int i = 1; i < 100; i++) {
n = i / 10; // 10의자리
m = i % 10; // 1의자리
// 10의자리 3의 배수 and 1의자리 3의 배수
if( n % 3 == 0 && n != 0) {
if( m % 3 == 0 && m != 0 ) {
System.out.println(i + " 박수 짝짝");
} else {
// 10의자리만 3의 배수
System.out.println(i + " 박수 짝");
}
} else {
// 1의 자리만 3의 배수
if(m % 3 == 0 && m != 0)
System.out.println(i + " 박수 짝");
}
}
}
}
}
package org.comstudy.ch;
import java.util.*;
public class Chapter3Ex14 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
String cours[] = {"java", "C++", "HTML5", "컴퓨터구조", "안드로이드"};
int score[] = {95, 88, 76, 62, 55};
String n = "";
int idx;
while (true){
idx = 0;
System.out.print("과목 이름>> ");
n = sc.next();
if (n.equals("그만")) break;
for (int i=0; i< cours.length; i++){
if (cours[i].equals(n)){
idx=score[i];
}
}
if(idx!=0)
System.out.println(n+"의 점수는"+idx);
else
System.out.println("없는 과목입니다.");
}
}
}
package org.comstudy.ch;
import java.util.*;
public class Chapter3Ex15 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true){
try {
System.out.print("곱하고자 하는 두 수 입력>>");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a+"x"+b+"="+a*b);
break;
} catch (InputMismatchException e){
System.out.println("실수는 입력하면 안됩니다.");
sc.nextLine();
}
}
sc.close();
}
}