2012.10.26.  for loop  while loop  do while loop  How to choose?  Nested loop  practice.

Slides:



Advertisements
Similar presentations
Introduction to Java Programming Lecture 10 Method Benefits, Declaring, and Calling Methods.
Advertisements

Building Java Programs
Computer Programming Lab 8.
Two examples of Problem Solving in Programming H. Chad Lane University of Pittsburgh CS7: Introduction to Programming.
: Arrange the Numbers ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11481: Arrange the Numbers 解題者:李重儀 解題日期: 2008 年 9 月 13 日 題意: 將數列 {1,2,3, …,N}
序列分析工具:MDDLogo 謝勝任 林宗慶 指導教授:李宗夷 教授.
: Road Construction ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 10724: Road Construction 解題者:徐文宏 解題日期: 2011 年 4 月 20 日 題意:給一個座標圖 (-1000~1000)
1 Chapter 6 狀態機設計法 狀態機設計法的介紹 在圖形編輯器視窗下執行 在 Project Manager 視窗下執行.
JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.
1 Introduction to Java Programming Lecture 7 Flow Control : Boolean expressions and the if statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
 Pearson Education, Inc. All rights reserved break and continue Statements Break and continue statements – Alter flow of control break.
Java class Outline for loop while loop do while loop How to choose? Nested loop.
第 5 章 深入 Response 物件 製作. 網頁的轉向與強制輸出 - 讓網頁轉彎的 Redirect 敘述 運用 Response 物件的 Redirect 方法,將瀏覽器顯 示的網頁,導向至其他網頁,語法如下: Response.Redirect 網頁路徑與名稱 此網頁路徑與名稱  若是導向到同一台.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
: A-Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10930: A-Sequence 解題者:陳盈村 解題日期: 2008 年 5 月 30 日 題意: A-Sequence 需符合以下的條件, 1 ≤ a.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
資料結構實習-二.
: Expect the Expected ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11427: Expect the Expected 解題者:李重儀 解題日期: 2008 年 9 月 21 日 題意:玩一種遊戲 (a game.
Introduction to Java Programming Lecture 9 Flow Control : while do-while and for loops II.
1 Introduction to Java Programming Lecture 6 Program Input : BufferedReader & Scanner.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010.
:Product ★☆☆☆☆ 題組: Contest Archive with Online Judge 題號: 10106: Product 解題者:李育賢 解題日期: 2009 年 5 月 13 日 題意: 題目會給兩個數字 X,Y (0
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
Introduction to Java Programming Lecture 8 Flow Control : while do-while and for loops.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
第 6 章 迴圈結構 6-1 計數迴圈 6-1 計數迴圈 6-2 條件迴圈 6-2 條件迴圈 6-3 巢狀迴圈 6-3 巢狀迴圈 6-4 While/End While 迴圈 6-4 While/End While 迴圈 6-5 跳出迴圈 6-5 跳出迴圈 6-6 VB.NET 的錯誤處理 6-6 VB.NET.
Java class Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java.
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
Visual C++重點複習.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
VHDL語法(3).
程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0 程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0許翠婷
LAB 10.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Chapter 5 Loops.
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
chap4 Chapter 4 Selection Structures: if and switch Statements.
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
Computer Programming1 Computer Science 1 Computer Programming.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 類別與物件 II (Classes and Objects II) 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Design of Rock-Paper-Scissors end Print instructions Play game Ask user If (s)he wants to play again Play again? Y Print stats: wins, losses and ties start.
Chapter 4 – Loops Dr. Larry G. Thomas – University of Toledo / LCCC
Chapter 5: Control Structures II
Repetition.
TK1114 Computer Programming
Building Java Programs
CIS 110: Introduction to Computer Programming
Repetition Statements
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Computer Science Club 1st November 2019.
Presentation transcript:

 for loop  while loop  do while loop  How to choose?  Nested loop  practice

迴圈內的動作 進入迴圈 true false 離開迴圈 迴圈條件運算式

for ( i=1 ; i<=100 ; i++ ) // 迴圈條件運算式 { // 迴圈內的動作 ; }

EX: int count=0; while ( count<100 ) { System.out.println(”Hello!”); count++; }

loop- continuation condition? Statement(s) (loop body) false true count=0 (count < 100) ? System.out.println(“Hello!”); count++;

EX: int count=0; do { System.out.println(”Hello!”); count++; } while ( count<100 );

loop- continuation condition? Statement(s) (loop body) false true count=0 (count < 100) ? System.out.println(“Hello!”); count++;

 先測迴圈 : while 、 for The condition is checked before the loop body is excuted.  後測迴圈 : do while The condition is checked after the loop body is excuted.

A for loop may be used if the number of repetitions is known in advance. A while loop may be used if the number of repetitions is not fixed. A do while loop can be used to replace a while loop if the loop body has to be executed before the condition is tested.

for { ……. for { ……. }

 1. 寫一個程式,計算 可被 3 整除的數值總 和  2.Create modified versions of the Stars program to print the following pattern. Create a separate program to produce each patten. 並且讓使用者可 以輸入 stars 的高.

 3.Design and implement an application that plays the Rock- Paper-Scissors game against the computer. When played between two people, each person picks on of three options at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly chosse on of the three options(without revealing it), then prompt for the user’s selection. At that point, the program reveals both choices and prints a statement indicating if the user won, the computer won, or if it was a tie. Continue playing until the user chooses to stop, then print the number of user wins, losses, and ties.

 Ppt 下載: 

import java.util.Scanner; public class SubtractionQuiz{ public static void main(String[] args){ int number1=(int)(Math.random()*10); int number2=(int)(Math.random()*10); if(number1<number2){ int temp =number1; number1=number2; number2=temp; System.out.print("What is "+number1+" - "+number2+" ? "); Scanner input= new Scanner(System.in); int answer =input.nextInt(); if(number1-number2==answer){ System.out.println("You are correct!!"); }else{ System.out.println("Your answer is wrong\n"+number1+"-"+number2+"is"+(number1- number2)); }