Você está na página 1de 2

Section3.

10InputfromtheConsoleUsingtheScannerClass
20969:WriteastatementthatreadsawordfromstandardinputintofirstWord.AssumethatfirstWordhasalreadybeen
declaredasaStringvariable.AssumealsothatstdinisavariablethatreferencesaScannerobjectassociatedwithstandard
input.
firstWord=stdin.next()
20967:Writeastatementthatreadsanintegervaluefromstandardinputintoval.Assumethatvalhasalreadybeen
declaredasanintvariable.AssumealsothatstdinisavariablethatreferencesaScannerobjectassociatedwithstandard
input.
val=stdin.nextInt()
20968:Writeastatementthatreadsafloatingpointvaluefromstandardinputintotermperature.Assumethattermperature
hasalreadybeendeclaredasandoublevariable.AssumealsothatstdinisavariablethatreferencesaScannerobject
associatedwithstandardinput.
temperature=stdin.nextDouble()
20971:Assumethatnamehasbeendeclaredsuitablyforstoringnames(likeMisha,EmilyandSofia).Assumealso
thatstdinisavariablethatreferencesaScannerobjectassociatedwithstandardinput.Writesomecodethatreadsavalue
intonamethenprintsthemessageGreetings,NAMEonalinebyitself,whereNAMEisreplacethevaluethatwasreadinto
name.Forexample,ifyourcodereadinRachelitwouldprintoutGreetings,Rachelonalinebyitself.
name=stdin.next()
System.out.println("Greetings,"+name)
20972:Assumethatnamehasbeendeclaredsuitablyforstoringnames(likeAmy,FritzandMoustafa).Assumealso
thatstdinisavariablethatreferencesaScannerobjectassociatedwithstandardinput.Writesomecodethatreadsavalue
intonamethenprintsthemessageGreetings,NAME!!!onalinebyitselfwhereNAMEisreplacedthevaluethatwasread
intoname.Forexample,ifyoucodereadinHassanitwouldprintoutGreetings,Hassan!!!onalinebyitself.
name=stdin.next()
System.out.println("Greetings,"+name+"!!!")
20970:Assumethatnameandagehavebeendeclaredsuitablyforstoringnames(likeAbdullah,AlexandraandZoe)
andagesrespectively.AssumealsothatstdinisavariablethatreferencesaScannerobjectassociatedwithstandard
input.WritesomecodethatreadsinanameandanageandthenprintsthemessageTheageofNAMEisAGEonaline
byitself,whereNAMEandAGEarereplacedbythevaluesreadinforthevariablesnameandage.Forexample,ifyour
codereadinRohitand70thenitwouldprintoutTheageofRohitis70onalinebyitself.ThereshouldNOTbeaperiod
intheoutput.
name=stdin.next()
age=stdin.nextInt()
System.out.println("Theageof"+name+"is"+age)
20966:ThreebusinesspartnersareformingacompanywhosenamewillbeoftheformName1,Name2andName3.
However,theycantagreewhosenameshouldbefirst,secondorlast.Helpthemoutbywritingcodethatreadsintheirthree
namesandprintseachpossiblecombinationexactlyonce,onalinebyitself(thatis,eachpossiblecombinationis
terminatedwithanewlinecharacter).Assumethatname1,name2andname3havealreadybeendeclaredandusethemin
yourcode.AssumealsothatstdinisavariablethatreferencesaScannerobjectassociatedwithstandardinput.For

example,ifyourcodereadinLarry,CurlyandMoeitwouldprintoutLarry,CurlyandMoe,Curly,LarryandMoe,etc.,
eachonaseparateline.
name1=stdin.next()
name2=stdin.next()
name3=stdin.next()
System.out.println(name1+","+name2+"and"+name3)
System.out.println(name1+","+name3+"and"+name2)
System.out.println(name2+","+name1+"and"+name3)
System.out.println(name2+","+name3+"and"+name1)
System.out.println(name3+","+name2+"and"+name1)
System.out.println(name3+","+name1+"and"+name2)
20606:Givenanintvariabledatumthathasalreadybeendeclared,writeafewstatementsthatreadanintegervaluefrom
standardinputintothisvariable.
Scannerstdin=newScanner(System.in)
datum=stdin.nextInt()

Você também pode gostar