Week5Ch4Intro

Week5Ch4Intro - Chapter 4 Conditions and Loops Janaka...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Janaka Balasooriya CSE 110, Arizona State University Chapter 4 Conditions and Loops
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Quick Recap – Chapters 1, 2, and 3 Problem solving process and algorithm development Basic elements in C++ Data types • Arithmetic operations • Data variables and memory allocation • Compound statements ( +=, -= …) Operator precedence rules • Objects More about input/output • Standard input/output Formatting
Background image of page 2
Janaka Balasooriya CSE 110, Arizona State University Program Execution Dice Program (Partial) Structure of the program – Flow Chart Statement 1 Statement n Statement 2 - - - import java.util.Random; import java.util.Scanner; public class Dice { public static void main (String[] args) { Random ranGenerator = new Random(); Scanner keyboard = new Scanner(System.in); int dieOneSides, dieTwoSides, dieOne, dieTwo,sumOne=0, sumTwo=0; System.out.println("How many sides does die 1 have?"); dieOneSides=keyboard.nextInt(); System.out.println("How many sides does die 2 have?"); dieTwoSides=keyboard.nextInt(); dieOne = ranGenerator.nextInt(dieOneSides) + 1; System.out.println("Die 1 first roll = " + dieOne); sumOne += dieOne; dieTwo = ranGenerator.nextInt(dieTwoSides) + 1; System.out.println("Die 2 first roll = " + dieTwo); sumTwo += dieTwo; ……………………………………… dieTwo = ranGenerator.nextInt(dieTwoSides) + 1; System.out.println("Die 2 third roll = " + dieTwo); sumTwo += dieTwo; System.out.println("Die 1 rolled a total of " + sumOne + " and rolled " + sumOne/3.0 + "on average"); System.out.println("Die 2 rolled a total of " + sumTwo + " and rolled " + sumTwo/3 .0+ "on average"); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Janaka Balasooriya CSE 100, Arizona State University Need to make decisions during the execution of the program Problem to be solved (Sum):
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

Week5Ch4Intro - Chapter 4 Conditions and Loops Janaka...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online