联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2025-02-11 11:43

Lab 2

The objectives of this lab include the understanding of if statements, loops, input, output and

strings.

Marks: each question is worth 1 mark for a total of 4 marks.

1. Write a program that prompts the user for a password. The password that must be entered is

“cmput101”. If the user enters a different password, the program will continue to prompt the user

for the password. If the user enters the correct the password, the program will print “Logging

in…” and will then exit. You may implement your program with a while loop and use the not

operator.

Sample runs:

Enter your password: cmput303

Enter your password: math313

Enter your password: spring2020

Enter your password: cmput101

Logging in...

2. Write a program that prompts the user for a fruit name. If the user enters “apple”, “pear”, or

“berry”, the program will print “We grow” followed by the fruit name that the user entered. But

if the user enters a different string, the program will print “We don’t grow this fruit!”. You may

use an if statement and the or operator.

4 Sample runs:

Enter a fruit name: apple

We grow apple

Enter a fruit name: pear

We grow pear

Enter a fruit name: berry

We grow berry

Enter a fruit name: mango

We don't grow this fruit!

3. Write a program that prompts the user for a letter and a string. The program outputs (prints)

the number of occurrences of the letter in the string.

Sample run:

Enter a letter: p

Enter a string: apple

The letter p occurs 2 times in the string apple

 

4. Write a program that inputs individual letters and creates a string of these letters except it will

exclude the letter “x”. To signal the end of the input, the user will input the letter “z”. The final

string should not include any “x” and should not include the “z”. The program must print the

final string.

Sample input and output

Enter a character: o

Enter a character: x

Enter a character: k

Enter a character: a

Enter a character: x

Enter a character: y

Enter a character: z

The final string is okay

http://www.6daixie.com/contents/9/15208.html


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp