4日目
21827 ワード
do-while文
ドアのように条件に合わせて調整するのは同じですが.
while文とは異なり、最初に実行され、条件が確認されます.
while,do-while文を条件式で移動
1.条件文と重複文の種類を括弧()に入れる.条件文と重複文のエラーを説明しました. while文とスキャンプログラムを使用してコードを記述し、キーボードから入力された預金、引き出し、クエリー、および終了機能を提供します.
ドアのように条件に合わせて調整するのは同じですが.
while文とは異なり、最初に実行され、条件が確認されます.
while문
while(조건)
{
실행문;
}
do -while문
do{
실행문;
}while(조건);
れんぞく文while,do-while文を条件式で移動
for(int i=1 ; i<=10 ; i++){
if(i%2!=0){
continue;
}
}
練習問題(134 p-136)1.条件文と重複文の種類を括弧()に入れる.
조건문 : if , else
반복문 : for , while , do-while
답 : 2.스위치 문에서 사용할 수 있는 변수의 타입은 int,double타입이다 -> string 타입도 사용 가능
3.for文で1から100の整数の3の倍数の和を求める.package _2021_01_22;
import java.util.*;
public class _1 {
public static void main(String[] args) {
int sum =0;
for(int i=1; i<=100 ; i++) {
if(i%3 ==0) sum+=i;
}
System.out.println("3의 배수의 합 "+sum);
}
}
4.whlie文とMath.random()メソッドを用いて,(目1,目2)の形で2つのサイコロを出力したときに生じる目(目1,目2)を,目の和が5でない場合はサイコロを続け,目の和が5であれば実行を停止するコードを記述する.
package _2021_01_20;
import java.util.*;
public class _1 {
public static void main(String[] args) {
while(true) {
int x = (int)(Math.random()*6)+1;
int y = (int)(Math.random()*6)+1;
System.out.println("("+x+","+y+")");
if(x+y==5) {
break;
}
}
}
}
5.重畳for文を用いて方程式4 x+5 y=60のすべての解を求め、(x,y)の形式で出力する.x,yは10未満の自然数である.package _2021_01_20;
import java.util.*;
public class _1 {
public static void main(String[] args) {
for(int x=1; x<=10 ; x++) {
for(int y=1; y<=10 ;y++) {
if(4*x+5*y ==60) {
System.out.println("("+x+","+y+")");
}
}
}
}
}
6.for文で実行結果と同じ三角形のコードを入力出力する
*
**
***
****
*****
package _2021_01_20;
import java.util.*;
public class _1 {
public static void main(String[] args) {
for(int x=1; x<=5 ; x++) {
for(int y=1; y<=x ;y++) {
System.out.print("*");
}
System.out.println();
}
}
}
package _2021_01_20;
import java.util.*;
public class _1 {
public static void main(String[] args) {
boolean run =true;
int balance =0;
Scanner scanner = new Scanner(System.in);
while(run) {
System.out.println("----------------------------");
System.out.println("1.예금 | 2.출금 | 3.잔고 | 4.종료");
System.out.println("----------------------------");
System.out.print("선택> ");
int n =scanner.nextInt();
if(n ==1) {
System.out.print("예금액> ");
int number= scanner.nextInt();
balance+=number;
}
if(n ==2) {
System.out.print("출금액> ");
int number= scanner.nextInt();
balance-=number;
}
if(n ==3) {
System.out.println("잔고> "+balance);
}
if(n ==4) {
break;
}
}
System.out.println("프로그램 종료");
}
}
Reference
この問題について(4日目), 我々は、より多くの情報をここで見つけました https://velog.io/@park517/4일차-r55o10yvテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol