{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part33

Intro to Java Web-Notes_Part33 -...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: System.out.println("lOO or less."); The problem can be fixed by combining the two seperated statements into one compound (i.e. multi-part) statement with curly braces: if (X > 100) { System.out.println("Large number."); System.out.println("More than one hundred."); else System.out.println("lOO or less."); Cascading else-if's A series of if—else statements can be strung together. For example, lets say that you have been asked to sell magazine subscriptions and have been offered various prizes that you can earn, based on how many magazine subscriptions you sell. If you sell 20 subscriptions your prize is wristwatch, if you sell 50 then you get a small radio, and if you sell 100 subscriptions then you get a DVD player. If you sell 500 subscriptions then your prize is a new computer. import java.util.*; public class SellSubscriptions l public static void main(String args) { Scanner scan = new Scanner(System.in); System.out.print("3nter the number of subscriptions that you sold: "); String prize : ""; int sales = scan.nextlnt(); if (sales >: 500) prize : "computerl"; else i if (sales >= 100) prize : "DVD player!"; else i if (sales >= 50) prize = "radio"; else i if (sales >: 20) prize = "wristwatch"; else prize : "nothing"; l l } System.out.println("Since you sold " " sales + " subscriptions, "); System.out.println("your prize is: " W prize); The code above prompts the user for their sales amount, then reports their prize. Here is a sample run of the program in which the user enters a sales amout of 53 subscriptions: ————jGEASP exec: java SellSub3eription3 a3 E11ter the number at subscriptions that you sold: fii Since yen 331d 53 3ub3tription3, your priz e i3: radio i” i Wi L————jGRASP: operation templete. ' CD”? I The first condition that resolves to t rue will end the processing of the above i f—el se. If a condition resolves to fa l s e, then the next i f— e l s e is processed. For example, let's say that the user enters a sales amount of 600. Then the first condition resolves as true and its e l s e clause is skipped (along with the three other i f—e l s e statements contained in that el se clause): if [3ale3 3: EDD] ‘r "mri3twatthg; i . i thing"; § § } $3M“, 5% In this example (Where sales is 600) the prize variable is set to "computer!" and the rest of the cascading else-if is skipped. If the user enters a sales amount of 53, then we Will eventually arrive at the (sales >= 50) condition: ...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

Intro to Java Web-Notes_Part33 -...

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

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