Week5Ch4Intro

# Week5Ch4Intro - Chapter 4 Conditions and Loops Janaka...

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

Janaka Balasooriya CSE 110, Arizona State University Chapter 4 Conditions and Loops

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

View Full Document
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
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"); } }

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

View Full Document
Janaka Balasooriya CSE 100, Arizona State University Need to make decisions during the execution of the program Problem to be solved (Sum):
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/08/2010 for the course CSE 110 taught by Professor Kim during the Spring '08 term at ASU.

### 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
Ask a homework question - tutors are online