Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Answer to problem 21
- package com.company;
- import java.util.*;
- import java.lang.*;
- public class WhileLoopPractice1 {
- public static void main(String[] args)
- {
- Random rand = new Random();
- int secretNumber = rand.nextInt(7)+3;
- while(secretNumber != 5)
- {
- secretNumber = rand.nextInt(7)+3;
- System.out.println(secretNumber);
- }
- }
- }
- Answer to problem 22
- package com.company;
- import java.util.*;
- import java.lang.*;
- public class TwoRandomInts {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- System.out.println("Type a number");
- int typedNumber = input.nextInt();
- int retypedNumber = input.nextInt();
- while(retypedNumber != 20) {
- System.out.println("Type a new number");
- retypedNumber = input.nextInt();
- if(retypedNumber > typedNumber && retypedNumber !=20){
- System.out.println(retypedNumber);
- }
- }
- }
- }
- Answer to problem 23
- package com.company;
- import java.util.*;
- import java.lang.*;
- public class WhileSums {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- System.out.println("Type a number");
- int number1 = input.nextInt();
- System.out.println("Type a second number");
- int number2 = input.nextInt();
- while(number1 != -1 || number2 != -1) {
- System.out.println(number1+number2);
- System.out.println("Type a new number");
- number1 = input.nextInt();
- System.out.println("Type a second new number");
- number2 = input.nextInt();
- }
- }
- }
- Answer to problem 25
- package com.company;
- import java.util.*;
- import java.lang.*;
- public class SentenceConcatenater {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- String sentence = "";
- String word = "";
- System.out.println("Type a sentence");
- sentence = input.nextLine();
- System.out.println("Type a second sentence");
- word = input.next();
- while(! word.equals("end")) {
- System.out.println(sentence+" "+word);
- System.out.println("Type a new sentence");
- sentence = input.nextLine();
- System.out.println("Type a second new sentence");
- word = input.next();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement